手机Av在线不卡,99r这里只有精品11,一区二区啪啪啪网站,白虎美女在线

廣西南寧達內(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)
久久穴99| 美女少妇真人内射视频| 欧美久久换aij| 精品少妇后入在线| 色欲av夜夜嗨av性色av| 996亚洲精品在线看| 91国内在线| 日本伦理有码中文字幕在线 | 欧美韩一区| 久久亚洲精品20| 长葛市| 天天综合影院久久久久久| A级啪啪片| 无码福利久久| 久久精品无码96| 久久福利资源在线观看| 亚洲欧美日韩成人电影| 日韩黄色影视| 日日夜夜操美| 性色Av一区太久Av| 欧美亚洲激情啪啪| 噜噜噜二区| www美女自慰高潮视频在线播放| 超碰在线 麻豆| 黄频永久免费在线观看| 操逼 毛片| 亚洲激情色情综合| 亚洲日韩久久麻豆| 偷拍2020熟女人妻| 91久久韩国| 国产日韩国欧美精品激情| 成人电影二区三区| 在线国产中文一区| 午夜一级】| 日本一三不卡观看视频| 国产女老师AV| 欧美性猛久久久久久四虎| 被黑人草草草| 91人人视频白丝| 性感美女教师援交视频网站| 欧美午夜黄色电影|