父类 package com.demon; public class Traffic { public String name; public String way; public int num; public void method(){ } }
子类 package com.demon; public class Bus extends Traffic { public String name="巴士"; public String way="陆地"; public int num=40; public void method(){ System.out.println(name+"在"+way+"上行走,"+"可以载"+num+"个人"); } }
测试页面
package com.demon; public class Initail { public static void main(String[] args) { Traffic plane=new Plane(); plane.method(); Traffic bus=new Bus(); bus.method(); Traffic boat=new Boat(); boat.method(); } }
其实在测试页面还可以简化 一下的,如 把父类的方法定义成静态方法,在测试页面,直接通过类名调用也可以,简化一点而且
还有两个子类
package com.demon; public class Plane extends Traffic { public String name="飞机"; public String way="天空"; public int num=20; public void method(){ System.out.println(name+"在"+way+"上行走,"+"可以载"+num+"个人"); } } ------------------------------------------------------------- package com.demon; public class Boat extends Traffic { public String name="轮船"; public String way="水上"; public int num=10; public void method(){ System.out.println(name+"在"+way+"上行走,"+"可以载"+num+"个人"); } }
无法编译
还有两个子类呢?发出来学习学习!