简体   繁体   English

在继承类重写方法中使用基类方法值

[英]Using Base Class Method Value In Inherited Classes Overridden Method

Base Class; 基类;

import java.util.Random;

public class Animal
{

    public void move()
    {
        int value = 0;
        System.out.println("Move");
        Random rand = new Random(); 
        value = rand.nextInt(2)+1;

    }

}

Inherited Class; 继承类;

public class Cat extends Animal
{

    public void moveCat()
    {
        super.move();
        System.out.println("Move Cat");

    }

    public static void main(String[] args)
    {
        Cat test = new Cat();
        test.moveCat();
    }
}

I Am trying to use a value of the base class animals method move in the override method moveCat. 我试图使用基类动物方法的值移动覆盖方法moveCat。 Why cant I use the value "value" in moveCat from Cat. 为什么我不能在Cat的moveCat中使用值“value”。

For Example; 例如;

public void moveDoodle()
{
    super.move();
    System.out.println("Move Doodle");
    if(value == 1)
    {
        System.out.println("Value from base");
    }

}

If I am grabbing the content from the base method why can't I also use the values. 如果我从基本方法中获取内容,为什么我也不能使用这些值。 If its not possible what should I be doing instead in order to get the values I need. 如果它不可能我应该做什么而不是为了获得我需要的值。

That's because value is in the local scope of the method move() of your base( Animal ) class and that is why its not inherited. 那是因为value在你的base( Animal )类的方法move()的局部范围内,这就是为什么它不被继承。 Only the instance variables will be inherited(provided they are not private ). 只有实例变量才会被继承(前提是它们不是private )。 Thus, you need to make value an instance variable for you to be able to inherit it in your base( Animal ) class. 因此,你需要做value实例变量让你能够继承它在你的基地( Animal )类。

int value = 0;
public void move()
{
    // int value = 0;
    System.out.println("Move");
    Random rand = new Random(); 
    value = rand.nextInt(2)+1;
}

Note: I can see that you've inherited the Animal class but have not overriden any method, contrary to what was suggested in the question title. 注意:我可以看到你继承了Animal类但没有覆盖任何方法,这与问题标题中的建议相反。

because value is a local variable to method move() . 因为value是方法move()的局部变量。 Local variables are not inherited. 局部变量不会被继承。 Create the variable as instance variable. 将变量创建为实例变量。 By the way, if you are trying to override to move() method, you need to keep the same method signature. 顺便说一句,如果你试图覆盖move()方法,你需要保持相同的方法签名。

import java.util.Random;

public class Animal {
    int value = 0;

    public void move() {

        System.out.println("Move");
        Random rand = new Random();
        value = rand.nextInt(2) + 1;

    }

}

Your Cat class 你的猫班

public class Cat extends Animal {
    public void moveCat() {
        super.move();
        System.out.println("Move Cat");

    }

    public static void main(String[] args) {
        Cat test = new Cat();
        test.moveCat();
        System.out.println(test.value);
    }

}

With your implementation, the value variable is of local scope. 通过实现, value变量具有局部范围。 It is accessible only within the move method and the outer world doesn't know about this. 它只能在move方法中访问,外部世界不知道这一点。

You will need to declare this variable at the instance scope in order to make it accessible at the class level. 您需要在实例范围内声明此变量,以便在类级别访问它。 You will also need to declare the access modifier for this so that the inheriting class know about it. 您还需要为此声明访问修饰符,以便继承类了解它。

The following will work. 以下将有效。

protected int value = 0;

public void move()
{
    System.out.println("Move");
    Random rand = new Random(); 
    value = rand.nextInt(2)+1;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM