简体   繁体   English

如何从 Java 中的子类访问私有数组?

[英]How do I access a private array from a subclass in Java?

I know its not possible to directly call private array's from within a subclass but I still need to someway to access it so what's the best way to go about this?我知道从子类中直接调用私有数组是不可能的,但我仍然需要以某种方式访问它,那么关于这个问题,go 的最佳方法是什么? Right now I have a class for my main method, random array, player array and a fourth class which I want to use to compare the two arrays.现在我有一个 class 用于我的主要方法、随机数组、播放器数组和第四个 class,我想用它来比较两个 arrays。

So far I have tried to make a getter method which just returns the array and then I would call that getter method from the compare class.到目前为止,我已经尝试创建一个仅返回数组的 getter 方法,然后我会从比较 class 中调用该 getter 方法。 This failed so then I tried to make a basic for loop would return each value in that array but this also does not work.这失败了,所以我尝试制作一个基本的 for 循环将返回该数组中的每个值,但这也不起作用。 So my question is how can I get the the values of a private array into a subclass?所以我的问题是如何将私有数组的值放入子类中?

Here is one of my attempts which just prints 0 0 0 0这是我的尝试之一,它只打印 0 0 0 0

    public class test {
      public static void main(String[] args) {
      Scanner console = new Scanner(System.in);

      PlayerArray player = new PlayerArray(console);

      testArray test = new testArray();
      test.printArray();
    }


    class PlayerArray extends RandomArray {
       private int[] playerArray = new int[4];

       public PlayerArray() {
       }

       public PlayerArray(Scanner console) {
           System.out.println();
           System.out.println("Enter 4 numbers you wish to guess: ");
           for(int i=0; i<playerArray.length; i++) {
              playerArray[i] = console.nextInt();
           }
       }

      public int[] getPlayerArray() {
         return playerArray;
      }
    }


  class testArray extends PlayerArray {

    public void testArray() {
    }

    public void printArray() {
        System.out.println(getPlayerArray());
        for (int i = 0; i < 4; i++) {
            System.out.print(getPlayerArray()[i]);
        }

    }
 }

You're doing it.你在做。

However, public void testArray() does nothing - did you intend to make a constructor?但是, public void testArray()什么都不做——你打算创建一个构造函数吗? Those don't have a return type, so, just public testArray .那些没有返回类型,所以只有public testArray

Because you did not write a constructor, java made one for you.因为你没有写构造函数,所以java给你做了一个。 It's equivalent to:它相当于:

public testArray() {
    super();
}

as in, it calls the PlayerArray constructor that doesn't do anything, thus resulting in the playerArray holding 4x a 0 value.例如,它调用了不做任何事情的 PlayerArray 构造函数,从而导致 playerArray 持有 4x a 0 值。 If you want something else, well, that Scanner reference isn't going to appear out of thin air, so:如果您想要其他东西,那么Scanner参考不会凭空出现,所以:

public testArray(Scanner console) {
    super(scanner);
}

to invoke 'the right one'.调用“正确的人”。

NB: It should be TestArray , and making arrays visible to externals is bad as making them public;注意:它应该是TestArray ,让 arrays 对外部可见是不好的; you have no control over it, and anybody you give it to can change it with eg getPlayerArray()[i] = 10;你无法控制它,你给它的任何人都可以改变它,例如getPlayerArray()[i] = 10; . . Generally, use Lists ( java.util.List ) and such instead of arrays.通常,使用列表( java.util.List )等代替 arrays。

  PlayerArray player = new PlayerArray(console);

Here you create an instance of PlayerArray which takes in the console and populating the internal array.在这里,您创建一个 PlayerArray 实例,它接收控制台并填充内部数组。

  testArray test = new testArray();
  test.printArray();

Here you create a NEW instance of testArray where the internal array hasn't been populated.在这里,您创建一个新的 testArray 实例,其中尚未填充内部数组。

For it to print the correct values you need to copy the printArray() method to the Player class and call the printArray() method on the player instance.为了打印正确的值,您需要将 printArray() 方法复制到播放器 class 并在播放器实例上调用 printArray() 方法。

  player.printArray();

and just delete the testArray() instance from the Main method.并从 Main 方法中删除 testArray() 实例。 It serves no purpose here.它在这里毫无用处。

Alternatively you can add a constructor in the Player class that takes in a Console and passes it to the Super via super(console);或者,您可以在 Player class 中添加一个构造函数,该构造函数接受一个控制台并通过 super(console) 将其传递给 Super;

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

相关问题 如何从子类访问私有成员 - How do I access a private member from a subclass 如何访问 java 中子类的变量? - How do I access the variables of a subclass in java? 如何访问从Java中抽象类的子类实例化的对象? - How do I access the object that's been instantiated from the abstract class's subclass in java? 在Java中,如何在Deque类的子类中实现新功能而不访问Deque类中的私有字段? - In Java, how can I implement new functionality in a subclass of a Deque class without access to the private fields in the Deque class? 如何访问从超类传递下来的子类属性? - How do I access subclass attributes passed down from superclass? 如何使用Java反射访问私有数组? - How can I get access to a private array using Java reflection? 如何从Java中私有的另一个类访问数组? - How to access an array from another class that is private in Java? 如何从子类访问属性[field]并在子类的构造函数中创建它? - How do i access attribute [field] from subclass and create it in constructor of a subclass? 如何在不使用setter的情况下从子类访问父类中的私有字段? - How to access private fields in superclass from a subclass without using setters? Java中私有成员变量的子类访问 - Subclass access of Private member variables in java
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM