this表示当前对象,super表示当前对象类的父类。
例:
class Employee{
String name;
String jobid;
double wage;
//局部变量与成员变量重名的时候就要用到this.
Employee(String name,String jobid,double wage){
this.name=name;
this.jobid=jobid;
this.wage=wage;
}
public void work(){
System.out.println(this.name+","+this.jobid+","+this.wage);
System.out.println("工作");
}
}
class Manager extends Employee{
double reward;
Manager(String name,String jobid,double wage,double reward){
super(name,jobid,wage); //由于存在继承关系,这时就可以用super调用父类的构造方法进行初始化,如果不写,默认是super(); 注:这时super只能写在第一行。
this.reward=reward; //reward是子类特有的字段。
}
public void work(){
System.out.println(this.name+","+this.jobid+","+this.wage+","+this.reward);
System.out.println("工作");
}
}
public class Test1{
public static void main(String args[]){
Employee em=new Employee("普通员工","123",4500);
em.work();
Manager m=new Manager("经理","234",8200,1800);
m.work();
}
} |