当前位置: 技术问答>java相关
一个关于 instanceof 的问题。
来源: 互联网 发布时间:2015-03-09
本文导语: 下面是sun的教程中的一段applet,再instanceof那一句中。使用了Receiver。但是这个applet中从来没有声明过这个类啊,编译器又怎么知道Reciver到底是什么呢?Receiver在另一个文件中声明的。 /* * 1.1 version. */ import java.a...
下面是sun的教程中的一段applet,再instanceof那一句中。使用了Receiver。但是这个applet中从来没有声明过这个类啊,编译器又怎么知道Reciver到底是什么呢?Receiver在另一个文件中声明的。
/*
* 1.1 version.
*/
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
import java.util.Enumeration;
public class Sender extends Applet
implements ActionListener {
private String myName;
private TextField nameField;
private TextArea status;
private String newline;
public void init() {
GridBagLayout gridBag = new GridBagLayout();
GridBagConstraints c = new GridBagConstraints();
setLayout(gridBag);
Label receiverLabel = new Label("Receiver name:",
Label.RIGHT);
gridBag.setConstraints(receiverLabel, c);
add(receiverLabel);
nameField = new TextField(getParameter("RECEIVERNAME"),
10);
c.fill = GridBagConstraints.HORIZONTAL;
gridBag.setConstraints(nameField, c);
add(nameField);
nameField.addActionListener(this);
Button button = new Button("Send message");
c.gridwidth = GridBagConstraints.REMAINDER; //end row
c.anchor = GridBagConstraints.WEST; //stick to the
//text field
c.fill = GridBagConstraints.NONE; //keep the button
//small
gridBag.setConstraints(button, c);
add(button);
button.addActionListener(this);
status = new TextArea(5, 60);
status.setEditable(false);
c.anchor = GridBagConstraints.CENTER; //reset to the default
c.fill = GridBagConstraints.BOTH; //make this big
c.weightx = 1.0;
c.weighty = 1.0;
gridBag.setConstraints(status, c);
add(status);
myName = getParameter("NAME");
Label senderLabel = new Label("(My name is " + myName + ".)",
Label.CENTER);
c.weightx = 0.0;
c.weighty = 0.0;
gridBag.setConstraints(senderLabel, c);
add(senderLabel);
newline = System.getProperty("line.separator");
}
public void actionPerformed(ActionEvent event) {
Applet receiver = null;
String receiverName = nameField.getText(); //Get name to
//search for.
receiver = getAppletContext().getApplet(receiverName);
if (receiver != null) {
//Use the instanceof operator to make sure the applet
//we found is a Receiver object.
if (!(receiver instanceof Receiver)) { //问题就在这里
status.append("Found applet named "
+ receiverName + ", "
+ "but it's not a Receiver object."
+ newline);
} else {
status.append("Found applet named "
+ receiverName + newline
+ " Sending message to it."
+ newline);
//Cast the receiver to be a Receiver object
//(instead of just an Applet object) so that the
//compiler will let us call a Receiver method.
((Receiver)receiver).processRequestFrom(myName);
}
} else {
status.append("Couldn't find any applet named "
+ receiverName + "." + newline);
}
}
public Insets getInsets() {
return new Insets(3,3,3,3);
}
public void paint(Graphics g) {
g.drawRect(0, 0,
getSize().width - 1, getSize().height - 1);
}
public String getAppletInfo() {
return "Sender by Kathy Walrath";
}
}
/*
* 1.1 version.
*/
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
import java.util.Enumeration;
public class Sender extends Applet
implements ActionListener {
private String myName;
private TextField nameField;
private TextArea status;
private String newline;
public void init() {
GridBagLayout gridBag = new GridBagLayout();
GridBagConstraints c = new GridBagConstraints();
setLayout(gridBag);
Label receiverLabel = new Label("Receiver name:",
Label.RIGHT);
gridBag.setConstraints(receiverLabel, c);
add(receiverLabel);
nameField = new TextField(getParameter("RECEIVERNAME"),
10);
c.fill = GridBagConstraints.HORIZONTAL;
gridBag.setConstraints(nameField, c);
add(nameField);
nameField.addActionListener(this);
Button button = new Button("Send message");
c.gridwidth = GridBagConstraints.REMAINDER; //end row
c.anchor = GridBagConstraints.WEST; //stick to the
//text field
c.fill = GridBagConstraints.NONE; //keep the button
//small
gridBag.setConstraints(button, c);
add(button);
button.addActionListener(this);
status = new TextArea(5, 60);
status.setEditable(false);
c.anchor = GridBagConstraints.CENTER; //reset to the default
c.fill = GridBagConstraints.BOTH; //make this big
c.weightx = 1.0;
c.weighty = 1.0;
gridBag.setConstraints(status, c);
add(status);
myName = getParameter("NAME");
Label senderLabel = new Label("(My name is " + myName + ".)",
Label.CENTER);
c.weightx = 0.0;
c.weighty = 0.0;
gridBag.setConstraints(senderLabel, c);
add(senderLabel);
newline = System.getProperty("line.separator");
}
public void actionPerformed(ActionEvent event) {
Applet receiver = null;
String receiverName = nameField.getText(); //Get name to
//search for.
receiver = getAppletContext().getApplet(receiverName);
if (receiver != null) {
//Use the instanceof operator to make sure the applet
//we found is a Receiver object.
if (!(receiver instanceof Receiver)) { //问题就在这里
status.append("Found applet named "
+ receiverName + ", "
+ "but it's not a Receiver object."
+ newline);
} else {
status.append("Found applet named "
+ receiverName + newline
+ " Sending message to it."
+ newline);
//Cast the receiver to be a Receiver object
//(instead of just an Applet object) so that the
//compiler will let us call a Receiver method.
((Receiver)receiver).processRequestFrom(myName);
}
} else {
status.append("Couldn't find any applet named "
+ receiverName + "." + newline);
}
}
public Insets getInsets() {
return new Insets(3,3,3,3);
}
public void paint(Graphics g) {
g.drawRect(0, 0,
getSize().width - 1, getSize().height - 1);
}
public String getAppletInfo() {
return "Sender by Kathy Walrath";
}
}
|
只要你有Receiver.class并且在classpath设置过的目录里,编译器当然就可以找到啦。
|
因为它们都在同一个包(包为空)里,同一个包的内容是不用引入的
您可能感兴趣的文章:
本站(WWW.)旨在分享和传播互联网科技相关的资讯和技术,将尽最大努力为读者提供更好的信息聚合和浏览方式。
本站(WWW.)站内文章除注明原创外,均为转载、整理或搜集自网络。欢迎任何形式的转载,转载请注明出处。
本站(WWW.)站内文章除注明原创外,均为转载、整理或搜集自网络。欢迎任何形式的转载,转载请注明出处。