//抽象父类,交通工具(Vehicle) public abstract class Vehicle{ private String name; private double cost; public Vehicle(){ } public Vehicle(String name ,double cost){ this.name = name; this.cost = cost; } public abstract void run(); public abstract void stop(); public String getName(){ return this.name; } public double getCost(){ return this.cost; } public void setName(String name){ this.name = name; } public void setCost (double cost){ this.cost = cost; } } //上缴养路费的接口 public interface RoadTax{ public void tax(); } //子类自行车Bike public class Bike extends Vehicle{ private int bikeType; public Bike(){} public Bike(String name,double cost,int type){ super(name,cost); // this.name = name; // this.cost = cost; this.bikeType = type; } public void setBikeType(int type){ this.bikeType = type; } public int getBikeType(){ return this.bikeType; } public void run(){ System.out.println("i am bike and running....."); } public void stop(){ System.out.println("i am bike and stopping....."); } } //子类汽车Car public class Car extends Vehicle implements RoadTax{ private int segment; public Car(){} public Car(String name,double cost,int seg){ super(name,cost); this.segment = seg; } public int getSegment(){ return this.segment; } public void setSegment(int seg){ this.segment = seg; } public void run(){ System.out.println("i am car and running....."); } public void stop(){ System.out.println("i am car and stopping....."); } public void tax(){ System.out.println("the car tax is " + getCost() * 0.1); } } //子类摩托车Motor public class Motor extends Vehicle implements RoadTax{ public Motor(){} public Motor(String name,double cost){ super(name,cost); } public void run(){ System.out.println("i am Motor and running....."); } public void stop(){ System.out.println("i am Motor and stopping....."); } public void tax(){ System.out.println("the motor tax is " + getCost() * 0.05); } } //交通工具的主人 public class User{ private int id; private String name; private Vehicle v; public User(){} public User(int _id,String _name){ id = _id; name = _name; } public void print(){ System.out.println("the user is "+ id +" - "+ name); } public void setV(Vehicle v){ this.v = v; } public Vehicle getV(){ return this.v; } } //测试类 public class UserTest{ public static void main(String[] args){ User[] users = new User[3]; Vehicle v1 = new Bike("changjiu",500.00,1); Vehicle v2 = new Motor("honda",10000.00); Vehicle v3 = new Car("polo",145678.00,2); User ym = new User(1,"yanming"); ym.setV(v1); User lx = new User(2,"laoxia"); lx.setV(v2); User zmr = new User(3,"zhaomengran"); zmr.setV(v3); users[0] = ym; users[1] = zmr; users[2] = lx; System.out.println("========to begin test class====="); ym.print(); ym.getV().run(); zmr.print(); zmr.getV().run(); lx.print(); lx.getV().run(); System.out.println("========to hand up Road Tax====="); // for(int i =0;i <users.length;i++){ // } for(User u : users){ Vehicle v = u.getV(); if(v instanceof Car){ Car c = (Car)v; c.tax(); } if(v instanceof Motor){ Motor m = (Motor)v; m.tax(); } if(v instanceof Bike){ System.out.println(" hah ,no tax"); } } } } 更多信息见www.persist.net.cn