简体   繁体   English

Java重写继承的方法

[英]Java rewriting inherited methods

So I naively wrote an abstract class (A game board) containing a field with no access modifier for an array used to store the game state (a Square[]). 所以我天真地写了一个抽象类(游戏板),其中包含一个没有访问修饰符的字段,用于存储游戏状态的数组(Square [])。 This class has various methods implemented using this Square[]. 这个类有使用这个Square []实现的各种方法。 I also have two subclasses both inheriting from this class, using these same methods from their parent class. 我还有两个继承自这个类的子类,它们使用父类中的相同方法。

Now, for one reason or another, I have to make my Square[] private. 现在,出于某种原因,我必须让我的Square []私有化。 I thought a simple fix would be to make a getter method as usual, 我认为一个简单的解决办法是像往常一样制作一个getter方法,

Class Board {
...
    /**An array contain all the Squares for my board. */
    private Square[] squares;

    public Square[] getSquares() {
        return squares;
    }
}

class MutableBoard extends Board {

    /** An N x N board in initial configuration. */
    MutableBoard(int N) {
        _N = N;
        squares = new Square[_N * _N];
        for (int i = 0; i < squares.length; i++) {
            squares[i] = new Square();
        }
    }

    @Override
    void copy(Board board) {
        _N = board.size();
        _moves = board.numMoves();
        squares = new Square[board.getSquares().length]; //Errors here when used
        for (int i = 0; i < squares.length; i++) {
            squares[i] = board.squares[i].clone();
        }
    }
}

class ConstantBoard extends Board {

    /** A new ConstantBoard that allows a read-only view of BOARD. */
    ConstantBoard(Board board) {
        _board = board;
        squares = board.getSquares();
    }
}

but I keep getting null pointer exceptions every time I try to access or modify my array in my subclasses using inherited methods. 但每次我尝试使用继承方法访问或修改子类中的数组时,我都会一直得到空指针异常。 I think because my Square[] is defined in my parent class, its trying to access Board's copy of the array, not the subclasses copy. 我认为因为我的Square []是在我的父类中定义的,它试图访问Board的数组副本,而不是子类副本。 Does anyone know an easy fix to this that doesn't involve just putting all my methods in both subclasses (and defeating the whole point of inheritance)? 有没有人知道一个简单的解决方法,不涉及只是将我的所有方法都放在两个子类中(并打败整个继承点)?

For example, in my copy method, I want to be able to pass in both a MutableBoard, and Constant Board, but I think its trying to access a copy of Board's squares somehow, which isn't even initialized. 例如,在我的复制方法中,我希望能够传入MutableBoard和Constant Board,但我认为它试图以某种方式访问​​Board的正方形副本,甚至没有初始化。

You should return Square[] 你应该返回Square[]

public Square[] getSquares() {
    return squares;
}

squares[] is of type Square , therefore the method should return a Square[] not a Board squares[]的类型为Square ,因此该方法应返回Square[]而不是Board

You also need to make sure you have squares[] initialized 您还需要确保已初始化squares[]

private Square[] squares = new Square[someSize];

for (int i = 0; i < suqares.length; i++){
    squares[i] = new Square();
}

Also, you haven't delcared _N before trying to initialize it in you MutableBoard 此外,在尝试在MutableBoard初始化之前,您还没有对_NMutableBoard

 _N = N;

public class MutableBoard extends Board {
    int _N;

    public MutablerBoard(int N){
        _N = N;
    }
}

-or- 

public abstract class Board {
    int _N;
}

same for _board

Also, you should make square[] in your Board class public static . 另外,你应该在你的Board类中使用square[] public static You can't modify it from MutableBoard if its private and you can't access the "current state" of square from ConstantBoard if it is not static . 如果它是private ,你不能从MutableBoard修改它,如果它不是static ,你不能从ConstantBoard访问square的“当前状态”。

Didn't getSquares() should return squares? 没有getSquares()应该返回正方形?

public Square[] getSquares() {
        return squares;
 }

You declare that you want to return instance of Board class and you return array of Squares. 您声明要返回Board类的实例并返回Square数组。

Assuming you mean: 假设你的意思是:

public Square[] getSquares() {
 return squares;
}

You have not initialised the array squares . 您尚未初始化数组squares You'll need: 你需要:

Square[] squares = new Square[SIZE];

Even then, although the array will be initialised, each Square in the array will still be null. 即使这样,虽然数组将被初始化,但数组中的每个Square仍将为null。 You'll need to initialsie them too: 你也需要初始化它们:

for (int i=0; i<SIZE; i++) {
  squares[i] = new Square();  //or some other way of getting a `Square`
}

Above, SIZE is some static final int indicating the number of squares. 在上面,SIZE是一些static final int表示正方形的数量。

EDIT: 编辑:

Your MutableBoard cannot see the squares array in Board because its private, and subclasses do not inherit private fields or methods. 您的MutableBoard无法在Board看到squares数组,因为它的私有和子类不会继承私有字段或方法。 The code you have should not compile. 你的代码不应该编译。 The squares variable in the MutableBoard constructor is not the squares field in Board . squares在变量MutableBoard构造不是 squares的场Board

I suggest you change squares to be protected so subclasses can inherit it. 我建议您更改要protected squares ,以便子类可以继承它。

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

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