Engine.java
創(chuàng)新互聯(lián)網(wǎng)站建設提供從項目策劃、軟件開發(fā),軟件安全維護、網(wǎng)站優(yōu)化(SEO)、網(wǎng)站分析、效果評估等整套的建站服務,主營業(yè)務為網(wǎng)站設計制作、成都做網(wǎng)站,app軟件開發(fā)以傳統(tǒng)方式定制建設網(wǎng)站,并提供域名空間備案等一條龍服務,秉承以專業(yè)、用心的態(tài)度為用戶提供真誠的服務。創(chuàng)新互聯(lián)深信只要達到每一位用戶的要求,就會得到認可,從而選擇與我們長期合作。這樣,我們也可以走得更遠!
package?com.nemo.test;
/**
*?發(fā)動機
*/
public?class?Engine?{
private?float?paiqiliang;?//?排氣量
public?float?getPaiqiliang()?{
return?paiqiliang;
}
public?void?setPaiqiliang(float?paiqiliang)?{
this.paiqiliang?=?paiqiliang;
}
public?void?startEngine()?{
System.out.println("發(fā)動機啟動!??!");
}
public?void?stopEngine()?{
System.out.println("發(fā)動機關閉?。?!");
}
}
SteeringWheel.java
package?com.nemo.test;
/**
*?方向盤
*/
public?class?SteeringWheel?{
private?String?caizhi;?//?材質(zhì)
public?String?getCaizhi()?{
return?caizhi;
}
public?void?setCaizhi(String?caizhi)?{
this.caizhi?=?caizhi;
}
public?void?xuanzhuanSteeringWheel()?{
System.out.println("選中方向盤?。?!");
}
}
Wheel.java
package?com.nemo.test;
/**
*?車輪
*/
public?class?Wheel?{
private?String?brand;?//?品牌
public?String?getBrand()?{
return?brand;
}
public?void?setBrand(String?brand)?{
this.brand?=?brand;
}
public?void?chongqi()?{
System.out.println("給車輪充氣?。?!");
}
}
Car.java
package?com.nemo.test;
/**
*?汽車
*/
public?class?Car?{
private?String?brand;?//?汽車品牌
private?Engine?engine;?//?發(fā)動機
private?Wheel?wheel;?//?車輪
private?SteeringWheel?steeringWheel;?//?方向盤
public?String?getBrand()?{
return?brand;
}
public?void?setBrand(String?brand)?{
this.brand?=?brand;
}
public?Engine?getEngine()?{
return?engine;
}
public?void?setEngine(Engine?engine)?{
this.engine?=?engine;
}
public?Wheel?getWheel()?{
return?wheel;
}
public?void?setWheel(Wheel?wheel)?{
this.wheel?=?wheel;
}
public?SteeringWheel?getSteeringWheel()?{
return?steeringWheel;
}
public?void?setSteeringWheel(SteeringWheel?steeringWheel)?{
this.steeringWheel?=?steeringWheel;
}
public?Car(Engine?engine,?Wheel?wheel,?SteeringWheel?steeringWheel)?{
this.brand?=?"東風";
this.engine?=?engine;
this.wheel?=?wheel;
this.steeringWheel?=?steeringWheel;
}
public?Car()?{
}
public?static?void?main(String[]?args)?{
Engine?engine?=?new?Engine();?//?造發(fā)動機
engine.setPaiqiliang(1.5f);?//?設置發(fā)動機排氣量
SteeringWheel?steeringWheel?=?new?SteeringWheel();?//?造方向盤
steeringWheel.setCaizhi("黃金");?//?設置方向盤材質(zhì)
Wheel?wheel?=?new?Wheel();?//?造車輪
wheel.setBrand("蘭博基尼");?//?設置車輪品牌
Car?car?=?new?Car(engine,?wheel,?steeringWheel);?//?造汽車
wheel.chongqi();?//?給車輪充氣
engine.startEngine();?//?啟動發(fā)動機
steeringWheel.xuanzhuanSteeringWheel();?//?選中方向盤
System.out.println(car.getBrand()?+?"牌子的汽車開動了!??!");
}
}
發(fā)動機是汽車的一個零件,可以用組合模式來寫,奔馳汽車與汽車之間是屬于繼承關系,這題考的應該就是繼承與組合的應用,你可以試著寫寫
class?Automobile
{
private?String?發(fā)動機型號;
private?int?座位數(shù);
public?String?get發(fā)動機型號()
{
return?發(fā)動機型號;
}
public?void?set發(fā)動機型號(String?發(fā)動機型號)
{
this.發(fā)動機型號?=?發(fā)動機型號;
}
public?int?get座位數(shù)()
{
return?座位數(shù);
}
public?void?set座位數(shù)(int?座位數(shù))
{
this.座位數(shù)?=?座位數(shù);
}
@Override
public?String?toString()
{
return?String.format("[發(fā)動機型號=%s,?座位數(shù)=%s]",?發(fā)動機型號,?座位數(shù));
}
}
interface?收費
{
public?void?收費方法();
}
interface?調(diào)速
{
public?void?調(diào)速方法();
}
class?公共汽車?extends?Automobile?implements?收費,?調(diào)速
{
@Override
public?void?調(diào)速方法()
{
System.out.println("速度非常慢");
}
@Override
public?void?收費方法()
{
System.out.println("天價呢");
}
}
class?出租車?extends?Automobile?implements?收費,?調(diào)速
{
@Override
public?void?調(diào)速方法()
{
System.out.println("速度是20公里每小時");
}
@Override
public?void?收費方法()
{
System.out.println("出租車的收費是便宜的");
}
}
public?class?Main
{
public?static?void?main(String[]?args)
{
出租車?czc?=?new?出租車();
czc.set發(fā)動機型號("abc");
czc.set座位數(shù)(4);
System.out.println(czc);
czc.調(diào)速方法();
czc.收費方法();
公共汽車?ggqc?=?new?公共汽車();
ggqc.set發(fā)動機型號("ddf");
ggqc.set座位數(shù)(30);
System.out.println(ggqc);
ggqc.調(diào)速方法();
ggqc.收費方法();
}
}