在Java变量中使用关键字final的原因

时间:2019-01-18 22:21:49

标签: java android inheritance

我正在继承方面在Android上进行培训,并且我想知道为什么使用final关键字定义名称和颜色变量的原因-当我删除此关键字时,没有任何用处。当我得到这个关键字时,没有错误或意外 -请告诉我使用决赛的原因是什么

MainActivity.java

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    TextView txtAnimal = (TextView) findViewById(R.id.txtAnimal);
    TextView txtCat = (TextView) findViewById(R.id.txtCat);

    Animal animal1 = new Animal("tiger", "orange", 60, 80);
    Cat cat1 = new Cat("persian", "brown", 40, 25, 4, true);

    txtAnimal.setText(animal1.toString());
    txtCat.setText(cat1.toString());


}

Animal.java

public class Animal extends Object{

    private final String name;
    private final String color;
    private int amountOfSpeed;
    private int amountOfPower;

    public Animal(String name, String color, int amountOfSpeed, int amountOfPower){

        // this. for same name
        this.name = name;
        this.color = color;
        this.amountOfSpeed = amountOfSpeed;
        this.amountOfPower = amountOfPower;
    }

    // we can use setter because variable (name-color) are defined final
    public String getName(){
        return name;
    }
    public String getColor(){
        return color;
    }
    public void setAmountOfSpeed(int amountOfSpeed){
        this.amountOfSpeed = amountOfSpeed;
    }
    public int getAmountOfSpeed(){
        return amountOfSpeed;
    }
    public void setAmountOfPower(int amountOfPower){
        this.amountOfPower = amountOfPower;
    }
    public int getAmountOfPower(){
        return amountOfPower;
    }

    public int evaluateAnimalValue(){
        int result = amountOfSpeed *amountOfPower;
        return result;
    }


    @Override
    public String toString() {
        return String.format("%s: %s  %s: %s  %s: %d  %s: %d",
                "Name", name,
                "Color", color,
                "Speed", amountOfSpeed,
                "Power", amountOfPower);
    }
}

Cat.java

  private final int numberOfLegs;
    private boolean canHuntOtherAnimal;

    public Cat(String name, String color, int amountOfSpeed, int amountOfPower, int numberOfLegs, boolean canHuntOtherAnimal){

        super(name, color, amountOfSpeed, amountOfPower);
        this.numberOfLegs = numberOfLegs;
        this.canHuntOtherAnimal = canHuntOtherAnimal;
    }



    public int getNumberOfLegs() {
        return numberOfLegs;
    }

    public boolean getCanHuntOtherAnimal() {
        return canHuntOtherAnimal;
    }

    public void setCanHuntOtherAnimal(boolean canHuntOtherAnimal) {
        this.canHuntOtherAnimal = canHuntOtherAnimal;
    }

    @Override
    public String toString() {

        return super.toString() + String.format("  %s: %d  %s: %b",
                "Legs", numberOfLegs,
                "Fight", canHuntOtherAnimal) + "  Animal Value: " + evaluateAnimalValue();
    }
}

1 个答案:

答案 0 :(得分:0)

变量的最终关键字表示无法修改该值。该变量必须设置一次,然后才能更改。

在声明变量时对其进行初始化:

private final String name = "Rover";

...或者在构造函数中,就像上面的代码中一样。

在上面的代码示例中,setName方法无法使用最终名称。但是,由于amountOfSpeed不是最终值,因此可以调用setAmountOfSpeed。

final关键字通常用于表示常量:

public final float PI = 3.14159;

这是一篇有关final关键字的其他用法的文章。

https://www.geeksforgeeks.org/final-keyword-java/