网站的标题符号,asp网站开发软件,甘肃网站排名公司,建网站需要哪些步骤1、运行TextInherists.java 示例#xff0c;观察输出#xff0c;注意总结父类与子类之间构造方法的的调用关系#xff0c;修改parent构造方法的代码#xff0c;显式调用grandparent另一个构造函数。 源代码#xff1a; class GrandParent{
public GrandParent(){
System.o…1、运行TextInherists.java 示例观察输出注意总结父类与子类之间构造方法的的调用关系修改parent构造方法的代码显式调用grandparent另一个构造函数。 源代码 class GrandParent{
public GrandParent(){
System.out.println(GrandParent Created);
}public GrandParent(String string){
System.out.println(GrandParent Createdstring);
}
}
class Parent extends GrandParent{
public Parent(){
super(123);//调用父类有参数的构造函数
System.out.println(Parent Created);
}
}
class Child extends Parent{
public static void main(String[] args){
Child cnew Child();
}public Child(){
System.out.println(Child Created);
}
} 运行结果截图 修改后的截图 通过super调用父类构造方法必须在子类构造方法的第一句。 2、为什么子类的构造方法在运行之前必须调用父类的构造方法能不能反过来为什么不能反过来 构造函数的作用是初始化对象类继承了父类就默认含有父类的公共成员方法和公共成员变量这些不再子类中重复声明。而构造方法则相当于把父类给实例化出来 如果子类实例化的时候不调用父类的构造方法相当于子类压根就没有父亲。不能反过来。 3、在子类中如果调用父类被覆盖的方法可以使用super关键字。 class Friut{
public void show(){
System.out.println(Fruit!);
}
}
class Apple extends Friut{public void show(){
System.out.println(Apple!);
super.show();
}
}public class fangFaSuper{
public static void main(String[] args){
Apple anew Apple();
a.show();
}
} 运行结果截图 转载于:https://www.cnblogs.com/jingxiaopu/p/6049076.html