super的作用和细节
class A
{
public int i;
private int j,a,b,c,d,e; // 如何A函数中100个属性 并在A中的构造函数中完成赋值
public A(int i)
{
this.i = i;
}
}
class B extends A
{
// 而继承函数也需要这100个属性 并且赋值。 super 就可以在B中调用A中的构造函数 并第一个就是super()
public B()
{
//super(); // 错误 当你B中构造函数不写super时,会自动第一行是 super(); 而A中没有无参数的构造函数
super(22);
//super(11); // 错误 super只可以放到构造函数的开头第一句
}
}
public class super的作用和细节 {
public static void main (String[] args)
{
B bb1 = new B();
System.out.printf("%d", bb1.i);
}
}
/*ps
1, 每个子类构造函数中第一句,都是隐含的调用super();,如果父类没有对应的构造函数将会报错
2,如果显示出super()将一定要是第一条语句,不然将报错
*/