Java类继承以及接口实现实例
Java类继承以及接口实现实例:这个实例非常容易理解,贴在这里与大家共享。
1 //抽象父类,交通工具(Vehicle) 2 public abstract class Vehicle{ 3 private String name; 4 private double cost; 5 6 //无参构造函数 7 public Vehicle(){ 8 } 9 10 //有参构造函数 11 public Vehicle(String name, double cost){ 12 this.name = name; 13 this.cost = cost; 14 } 15 16 //父类的抽象方法,在子类中要具体实现。 17 public abstract void run(); 18 public abstract void stop(); 19 20 //getter方法 21 public String getName(){ 22 return this.name; 23 } 24 25 public double getCost(){ 26 return this.cost; 27 } 28 29 //setter方法 30 public void setName(String name){ 31 this.name = name; 32 } 33 34 public void setCost (double cost){ 35 this.cost = cost; 36 } 37 } 38 39 40 41 //上缴养路费的接口 42 public interface interRoadTax{ 43 public void tax(); 44 } 45 46 47 48 //子类自行车Bike,继承Vehicle 49 public class Bike extends Vehicle{ 50 private int bikeType; 51 52 //无参构造函数 53 public Bike(){} 54 55 //有参构造函数 56 public Bike(String name, double cost, int type){ 57 super(name,cost); //通过父类方法进行赋值 58 this.bikeType = type; 59 } 60 61 //setter方法 62 public void setBikeType(int type){ 63 this.bikeType = type; 64 } 65 66 //getter方法 67 public int getBikeType(){ 68 return this.bikeType; 69 } 70 71 //继承父类Vehicle的抽象方法,在子类中要具体实现。 72 public void run(){ 73 System.out.println("i am bike and running....."); 74 } 75 76 //继承父类Vehicle的抽象方法,在子类中要具体实现。 77 public void stop(){ 78 System.out.println("i am bike and stopping....."); 79 } 80 } 81 82 83 84 //子类汽车Car,继承Vehicle,同时实现interRoadTax接口 85 public class Car extends Vehicle implements interRoadTax{ 86 private int numberOfPeople; 87 88 //无参构造函数 89 public Car(){} 90 91 //有参构造函数 92 public Car(String name, double cost, int numberOfPeople){ 93 super(name,cost);//通过父类方法进行赋值 94 this.numberOfPeople = numberOfPeople; 95 } 96 97 //getter方法 98 public int getNumberOfPeople(){ 99 return this.numberOfPeople; 100 } 101 102 public void setNumberOfPeople(int numberOfPeople){ 103 this.numberOfPeople = numberOfPeople; 104 } 105 106 //继承父类Vehicle的抽象方法,在子类中要具体实现。 107 public void run(){ 108 System.out.println("i am car and running....."); 109 } 110 111 //继承父类Vehicle的抽象方法,在子类中要具体实现。 112 public void stop(){ 113 System.out.println("i am car and stopping....."); 114 } 115 116 //实现上缴养路费的接口方法 117 public void tax(){ 118 System.out.println("the car tax is " + getCost() * 0.1); 119 } 120 } 121 122 123 124 //子类摩托车Motor,继承Vehicle,同时实现interRoadTax接口 125 public class Motor extends Vehicle implements interRoadTax{ 126 public Motor(){} 127 public Motor(String name,double cost){ 128 super(name,cost); 129 } 130 131 public void run(){ 132 System.out.println("i am Motor and running....."); 133 } 134 135 public void stop(){ 136 System.out.println("i am Motor and stopping....."); 137 } 138 139 public void tax(){ 140 System.out.println("the motor tax is " + getCost() * 0.05); 141 } 142 } 143 144 145 146 //交通工具的主人 147 public class User{ 148 private int id; 149 private String name; 150 151 private Vehicle v; 152 153 public User(){} 154 155 public User(int _id,String _name){ 156 id = _id; 157 name = _name; 158 } 159 160 public void print(){ 161 System.out.println("the user is "+ id +" - "+ name); 162 } 163 164 public void setV(Vehicle v){ 165 this.v = v; 166 } 167 168 public Vehicle getV(){ 169 return this.v; 170 } 171 } 172 173 174 //测试类 175 public class UserTest{ 176 public static void main(String[] args){ 177 178 User[] users = new User[3]; 179 180 Vehicle v1 = new Bike("fenghuang", 500.00, 1); 181 Vehicle v2 = new Motor("honda", 10000.00); 182 Vehicle v3 = new Car("polo", 145678.00, 5); 183 184 users[0] = new User(1,"yanming"); 185 users[0].setV(v1); 186 users[1] = new User(2,"laoxia"); 187 users[1].setV(v2); 188 users[2] = new User(3,"zhaomengran"); 189 users[2].setV(v3); 190 191 192 System.out.println("=====to begin test class====="); 193 users[0].print(); 194 users[0].getV().run(); 195 users[1].print(); 196 users[1].getV().run(); 197 users[2].print(); 198 users[2].getV().run(); 199 200 201 System.out.println("=====to hand up Road Tax====="); 202 for(User u : users){ 203 Vehicle v = u.getV(); 204 if(v instanceof Car){ 205 Car c = (Car)v; 206 c.tax(); 207 } 208 209 if(v instanceof Motor){ 210 Motor m = (Motor)v; 211 m.tax(); 212 } 213 214 if(v instanceof Bike){ 215 System.out.println(" hah ,no tax"); 216 } 217 } 218 } 219 }