/////////////////////////2016-10-31//////////////////////////
int study_data(){
? ? ? ? 構(gòu)造方法:
? ? ? ? 構(gòu)造函數(shù)是一種特殊的函數(shù)。其主要功能是用來(lái)在創(chuàng)建對(duì)象時(shí)初始化對(duì)象, 即為v對(duì)象成員變量賦初始值,總與new運(yùn)算符一起使用在創(chuàng)建對(duì)象的語(yǔ)句中。構(gòu)造函數(shù)與類(lèi)名相同,可重載多個(gè)不同的構(gòu)造函數(shù)。在JAVA語(yǔ)言中,構(gòu)造函數(shù)與C++語(yǔ)言中的構(gòu)造函數(shù)相同,JAVA語(yǔ)言中普遍稱(chēng)之為構(gòu)造方法。
? ? ? ? 方法重載:
? ? ? ? 對(duì)于同一個(gè)類(lèi),如果這個(gè)類(lèi)里面有兩個(gè)或者多個(gè)重名的方法,但是方法的參數(shù)個(gè)數(shù)、類(lèi)型、順序至少有一個(gè)不一樣,這時(shí)候局構(gòu)成方法重載。
}
int mission(){
? ? ? ? 建立一個(gè)汽車(chē)類(lèi),包括輪胎個(gè)數(shù),汽車(chē)顏色,車(chē)身重量等屬性。并通過(guò)不同的構(gòu)造方法創(chuàng)建事例。至少要求:汽車(chē)能夠加速,減速,停車(chē)。
要求:命名規(guī)范,代碼體現(xiàn)層次,有友好的操作提示。
源碼如下:
public class Cars {
? ? int tyre,weight;
? ? String color;
? ? public static void main(String []args){
? ? ? ? Cars c=new Cars();
? ? ? ? c.display();
? ? ? ? Cars c1=new Cars(4,100);
? ? ? ? c1.display();
? ? ? ? Cars c2=new Cars(6,200,"白色");
? ? ? ? c2.display();
? ? ? ? c2.add(10);
? ? ? ? c2.sub(10);
? ? ? ? c2.stop();
? ? }
? ? public Cars(){
? ? ? ? tyre=1;
? ? ? ? weight=50;
? ? ? ? color="黑色";
? ? }
? ? public Cars(int tyre,int weight){
? ? ? ? this.tyre=tyre;
? ? ? ? this.weight=weight;
? ? ? ? color="黑色";
? ? }
? ? public Cars(int tyre,int weight,String color){
? ? ? ? this.tyre=tyre;
? ? ? ? this.weight=weight;
? ? ? ? this.color=color;
? ? }
? ? public void add(int speed){
? ? ? ? System.out.println("車(chē)加速了"+speed+"km/h");
? ? }
? ? public void sub(int speed){
? ? ? ? System.out.println("車(chē)減速了"+speed+"km/h");
? ? }
? ? public void stop(){
? ? ? ? System.out.println("車(chē)停了");
? ? }
? ? public void display(){
? ? ? ? System.out.println("車(chē)的輪胎數(shù):"+tyre);
? ? ? ? System.out.println("車(chē)的載重:"+weight);
? ? ? ? System.out.println("車(chē)的顏色:"+color);
? ? }
}
}