class TestConstructureSuquenceZi extends TestConstructureSuquenceFu {
int parameter;
String member;
public void manner() {
System.out.println("子类方法——执行");
this.parameter = 2;
System.out.println(this.parameter + " + " + super.parameter);
}
public static void main(String[] args) {
System.out.println("子类主运行程序运行");
System.out.println("=======================");
TestConstructureSuquenceFu fu = new TestConstructureSuquenceFu();
System.out.println("fu的值为:" + fu);
System.out.println("=======================");
TestConstructureSuquenceZi zi = new TestConstructureSuquenceZi();
System.out.println("zi的值为:" + zi);
System.out.println("=======================");
fu.manner();
System.out.println(fu); // 地址与前值相同
System.out.println(fu.parameter); // 值为8
zi.manner();
}
}
class TestConstructureSuquenceFu {
int parameter;
public void manner() {
System.out.println("父类方法——执行");
this.parameter = 8;
System.out.println(parameter);
}
public static void main(String[] args) {
System.out.println("父类主运行程序运行");
TestConstructureSuquenceFu fu = new TestConstructureSuquenceFu();
System.out.println("=======================");
System.out.println(fu.parameter);
fu.manner();
}
}
以第一个程序为入口点,程序执行结果是
子类主运行程序运行
=======================
fu的值为:TestConstructureSuquenceFu@3af49f1c
=======================
zi的值为:TestConstructureSuquenceZi@19469ea2
=======================
父类方法——执行
8
TestConstructureSuquenceFu@3af49f1c
8
子类方法——执行
2 + 0
你有两个对象 fu 和 zi,每个对象的变量都是独立存在
的。
调用 fu.manner,修改了 fu 对象的 parameter
调用 zi.manner,修改了 zi 对象的 parameter(parameter是TestConstructureSuquenceZi的成员,这个成员隐藏了基类的同名的成员)
调用TestConstructureSuquenceZi的manner并不会调用基类的manner
如果你希望0变成8,有几种修改方式(任意选一个)
(1)
public void manner() {
System.out.println("子类方法——执行");
this.parameter = 2;
super.parameter = 8;
System.out.println(this.parameter + " + " + super.parameter);
}
(2)
public void manner() {
System.out.println("子类方法——执行");
this.parameter = 2;
super.manner();
System.out.println(this.parameter + " + " + super.parameter);
}
(3)
class TestConstructureSuquenceZi extends TestConstructureSuquenceFu {
static int parameter;
static String member;
public void manner() {
System.out.println("子类方法——执行");
this.parameter = 2;
System.out.println(this.parameter + " + " + super.parameter);
}
public static void main(String[] args) {
System.out.println("子类主运行程序运行");
System.out.println("=======================");
TestConstructureSuquenceFu fu = new TestConstructureSuquenceFu();
System.out.println("fu的值为:" + fu);
System.out.println("=======================");
TestConstructureSuquenceZi zi = new TestConstructureSuquenceZi();
System.out.println("zi的值为:" + zi);
System.out.println("=======================");
fu.manner();
System.out.println(fu); // 地址与前值相同
System.out.println(fu.parameter); // 值为8
zi.manner();
}
}
class TestConstructureSuquenceFu {
static int parameter;
public void manner() {
System.out.println("父类方法——执行");
this.parameter = 8;
System.out.println(parameter);
}
public static void main(String[] args) {
System.out.println("父类主运行程序运行");
TestConstructureSuquenceFu fu = new TestConstructureSuquenceFu();
System.out.println("=======================");
System.out.println(fu.parameter);
fu.manner();
}
}