class BaseClass{
public String name;
public int num;
BaseClass(String name,int num){
this.name=name;
this.num=num;
System.out.println("BaseClass:"+"name "+name+" num "+num);
} }
class SubClass extends BaseClass{ public String name;
public int num;
private String color="Green";
SubClass(String name,int num,String color){
super(name,num);
this.color=color;//注释掉不影响输出结果
System.out.println("SubClass:"+"name "+name+" num "+num+" color "+color);
} }
public class Demo{ public static void main(String[] args)
{ SubClass sc=new SubClass("B",5,"red");
SubClass sc2=new SubClass("B2",6,"Yello");
BaseClass bc=new BaseClass("C",2);
} }
简单的介绍:定义上层类BaseClass 下层类SubClass继承了上层类,在SubClass中通过super调用了BaseClass的构造方法,但是如果未对SubClass类中的color特有属性设置(代码中注释处语句去掉),当创建SubClass对象时并传入值,输出的结果一样?不明白为什么…… |