Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/340.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/215.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在java变量中使用关键字final的原因_Java_Android_Inheritance - Fatal编程技术网

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

在java变量中使用关键字final的原因,java,android,inheritance,Java,Android,Inheritance,我正在Android上进行继承领域的培训,我想知道为什么名字和颜色变量的定义来自最终关键字——当我删除这个关键字时,没有任何用处。当我得到这个关键字时,没有错误或意外 -请告诉我使用期末考试的原因是什么 MainActivity.java public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.on

我正在Android上进行继承领域的培训,我想知道为什么名字和颜色变量的定义来自最终关键字——当我删除这个关键字时,没有任何用处。当我得到这个关键字时,没有错误或意外 -请告诉我使用期末考试的原因是什么

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

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

变量在声明时初始化:

private final String name = "Rover";
…或在构造函数中,正如您在上面的代码中所做的那样

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

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

public final float PI = 3.14159;
这里有一篇文章介绍final关键字的其他用法


记录并强制执行该值是不可变的,即在构造函数中赋值后不能更改。@安德烈如果它不是最终值,如何在构造函数中更改?还有任何继承可以改变的类吗?@Andreas这是我认为最好的理由。如果你看到一些声明为final的东西,你(通常)应该想“嘿,也许我不应该试图编辑它”。尽管已经不可能在课堂外编辑,但将其声明为最终版本只会让你在课堂内更加安全,以防发生意外。如果最终版本无法更改,为什么可以在此处更改?动物动物1=新动物(“老虎”,“橙色”,60,80);Cat cat1=新猫(“波斯猫”、“棕色猫”、40、25、4、真猫);这是否意味着变量名不会在最后更改?在这两种情况下,您都是在第一次调用构造函数来分配名称。构造函数是唯一的例外。要亲自查看,请将此方法添加到Animal:public void setName(String name){this.name=name;}如果您有一个合适的编辑器,它将显示一个错误,例如“无法分配最终字段Animal.name”。现在我很清楚了