package jicheng;
public class jiaotong {
String name;
int num;
String way;
public void showDate(){
System.out.println(name+"在"+way+"上能载客"+num+"人");
}
}
父类
package jicheng;
public class car extends jiaotong {
public car(String newName,String newWay,int newNum){
super.name=newName;
super.way=newWay;
super.num=newNum;
}
}
汽车子类
package jicheng;
public class plane extends jiaotong {
public plane(String newName,String newWay,int newNum){
super.name=newName;
super.way=newWay;
super.num=newNum;
}
}
飞机子类
package jicheng;
public class ship extends jiaotong {
public ship(String newName,String newWay,int newNum){
super.name=newName;
super.way=newWay;
super.num=newNum;
}
}
轮船子类
package jicheng;
public class initial {
public static void main(String[] args) {
// TODO Auto-generated method stub
jiaotong car=new car("汽车","陆地",4);
jiaotong plane=new plane("飞机","空中",30);
jiaotong ship=new ship("轮船","海上",50);
car.showDate();
plane.showDate();
ship.showDate();
}
}
测试类
可以用单粒设计模式
弄错了,单粒设计模式用不了,会使它失去多态性
既然是下面的每一个类都继承交通类,通过父类句柄new子类对象,实际上父类的可以写成abstract,子类重写父类方法,不用super,用this。另外可以参照设计模式的单粒,只要new一个父类句柄就可以调用所有子类了