2016-03-07 32 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,或类变量,可以无需创建一个实例可以使用该类别。但其价值可能会在运行时自由更改。

最终变量不是真正意义上的变量,因为它的值在运行时无法更改。

因此,你可能有一个静态变量的设置方法,但从来没有到最后一个变量。