2016-03-07 33 views
1

我開始發展我的JAVA技能,但是我有一個疑問。 我在JAVA中創建一個對象,創建了構造函數等,然後,它詢問「將AGE_RECENT值從1更改爲3」,我最初聲明這是最終的,因爲我從未想過它會改變,所以沒有SET或GET已創建。我想知道如何在SET方法中將值從1更改爲3。 我有這個變量如何更改SET方法中的值

private static int AGE_RECENT=1; 

我做到了。

public void setAgeRecent() { 
    Vehicle.AGE_RECENT = 3; 
} 

它可以工作,如果您運行程序,它會更改變量的值,但是沒有在每個SET方法中聲明該方法。 只是想知道我該如何做到這一點。如果這是正確的,好的,如果沒有,謝謝你的幫助!

正如有人問,代碼。

/* 
* To change this license header, choose License Headers in Project Properties. 
* To change this template file, choose Tools | Templates 
* and open the template in the editor. 
*/ 
package tp1; 

/** 
* 
* @author Nelson 
*/ 
public class Vehicle { 

/** Variáveis da classe, têm como função **/ 
private String registration; 

private int registrationYear; 

private double consumption; 

private double autonomy; 

private int cilinderCapacity; 

/** 
* Final variables. They are final because they do not suffer any kind of modification during the project. 
* YEAR_OMISSION is 2016 because the currect year is 2016. 
* ENVIRONMENTAL_CHARGE_OMISSION is 0.10(10 cents), gave this value because there is nothing to mention the 
especific value, hence why I gave 0.10. 
* RATING_RECENT = Is a string, just has the text "RECENT" inside. 
* RATING_COMTEMPORY - Another string, just with the "Comtempory" text inside. 
* RATING_CLASSIC - Yet again another string, with the "Classic" text. 
* AGE_RECENT - It is to help to compare if a vehicle is recent or not, it has the value 3. 
* AGE_CLASSIC - It is to again help to compare, value is 20. 
*/ 

private static final int YEAR_OMISSION = 2016; 
private static final double ENVIRONMENTAL_CHARGE_OMISSION=0.10; 
private static final String RATING_RECENT="Recent"; 
private static final String RATING_CONTEMPORY="Contempory"; 
private static final String RATING_CLASSIC="Classic"; 
private static int AGE_RECENT=1; 
private static final int AGE_CLASSIC=20; 




/** 
* Constructor of the object, it has the Registration 
    * @param registration 
    * @param registrationYear - The year the vehicle was first registered. 
    * @param consumption - How many liters the vehicle consumes. 
    * @param autonomy - How many KMs a vehicle can go without refuelling. 
    * @param cilinderCapacity - How many Cubic Inches the engine has. 
*/ 
public Vehicle(String registration,int registrationYear, double consumption, double autonomy, int cilinderCapacity) { 
this.registration = registration; 
this.registrationYear = registrationYear; 
this.consumption = consumption; 
this.autonomy = autonomy; 
this.cilinderCapacity = cilinderCapacity; 
} 

/** 
* Null Constructor, it has no values, they will be attributed in the MAIN Class. 
*/ 

public Vehicle() { 
this.registration = ""; 
this.registrationYear = 0; 
this.consumption = 0; 
this.autonomy = 0; 
this.cilinderCapacity =0; 
this.registrationYear = YEAR_OMISSION; 
} 
/** 
* Copy Constructor. 

*/ 
public Vehicle(Vehicle vehicle) { 
this.registration = vehicle.getRegistration(); 
this.registrationYear = vehicle.getRegistrationYear(); 
this.consumption = vehicle.getConsumption(); 
this.autonomy = vehicle.getAutonomy(); 
this.cilinderCapacity = vehicle.getCilinderCapacity(); 
} 

    public String getRegistration() { 
     return registration; 
    } 

    public int getRegistrationYear() { 
     return registrationYear; 
    } 

    public double getConsumption() { 
     return consumption; 
    } 

    public double getAutonomy() { 
     return autonomy; 
    } 

    public int getCilinderCapacity() { 
     return cilinderCapacity; 
    } 


    public double getYearRecent() { 
     return AGE_RECENT; 
    } 

