黑马程序员技术交流社区
标题:
求super引用
[打印本页]
作者:
∑づ東〗
时间:
2012-5-20 20:59
标题:
求super引用
abstract class Employee
{
//姓名
String name;
//职位
String position;
//地址
String address;
Employee(){}
//用于显示属性
abstract void show();
}
class Manager extends Employee
{
//部门
String department;
Manager(){}
Manager(String name,String position,String address,String department)
{
this.name = name;
this.position = position;
this.address = address;
this.department = department;
}
void show()
{
System.out.println(name+" "+position+" "+address+" "+department);
}
}
class Director extends Employee
{
//补助
double transportAllowance;
Director(){}
Director(String name,String position,String address,double transportAllowance)
{
this.name = name;
this.position = position;
this.address = address;
this.transportAllowance = transportAllowance;
//听 说这里可以用super();代替。但是这个super()到底是怎么来的?怎么代入进去的?求解!
}
void show()
{
System.out.println(name+" "+position+" "+address+" "+transportAllowance);
}
}
class AbstractTest
{
public static void main(String[] args)
{
//抽象类不能够实例化
//Employee e = new Employee();
Manager m = new Manager("黄岩岛","经理","南海","外交部");
m.show();
Director d = new Director("钓鱼岛","程序员","东海",123456789.0);
d.show();
}
}
作者:
8161776
时间:
2012-5-20 21:12
本帖最后由 杨尧 于 2012-5-20 21:13 编辑
Director(String name,String position,String address,double transportAllowance)
{
this.name = name;
this.position = position;
this.address = address;
this.transportAllowance = transportAllowance;
//听 说这里可以用super();代替。但是这个super()到底是怎么来的?怎么代入进去的?求解!
//super代替什么?代替上面的代码吗?肯定不能呀,super()是掉用父类的构造方法,这个父类的构造方法是空的,
}
复制代码
作者:
余宏
时间:
2012-5-20 21:31
super调用的是父类的构造方法,你父类的构造函数什么都没有,没有意义。
super的使用:直接super就是了,当然也可以往里面传入参数。
作者:
niceBoy
时间:
2012-5-20 21:54
Director(String name,String position,String address,double transportAllowance)
{
this.name = name;
this.position = position;
this.address = address;
this.transportAllowance = transportAllowance;
//听 说这里可以用super();代替。但是这个super()到底是怎么来的?怎么代入进去的?求解!
}
完全没希望,你听错了吧,应该是构造函数第一行有个隐式的super()把
作者:
胥江
时间:
2012-5-20 21:58
super即是父类的引用,
super();即是引用父类无参构造函数。
super(参数);即是引用带参构造方法
一般用给本类进行初始化
欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/)
黑马程序员IT技术论坛 X3.2