AV大片,日韩伦理一区,国产在线a,开心播播网婷婷在线

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

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

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

84 2017-04-14

南寧達(dá)內(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);//通過父類方法進(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的抽象方法,在子類中要具體實現(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);//通過父類方法進(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的抽象方法,在子類中要具體實現(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)
少妇高潮无码一区| 久久香蕉国产线看观看亚洲小说| 国产精品久久久AAA| 蜜桃视频污瑟涩色在线| 久久即re| 人妻免费久久久久久久了| 天天爽av| 色鬼网站亚洲| 欧美中文字幕亚产强奸| 91看视频| 国产综合无码久久亚洲| 中文字幕7777| 97超碰论坛| 久久综合五月丁| 亚洲欧美日韩免费| 久久人妻情侣| 夜夜爽精彩免费视频| 国产高清视频一区三区| 国产一区二区三区麻豆| 东京热无码直播| 最新超碰97| 狼人大香伊蕉在人线国产| 亚洲国产精品按摩滚蛋| 亚洲无码高清大全| 人妻熟女25p| 人妻仑乱A级毛片免费看| 国模337| 国一级片| 国模无码免费观看视频| 亚洲天堂久久| 免费黄色无码视频| 婷婷六月色色色| 色五月之开心五月| 亚洲黄在线播放| 乱亲女H秽乱长久久久| 欧美AAA一级黄片| 国产精品伦理宾馆| 中文字幕日韩精品无码| 互换人妻久久| 99在线视频免费观看| 成人免费黄色视频|