>
學(xué)校機(jī)構(gòu) >
廣西南寧達(dá)內(nèi)軟件科技有限公司 >
學(xué)習(xí)資訊>
Java類繼承以及接口實(shí)現(xiàn)實(shí)例
Java類繼承以及接口實(shí)現(xiàn)實(shí)例
73 2017-04-14
南寧達(dá)內(nèi):Java類繼承以及接口實(shí)現(xiàn)實(shí)例
Java類繼承以及接口實(shí)現(xiàn)實(shí)例:這個(gè)實(shí)例非常容易理解,貼在這里與大家共享。
1//抽象父類,交通工具(Vehicle)2publicabstractclassVehicle{3
privateStringname;4
privatedoublecost;5
6
//無參構(gòu)造函數(shù)
7
publicVehicle(){8
}910
//有參構(gòu)造函數(shù)11
publicVehicle(Stringname,doublecost){12
this.name=name;13
this.cost=cost;14
}1516
//父類的抽象方法,在子類中要具體實(shí)現(xiàn)。
17
publicabstractvoidrun();
18
publicabstractvoidstop();1920
//getter方法
21
publicStringgetName(){22
returnthis.name;23
}2425
publicdoublegetCost(){26
returnthis.cost;27
}2829
//setter方法30
publicvoidsetName(Stringname){31
this.name=name;32
}3334
publicvoidsetCost(doublecost){35
this.cost=cost;36
}
37}38394041//上繳養(yǎng)路費(fèi)的接口42publicinterfaceinterRoadTax{43
publicvoidtax();44}45464748//子類自行車Bike,繼承Vehicle49publicclassBikeextendsVehicle{50
privateintbikeType;5152
//無參構(gòu)造函數(shù)53
publicBike(){}54
55
//有參構(gòu)造函數(shù)56
publicBike(Stringname,doublecost,inttype){57
super(name,cost);//通過父類方法進(jìn)行賦值58
this.bikeType=type;59
}6061
//setter方法
62
publicvoidsetBikeType(inttype){63
this.bikeType=type;64
}65
66
//getter方法67
publicintgetBikeType(){68
returnthis.bikeType;69
}70
71
//繼承父類Vehicle的抽象方法,在子類中要具體實(shí)現(xiàn)。72
publicvoidrun(){73
System.out.println("iambikeandrunning……");74
}7576
//繼承父類Vehicle的抽象方法,在子類中要具體實(shí)現(xiàn)。
77
publicvoidstop(){78
System.out.println("iambikeandstopping……");79
}80}81828384//子類汽車Car,繼承Vehicle,同時(shí)實(shí)現(xiàn)interRoadTax接口85publicclassCarextendsVehicleimplementsinterRoadTax{86
privateintnumberOfPeople;87
88
//無參構(gòu)造函數(shù)89
publicCar(){}90
91
//有參構(gòu)造函數(shù)92
publicCar(Stringname,doublecost,intnumberOfPeople){93
super(name,cost);//通過父類方法進(jìn)行賦值94
this.numberOfPeople=numberOfPeople;95
}96
97
//getter方法98
publicintgetNumberOfPeople(){99
returnthis.numberOfPeople;100
}101
102
publicvoidsetNumberOfPeople(intnumberOfPeople){103
this.numberOfPeople=numberOfPeople;104
}105
106
//繼承父類Vehicle的抽象方法,在子類中要具體實(shí)現(xiàn)。107
publicvoidrun(){108
System.out.println("iamcarandrunning……");109
}110111
//繼承父類Vehicle的抽象方法,在子類中要具體實(shí)現(xiàn)。112publicvoidstop(){113
System.out.println("iamcarandstopping……");114
}115116
//實(shí)現(xiàn)上繳養(yǎng)路費(fèi)的接口方法
117
publicvoidtax(){118
System.out.println("thecartaxis"+getCost()*0.1);119
}
120}121122123124//子類摩托車Motor,繼承Vehicle,同時(shí)實(shí)現(xiàn)interRoadTax接口125publicclassMotorextendsVehicleimplementsinterRoadTax{126
publicMotor(){}127
publicMotor(Stringname,doublecost){128
super(name,cost);129
}130
131
publicvoidrun(){132
System.out.println("iamMotorandrunning……");133
}134135
publicvoidstop(){136
System.out.println("iamMotorandstopping……");137
}138
139
publicvoidtax(){140
System.out.println("themotortaxis"+getCost()*0.05);141
}
142}143144145146//交通工具的主人147publicclassUser{148
privateintid;149
privateStringname;150151
privateVehiclev;152
153
publicUser(){}154155
publicUser(int_id,String_name){156
id=_id;157
name=_name;158
}159
160
publicvoidprint(){161
System.out.println("theuseris"+id+"-"+name);162
}163
164
publicvoidsetV(Vehiclev){165
this.v=v;166
}167
168
publicVehiclegetV(){169
returnthis.v;170
}171}172173174//測(cè)試類175publicclassUserTest{176
publicstaticvoidmain(String[]args){177
178
User[]users=newUser[3];179180
Vehiclev1=newBike("fenghuang",500.00,1);181
Vehiclev2=newMotor("honda",10000.00);182
Vehiclev3=newCar("polo",145678.00,5);183
184
users[0]=newUser(1,"yanming");185
users[0].setV(v1);186
users[1]=newUser(2,"laoxia");187
users[1].setV(v2);188
users[2]=newUser(3,"zhaomengran");189
users[2].setV(v3);190
191
192
System.out.println("=====tobegintestclass=====");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("=====tohandupRoadTax=====");202
for(Useru:users){203
Vehiclev=u.getV();204
if(vinstanceofCar){205
Carc=(Car)v;206
c.tax();207
}208209
if(vinstanceofMotor){210
Motorm=(Motor)v;211
m.tax();212
}213214
if(vinstanceofBike){215
System.out.println("hah,notax");216
}217
}218
}219}
掃一掃
獲取更多福利
獵學(xué)網(wǎng)企業(yè)微信
獵學(xué)網(wǎng)訂閱號(hào)
獵學(xué)網(wǎng)服務(wù)號(hào)