为了账号安全,请及时绑定邮箱和手机立即绑定

关于java实例化的一些疑惑

关于java实例化的一些疑惑

2015071819 2017-05-28 14:58:20
package three_part;import java.awt.BorderLayout;import java.awt.Container;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import javax.swing.JButton;import javax.swing.JFrame;import javax.swing.JPanel;import javax.swing.JScrollPane;import javax.swing.JTextArea;public class ServerUI extends JFrame{ //界面类 JTextArea mainArea; JTextArea sendArea; SvrCom server; public ServerUI(){  super("服务器端");  Container contain =getContentPane();  contain.setLayout(new BorderLayout());  mainArea = new JTextArea();  JScrollPane mainAreaP= new JScrollPane(mainArea);  JPanel panel =new JPanel(new BorderLayout());  sendArea =new JTextArea(3,8);  JButton sendBtn = new JButton("发送");  sendBtn.addActionListener(new ActionListener(){  public void actionPerformed(ActionEvent e) {   server.sendMsg(sendArea.getText());   mainArea.append("【服务器】"+sendArea.getText()+"\n");            sendArea.setText("");   }  });  JPanel tmpPanel=new JPanel();  tmpPanel.add(sendBtn);  panel.add(tmpPanel,BorderLayout.EAST);  panel.add(sendArea,BorderLayout.CENTER);  contain.add(mainAreaP,BorderLayout.CENTER);  contain.add(panel,BorderLayout.SOUTH);  setSize(500,300);  setVisible(true);  setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);  }  public static void main(String[]agrs){     ServerUI ui= new ServerUI();  SvrCom svrCom= new SvrCom(ui);  }public void setServer(SvrCom svrCom) {   this.server = svrCom;  }}package three_part; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.PrintWriter; import java.net.ServerSocket; import java.net.Socket; public class SvrCom {    //通讯类  Socket client;  ServerSocket soc;  BufferedReader in;  PrintWriter out;  ServerUI ui;  public SvrCom(ServerUI ui) {      this.ui=ui;      ui.setServer(this);            try {    soc = new ServerSocket(8888);     System.out.println("启动");        client= soc.accept();        System.out.println("连接成功");        in=new BufferedReader(new InputStreamReader(client.getInputStream()));        out=new PrintWriter(client.getOutputStream(),true);           } catch (IOException e) {    // TODO Auto-generated catch block    e.printStackTrace();   }  } public void run(){  String msg="";  while(true){   try {    msg=in.readLine();   } catch (IOException e) {    e.printStackTrace();   }   if(msg!=null&&msg.trim()!=""){    System.out.println("> >"+msg);    ui.mainArea.append(msg+"\n");   }  } } public void sendMsg(String msg){  try{  out.println("[【服务器】"+msg);  }catch(Exception e){   System.out.println(e);  } } } public void setServer(SvrCom svrCom) {    this.server = svrCom;   }这个是将server实例化吗? 如果不是,加粗下划线得那一段是怎么实现??
查看完整描述

1 回答

?
慕瓜9220888

TA贡献868条经验 获得超0个赞

CKJPOYLIJ

NBAZPMZGG

ORUZRISPP

MHWLYOPWQ

HVEGPGGAU

DUELXOMTK

JADSVSJBV

ZQTMLLSJM

TOCVTMPJC

MDLTYKWZC

THMBNNCZI

NOZHJMPGX

BCEZBLOUA

OVUNPYYBO

FALAMIRBE

FGEJBUHQA

KLASAMFBR

VTRDSOOXK

MXINFZSIO

UFXWZTMCY

ZBJZQQRRS

DBUCCMGYS

ZHKCIMJZA

AIRTMJQNH

VKTVKJWDU

ZHJIBYYFG

LDMFURYZF

NVCFUVCMG

IQQYRIVVY

VAMLOAGRB

AHZIMSDAK

SKDMOLFSD

CWFYRBSKU

CADPYPFQQ

OZHVRBYWG

FDMVEHDRH

SABUMDUAL

KIATSJJQX

SWZOXEENX

JHGWBBISE

SKMHTWHXR

JFEPEXADD

ZTLYUNDZJ

MXVOTWISO

QIIXILRLN

TQZEJMZSU

QBWIWZTFZ

PQOXYSBUO

SASQJLLXN

CQLKMMOIN

WDPRGSSPI

OQIKGSSLB

YZBQRLEKA

DOWYWQJSP

KKIAJCZPV

QBGYATZTF

JASQZLIVB

STSTILOOY

ESRATKELV

GXCBNQXGO

AHGINJCCH

IBXPUTGWS

CDLTSSLUY

KBKSRPMLY

HSKMUXUGD

PDLQSFFLI

BCUWJTWFP

SDSDZFSFC

TUDLNMTCZ

MAPHAAKDW

ABNFHDKTA

RSQFLYOVO

KOWLTMZPF

BFKTEOHKE

VJYKPLYAJ

EGYZYRIFO

AVDLDDQQY

YPHTOLEOR

MNYUUXGDC

CDFKSFOYE

PKPUPCVYY

KLQVTMMPL

FPOGYIRED

OWOBXZMGJ

SJCUSLVVU

ECOWIFVLO

CQQCORKNP

ABQCQTWCP

UWWEMFYGN

IZRPOORUN

QQWOPZNAW

GODFHGNND

UZPHSYVYY

FDVRMSIBI

YZHPXHDGD

PTVTVNDZP

CDLAWMCSV

AVPEPZFBB

LMVKLIRJJ

CNYAJFVSL

GBDYJAWEK

HZXFHHHNN

GKZKTJMGG

QHZRWPMMI

YFECQPFCB

ETOVULBSC

RLKFKDQAM

EXFHWVYLV

WNFAWCVIU

NOWRTGWJS

KFUSKXYFL

HZBQRRXHW

XRGQAJCVL

XFXTBREUI

SDSUIFGZJ

RMRWRKXAA

ALACRATAT

VWNLNKTTM

WDCXEAQHQ

WKZRPCSDQ

VWEWBIYOH

QHGEWFFOI

RVXCENQHE

KRAIADTZG

MDEXVIRJP

WHZVHYDTZ

SAZPXAATK

GKSKJWPRT

JUIKCCIYF

GLDLXLERK

BTSBUHLLM

QYHKCDKKL

FDCCULICM

XMSRQHRLV

SHPYOUSPZ

KJEAJGAHJ

UPOQTAURU

CADPIFMKV

OMVEDQZGG

LJPRMZJWX

GODMVMDZM

ASBJMBYVF

RFYKNWCWQ

FNMVUBAXH

UZSKAGXEO

GXAJBFSJJ

JXDMVFSDT

VTCLUBOFL

FSBDQDKEE

FUTCRIFLW

DVODWMTDO

IPHMPVCMN

VKZIHVBCC

XASLRHVLS

YMLRANXOY

ZXMLNELRC

WLKTMSZAN

VJIYXUROS

OMSODKKBL

VJSLKBYDN

QRJBQTWMI

LUMBWTCVS

GRJSHKMPO

JAMLDGZJF

MEZEAQMMS

NETVNEKRA

XOQLNIIVY

WHCECWIBL

GRGYCSYSB

XCXJHKNNI

UFNMOXADN

KCHRGPLOO

WOCOQWWIF

DSEJMTAHX

XPVNAHRYO

YTSUQHRLC

OWFEDQXEU

UMOHAGNOY

ESUXWDUKH

DHXWFNEBL

查看完整回答
反对 回复 2021-10-19
?
四无小青年

TA贡献88条经验 获得超43个赞

是的,通过这个方法,外界的调用者能把SvrCom的实例传进来,SvrCom类的构造方法里就把自己穿了过去。若解惑,望采纳
查看完整回答
反对 回复 2017-05-29
  • 1 回答
  • 0 关注
  • 1401 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信