首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何更改SET方法中的值

如何更改SET方法中的值
EN

Stack Overflow用户
提问于 2016-03-07 19:20:18
回答 2查看 354关注 0票数 1

我开始用JAVA开发我的技能,但是我有一个疑问。我用JAVA创建了一个对象,创建了构造函数等等,然后,它要求“将AGE_RECENT值从1更改为3",我最初将其声明为final,因为我从未想过它会改变,因此没有创建任何SET或GET。我想知道如何将SET方法中的值从1更改为3。我有这个变量

代码语言:javascript
复制
private static int AGE_RECENT=1;

是我干的。

代码语言:javascript
复制
   public void setAgeRecent() {
    Vehicle.AGE_RECENT = 3; 
}

如果您运行该程序,它会更改变量的值,但是该方法中没有任何声明为每个SET方法。只是想知道我该怎么做。如果这是正确的,好的,如果不是,谢谢你的帮助!

就像有人问的,密码。

代码语言:javascript
复制
/*
 * 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 + '}';
    }


}
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2016-03-07 19:25:52

不使用参数的setter只是一种方法,而不是setter。为了作为setter工作,方法必须采用与所设置的值的类型相匹配的参数--在您的情况下,这将是int

代码语言:javascript
复制
public static void setAgeRecent(int age) {
    AGE_RECENT = age; 
}

请注意以下几点:

  • 因为AGE_RECENTstatic,所以setAgeRecent应该是static
  • 因为AGE_RECENTsetAgeRecent是同一个类Vehicle的静态成员,所以不需要用Vehicle限定AGE_RECENT

现在,您类的用户可以按如下方式调用静态setter:

代码语言:javascript
复制
Vehicle.setAgeRecent(3);
票数 1
EN

Stack Overflow用户

发布于 2016-03-07 19:26:35

可以使用静态varible或类变量,而不需要创建该类的实例。但是它的值在运行时可以自由地改变。

最后一个变量不是真正意义上的变量,因为它的值不能在运行时更改。

因此,您可能有一个静态变量的set方法,但永远不会对最后一个变量。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/35852136

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档