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



我正在继承领域接受安卓训练 我想知道为什么从最终关键字定义名称和颜色变量 - 当我删除这个关键字时,没有任何用处。当我得到这个关键字时,没有错误或事故- 请告诉我使用决赛的原因是什么

主要活动.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());

}

动物.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);
    }
}

猫.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();
    }
}

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

变量在声明时初始化:

private final String name = "Rover";

。或在构造函数中,就像您在上面的代码中所做的那样。

在上面的代码示例中,setName 方法不适用于最终名称。但是,您可以调用setAmountOfSpeed,因为amountOfSpeed不是最终的。

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

public final float PI = 3.14159;

这是一篇关于最终关键字的其他用法的文章。

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

最新更新