1、代表自身对象
package GUI;
import java.awt.*;
import java.awt.event.*;
import java.awt.event.ActionListener;
/**
* Created by ADY on 2016/11/17.
*/
public class TFMath {
public static void main(String[] args){
new TMFrame().launchFrame();
}
}
class TMFrame extends Frame{
TextField num1,num2,num3;
public void launchFrame(){
num1 = new TextField(10);
num2 = new TextField(10);
num3 = new TextField(15);
// TextField test = new TextField(2);
Label lbplus = new Label("+");
Button btequal = new Button("=");
//this是当前类,当前对象。(既是TMFrame,包含的是成员变量num1、num2、num3)
btequal.addActionListener(new TFMonitor(this));
setLayout(new FlowLayout());
add(num1);
add(lbplus);
add(num2);
add(btequal);
add(num3);
pack();
setVisible(true);
}
}
class TFMonitor implements ActionListener{
TMFrame tf = null;
public TFMonitor(TMFrame tf){
this.tf = tf;
}
public void actionPerformed(ActionEvent e){
int n1 = Integer.parseInt(tf.num1.getText());
int n2 = Integer.parseInt(tf.num2.getText());
// " " 是一个字符串,字符串+int自动转成字符串
tf.num3.setText("" + (n1 + n2));
//int n3 = Integer.parseInt(tf.test.get)
//tf.num1.setText("");
//tf.num2.setText("");
}
}
2、引用构造方法
class student extends person{
private String school;
student(String name,String school){
//super(name,location);
this(name,"beijing",school); /**有显式this()调用的构造器就会抑制掉该构造器里隐式的super()调用。注意this()和
this.不一样。
this()调用本类中另一种构造方法,this()必须是构造方法的第一句
super()调用基类的一种构造方法,super()必须是构造方法的第一句
this:表示当前对象名
super:表示引用当前对象父类中的成员
*/
}
student(String n,String l,String school){
super(n,l);
this.school = school;
}
}