Java中构造函数,getset和this的用法?

public class Abstract {

public static void main(String[] args) {
Employee employee = new Employee("张三", 100);
employee.job();
}

}

abstract class Company {
private String name;
private double money;

public Company(String name, double money) {
this.name = name;
this.money = money;

}

public abstract void job();

public void setName(String name) {
this.name = name;
}

public String getName() {
return name;
}

public void setMoney(double money) {
this.money = money;
}

public double getMonry() {
return money;
}
}

class Employee extends Company {
public Employee(String name, double money) {
super(name, money);
}

public void job() {
System.out.println(super.getName() + super.getMonry() + "敲代码");
}
}

class Manager extends Company {
private double bonus;

public Manager(String name, double money, double bonus) {
super(name, money);
this.bonus = bonus;
}

public void job() {

System.out.println(super.getName() + super.getMonry() + " "
+ this.bonus + "指挥敲代码");
}
}

class Clear extends Company {
public Clear(String name, double money) {
super(name, money);
}

public void job() {
System.out.println(super.getName() + super.getMonry() + "清理垃圾");
}
}

在new子类的时候将 参数 传入子类的构造函数里,再通过super() 传给父类构造函数,然后下面name和money是怎么在父类里面传递的,是怎么通过子类的覆盖父类的方法job() {super.getName() + super.getMonry()}最后打印输出的?
父类的构造方法里的this.name和setName里的this.name,各自的作用是什么,到底谁给谁啊?

this.name是当前对象name属性值;
setName里的this.name是当有其他的地方调用setName方法时,将传入的名字赋值给当前对象
温馨提示:答案为网友推荐,仅供参考
第1个回答  2014-10-29
父类构造方法调用时,当前对象指的是this
传的参数赋值给this的属性
第2个回答  2014-10-29
this.name 是你定义的父类里面private String name初始化的 name 属性。
相似回答