慕仙6447988
2017-12-05 17:42
//父类 package com.imooc; public class FuLei { public String name; public int person; public String style; public void act() { System.out.println("名字="+name+" "+"人数="+person+" "+"类型="+style); } } //子类自行车 public class Bicycle extends FuLei { public void act() { super.name="自行车"; super.person=2; super.style="陆运"; super.act(); } } //子类汽车 public class Car extends FuLei { public void act() { super.name="小汽车"; super.person=5; super.style="陆运"; super.act(); } } //子类火车 public class Train extends FuLei { public void act() { super.name="火车"; super.person=2000; super.style="陆运"; super.act(); } } //子类轮船 public class stea extends FuLei{ public void act() { super.name="轮船"; super.person=20000; super.style="海运"; super.act(); } } //子类飞机 public class aircraft extends FuLei { public void act() { super.name="飞机"; super.person=500; super.style="空运"; super.act(); } } //测试类 public class Demo { public static void main(String [] sss) { FuLei obj1= new Bicycle(); obj1.act(); FuLei obj2= new Car(); obj2.act(); FuLei obj3= new Train(); obj3.act(); FuLei obj4= new stea(); obj4.act(); FuLei obj5= new aircraft(); obj5.act(); } }
package com.extends1; public class Vehicle { public String name; public String way; public int count; public Vehicle(String name,String way,int count){ this.name=name;this.way=way;this.count=count; System.out.println(name+"能"+way+"运输"+count+"人"); } }
package com.extends1; public class Steamer extends Vehicle { public Steamer(String name, String way, int count) { super(name, way, count); } }
package com.extends1; public class Plane extends Vehicle { public Plane(String name, String way, int count) { super(name, way, count); } }
package com.extends1; public class Bus extends Vehicle { public Bus(String name, String way, int count) { super(name, way, count); } }
package com.extends1; public class Test { /** * @param args * 多态问题的实现 * 引用的多态 * 方法的多态 */ public static void main(String[] args) { Vehicle bus=new Bus("公共汽车","陆上", 50); Vehicle stea=new Steamer("轮船","海上",200); Vehicle plane=new Plane("飞机","空中",1000);
你在做什么哟,你只是创建了几个继承类,然后输出而已,都没有满足客户的需要,客户要怎么选择,选那辆车?多少辆车?价格是多少?.......等等问题,你只是把有这几种类型的车展示出来而已。
Java入门第二季 升级版
530670 学习 · 6091 问题
相似问题