简体   繁体   中英

Access object from another class to get its variable

I got the object wolfOne of the Class Wolf , and I need to access to its variable mAlive in another Class, how may I don it?

    Wolf wolfOne;

//Wolf Class

public class Wolf extends Card {

    public Wolf(){
        mCharacter = "Wolf";
    }

    public void savage(Card card) {
        card.mAlive = false;
    }

}

//Card Class

public class Card {

    //Names
    public String mCharacter;

    //Status
    public static boolean mAlive;
    public static boolean mDefended;
    public static boolean mOwled;
    public static boolean mLastSavaged;
    public static boolean mLastLynched;


    //Constructor
    public Card() {
        // Do Card specific stuff.
    }
}

Remove static from all of your Class variables - make them instance variables instead. Then provide typical getters/setters for each, allowing clients of the class to retrieve or mutate the value:

public class Wolf extends Card {
    public Wolf(){
         setMCharacter("Wolf");
    }

    public void savage(Card card) {
        card.setMAlive(false);
    }
}


    public class Card {

        //Names
        private String mCharacter;

        //Status
        private boolean mAlive;
        private boolean mDefended;
        private static boolean mOwled;
        private static boolean mLastSavaged;
        private static boolean mLastLynched;

        public String getMCharacter(){}
            return mCharacter;
        }

        public void setMCharacter(String value){
            this.mCharacter = value;
        }

        public boolean getMAlive(){
            return mAlive;
        }

        public void setMAlive(boolean alive){
            this.mAlive = alive
        }

        //....So on and so forth
    }

static has a special meaning in Java. It doesn't mean that the variable or method is inheritable; it means that there is only one of it that belongs to the class, not the instance.

To inherit from a super class, all that is required is that it not private and the inheriting classes will get it. The following example shows this relationship.

import java.util.*;
import java.lang.*;
import java.io.*;

class A
{
    public String name;
    public boolean isAlive;
    public A()
    {
        name = "A";
        isAlive = true;
    }
}

class B extends A
{
    public B()
    {
        name = "B";
        isAlive = false;
    }
}

public class Main
{
    public static void main (String[] args)
    {
        A a = new A();
        A b1 = new B();
        B b2 = new B();
        b2.name = "B2";
        b2.isAlive = true;
        System.out.println(a.name);
        System.out.println(a.isAlive);
        System.out.println(b1.name);
        System.out.println(b1.isAlive);
        System.out.println(b2.name);
        System.out.println(b2.isAlive);
    }
}

And gives this output:

A
true
B
false
B2
true

This can be run here .

In the card class make the fields private not public, in oo this is called encapsulation or data hiding (look it up). Then simply add a getMAlive method that returns the mAlive value and a setMAlive method which will set it. Now in your wolf class to set mAlive you can with setMAlive(boolean). For external objects you will need to have a reference to your wolf/card and call wolfName.getMAlive()

For card...

private boolean mAlive;
public boolean getMAlive(){
  return mAlive;
}
public void setMAlive(boolean value){
  mAlive = value;
}

For wolf...

public void savage(){
  setMAlive(false);
}

For other classes to get mAlive...

wolfName.getMAlive()

You may consider making your mAlive (and other fields in Card) protected. Protected fields can only be seen by those classes that extend them eg wolf. So in wolfs savage method you could do...

public void savage(){
  mAlive = false;
}

But to set mAlive from other classes you would still need a setter in Card so yeah

I hope this helps :) good luck

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