手机Av在线不卡,日韩精品校园婷婷五月天,国产轮理电影一区二区,日韩三级欧美视频

廣西南寧達內(nèi)軟件科技有限公司

[其他技能培訓(xùn)]
獵學(xué)網(wǎng)訂閱號
獵學(xué)網(wǎng)官方企業(yè)微信
位置: 獵學(xué)網(wǎng) > 學(xué)校機構(gòu) > 廣西南寧達內(nèi)軟件科技有限公司 > 學(xué)習(xí)資訊> Java類繼承以及接口實現(xiàn)實例

Java類繼承以及接口實現(xiàn)實例

94 2017-04-14

南寧達內(nèi):Java類繼承以及接口實現(xiàn)實例

Java類繼承以及接口實現(xiàn)實例:這個實例非常容易理解,貼在這里與大家共享。

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

//父類的抽象方法,在子類中要具體實現(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)路費的接口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);//通過父類方法進行賦值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的抽象方法,在子類中要具體實現(xiàn)。72

publicvoidrun(){73

System.out.println("iambikeandrunning……");74

}7576

//繼承父類Vehicle的抽象方法,在子類中要具體實現(xiàn)。

77

publicvoidstop(){78

System.out.println("iambikeandstopping……");79

}80}81828384//子類汽車Car,繼承Vehicle,同時實現(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);//通過父類方法進行賦值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的抽象方法,在子類中要具體實現(xiàn)。107

publicvoidrun(){108

System.out.println("iamcarandrunning……");109

}110111

//繼承父類Vehicle的抽象方法,在子類中要具體實現(xiàn)。112publicvoidstop(){113

System.out.println("iamcarandstopping……");114

}115116

//實現(xiàn)上繳養(yǎng)路費的接口方法

117

publicvoidtax(){118

System.out.println("thecartaxis"+getCost()*0.1);119

}

120}121122123124//子類摩托車Motor,繼承Vehicle,同時實現(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//測試類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}

溫馨提示: 專業(yè)老師1對1為您解答    馬上填寫,¥1000 元豪禮免費領(lǐng)!

掃一掃
獲取更多福利

×
獵學(xué)網(wǎng)
后入韩国三级在线视频| 日韩一区,欧美,狠狠干.com| 欧美性感视频网站免费观看 | 久久av天天日av| 国产玖玖爱一区二区乱码| 日一区二区手机在线观看| 亚洲无码一区尤物| 在线超碰中文字幕网| 女同久久久久久久久久久久| chinese麻豆99| 国产美女自慰在线观看| 色情亚洲综合网站| 午夜看片欧美日韩精品| 激情欧美少妇性生活| 久久久国产精品露脸| 欧美二区三区在线| B.B视频在线| 日韩av一级片| 本庒优花无码在线| 成人黄色永久性综合网| Japanese69无码一区| 深爱五月视频在线免费观看| 97中文国产一区二区| 日本熟妇人妻在线| 日韩久久乱码AV一区| 国产精品婬乱一级毛| 亚洲资源在线中文字幕在线| 欧美亚洲色欲色一欲www软件| 色av性指导| 久久久久韩国视频| 日本高清视频一区二区三区| 偷拍一区免费| 日 韩 黄 片久久| 国产亚洲天堂呦呦在线播放| 久久高潮视频一区| 午夜无码国产一二三区天美| 中文无码人妻在线一区| 另类久久老女人| 久久亚洲精品天堂| 欧美日韩1级免费播放| 白姨91fv|