简体   繁体   中英

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. Why cant I use the value "value" in moveCat from Cat.

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. Only the instance variables will be inherited(provided they are not private ). Thus, you need to make value an instance variable for you to be able to inherit it in your base( Animal ) class.

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.

because value is a local variable to method 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.

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. It is accessible only within the move method and the outer world doesn't know about this.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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