第七章章节练习
1.创建子类对象时,先执行父类构造方法,输出"Baise",
再执行子类构造方法,输出"Child"
2.
1 package com.kgc.zzlx; 2 3 //测试 4 public class Zj7024 { 5 public static void main(String[] args) { 6 7 8 Zj7023 print = new Zj7022(); 9 print.printInfo(); 10 Zj7023 print1 = new Zj7021(); 11 print1.printInfo(); 12 } 13 }
1 package com.kgc.zzlx; 2 //动物 3 public class Zj7023 { 4 String name; 5 6 public void printInfo(){ 7 8 } 9 }
1 package com.kgc.zzlx; 2 //鸟 3 public class Zj7022 extends Zj7023 { 4 5 public void printInfo(){ 6 System.out.println("我是一只红色的鸟!"); 7 System.out.println("我今年4岁了!"); 8 } 9 }
1 package com.kgc.zzlx; 2 //鱼 3 public class Zj7021 extends Zj7023 { 4 public void printInfo(){ 5 System.out.println("我是一只5斤重的鱼!"); 6 System.out.println("我今年2岁了!"); 7 } 8 9 }
运行结果
3
1 package com.kgc.zzlx; 2 //测试 3 public class Zj7035 { 4 public static void main(String[] args){ 5 Zj7034 zj3=new Zj7033(); 6 zj3.call(); 7 Zj7034 zj2=new Zj7032(); 8 zj2.call(); 9 Zj7034 zj1=new Zj7031(); 10 zj1.call(); 11 } 12 }
1 package com.kgc.zzlx; 2 //手机类 3 public class Zj7034 { 4 5 public void call(){ 6 System.out.println("打电话"); 7 } 8 }
1 package com.kgc.zzlx; 2 //wphone类 3 public class Zj7033 extends Zj7034 { 4 5 6 public void call(){ 7 System.out.println("使用wphone打电话"); 8 } 9 }
1 package com.kgc.zzlx; 2 //安卓类 3 public class Zj7032 extends Zj7034{ 4 5 6 public void call(){ 7 System.out.println("使用安卓打电话"); 8 } 9 }
1 package com.kgc.zzlx; 2 //iphone类 3 public class Zj7031 extends Zj7034{ 4 5 6 public void call(){ 7 System.out.println("iphone打电话"); 8 } 9 }