父类traffic
package cn.edu.aufe.trafficdemol;
public class Traffic {
public int useage;
public String usename;
public Traffic(){
useage=10;
usename="xiaoming";
}
public void traffic(){
System.out.println("交通工具具有运输的功能。其使用者为"+usename+";使用年限为"+useage);
}
}
子类1:Car类
package cn.edu.aufe.trafficdemol;
public class Car extends Traffic {
public void traffic(){
System.out.println("Car在陆地上运输。其使用者为"+usename+";使用年限为"+useage);
}
}
子类2:Ship类
package cn.edu.aufe.trafficdemol;
public class Ship extends Traffic {
public void traffic(){
System.out.println("Ship在海洋上运输。其使用者为"+usename+";使用年限为"+useage);
}
}
子类3:AIplane类
package cn.edu.aufe.trafficdemol;
public class Airplane extends Traffic {
public void traffic(){
System.out.println("Airplane在天空中运输。其使用者为"+usename+";使用年限为"+useage);
}
}
主函数:
package cn.edu.aufe.trafficdemol;
public class Initial {
public static void main(String[] args) {
// TODO Auto-generated method stub
Traffic tf=new Traffic();
tf.traffic();
Traffic car=new Car();
car.useage=20;
car.usename="benyl";
car.traffic();
Ship ship=new Ship();
ship.useage=40;
ship.traffic();
Traffic airplane=new Airplane();
airplane.usename="alexwang";
airplane.traffic();
}
}