    public double getAgeRecent(){ 
     return AGE_RECENT; 
    } 



    public void setRegistration(String registration) { 
     this.registration = registration; 
    } 

    public void setRegistrationYear(int registrationYear) { 
     this.registrationYear = registrationYear; 
    } 

    public void setConsumption(double consumption) { 
     this.consumption = consumption; 
    } 

    public void setAutonomy(double autonomy) { 
     this.autonomy = autonomy; 
    } 

    public void setCilinderCapacity(int cilinderCapacity) { 
     this.cilinderCapacity = cilinderCapacity; 
    } 

    public void setAgeRecent() { 
    Vehicle.AGE_RECENT = 3; 
} 



/** 
* Calculate the age of the vehicle to compare in the vehicleRating method 
* @return The year, which is 2016 minus the year the vehicle was first registered. 
*/ 
private int calculateAge(){ 
    return YEAR_OMISSION-this.registrationYear; 

} 

/** 
* Calculate the Circulation Tax. 
* @return Returns the value of the Environmental Charge multiplied by the Cilinder Capacity of the vehicle. 
*/ 
    public double calculateCirculationTax(){ 

    return ENVIRONMENTAL_CHARGE_OMISSION*cilinderCapacity; 

     } 





    /** 
    * Classify the vehicle based on the age. 
    * If the result given by the calculateAge method is minor than the AGE_RECENT variable(3), then it will 
    return "Recent" 
    * If the result is between Age_RECENT and AGE_CLASSIC(20), then it will say "Contemporary" 
    * If none of the IFs apply, it will return "Classic". 
    **/ 
public static String vehicleRating(Vehicle vehicle) { 
if(vehicle.calculateAge() < Vehicle.AGE_RECENT) { 
    return Vehicle.RATING_RECENT; } 
else if ((vehicle.calculateAge()>=Vehicle.AGE_RECENT)&&(vehicle.calculateAge()<=Vehicle.AGE_CLASSIC)){ 
    return Vehicle.RATING_CONTEMPORY;} 
else 
return Vehicle.RATING_CLASSIC; 

} 

    @Override 
    public String toString() { 
     return "Vehicle{" + "registration=" + registration + ", registrationYear=" + registrationYear + ", consumption=" + consumption + ", autonomy=" + autonomy + ", cilinderCapacity=" + cilinderCapacity + '}'; 
    } 


} 
+0

變量不是最終的,是靜態的,這意味着它屬於類,靜態變量和最終變量之間存在巨大差異。 –

+0

它可能會有助於顯示其餘的代碼,以便我們可以看到發生了什麼。該屬性可能最適合作爲實例變量而不是類變量。 –

+0

我知道它不是最終的,它是在一開始,但如果我想改變它,這將是不可能的,因爲你知道,我刪除了最終。 –

回答

1

甲設定器,它沒有參數是一個簡單的方法,而不是設置器。爲了工作作爲一個setter方法必須是所設置的值的類型相匹配的參數 - 在你的情況,這將是int

public static void setAgeRecent(int age) { 
    AGE_RECENT = age; 
} 

注意的幾件事情在這裏:

  • 由於AGE_RECENTstaticsetAgeRecentstatic
  • 由於AGE_RECENTsetAgeRecent是同一類Vehicle的靜態成員,你不需要資格AGE_RECENTVehicle

現在類的用戶將能夠打電話給你靜態的setter方法如下:

Vehicle.setAgeRecent(3); 
+0

Waw,它實際上就是這樣工作的。謝謝! –

+0

@GregorioMerazJr。可以採用「靜態」(即「任何車型,無論其型號被認爲是最近的」)還是非「靜態」(即「三年前的梅賽德斯車型是近期的,而本田最近只有它是一年以下「)。最終,由程序設計者決定他將採取哪種方法。 – dasblinkenlight

+0

@dasblinkenlight是的,只是看到了整個代碼,它現在有點意義。 –

0

靜態varible,或類變量,可以無需創建一個實例可以使用該類別。但其價值可能會在運行時自由更改。

最終變量不是真正意義上的變量,因爲它的值在運行時無法更改。

因此,你可能有一個靜態變量的設置方法,但從來沒有到最後一個變量。