【发布时间】:2020-02-09 08:14:18
【问题描述】:
import java.util.*;
class A {
protected int n;
public A(int a) {
n = a;
}
protected int disp() {
return n;
}
}
class B extends A {
// what should i do here
}
public class Hello {
public static void main(String[] args) {
//Your Code Here
int a =5;
A obj= new B(a);
System.out.print(obj.disp());
}
}
【问题讨论】:
-
public B(A a)?如果您真的希望B保存其父类的实例,您还应该仔细考虑,这通常是不必要的。 -
因为你为了
extend class A,你必须理想地初始化Object A所以创建一个调用new B(int a){ this.super(a);}的构造函数 -
@SiddarthSreeni
this是多余的。super(a)就足够了
标签: java inheritance constructor