简体   繁体   English

是否可以在 Java 中为其父类中的子类赋值?

[英]Is it possible to give a value to a child class in its parent class in Java?

I searched a lot for this but couldn't find it anywhere, but if you think that it had been asked before, please send me the link.我为此搜索了很多,但在任何地方都找不到,但如果您认为之前有人问过它,请将链接发给我。

I have a parent class called chesspiece and I have 6 child classes: Pawn with the value of 1, Knight with the value of 2, Bishop with the value of 3, Rook with the value of 5, Queen with the value of 9 and king with the value of 1000.我有一个名为chesspiece的父类,我有6个子类:值为1的Pawn,值为2的Knight,值为3的Bishop,值为5的Rook,值为9的Queen和king值为 1000。

I have two instance variable: value and the color of the piece我有两个实例变量:值和作品的颜色

I want to know is it possible to say if you have the value of 1 you belong to the pawn class or if you have the value of 2, you belong to the knight class.我想知道是否可以说,如果您的值为 1,您属于 pawn 类,或者如果您的值为 2,则您属于 Knight 类。 So, right now I'm doing this using a method but it only returns a String and nothing more.所以,现在我正在使用一种方法来执行此操作,但它只返回一个字符串,仅此而已。 Here's my code:这是我的代码:

public class ChessPiece {
    private boolean     isWhite;
    private int         value;

    public ChessPiece (boolean isWhite, int value) {
        this.isWhite = isWhite;
        this.value = value;
    }
public String getNamebyValue() {
        switch (value) {
          case 1:
            return "Pawn";
          case 2:
            return "Knight";
          case 3:
            return "Bishop";
          case 5:
            return "Rook";
          case 9:
            return "Queen";
          case 1000:
            return "King";
        }
        return null;
    } 

And here's my child class:这是我的孩子班级:

public class Bishop extends ChessPiece {

    public Bishop(boolean isWhite) {
        super(isWhite, 3);

    }

Suggestion:建议:

ChessPiece.java棋子.java

public class ChessPiece {
    private boolean     isWhite;
    private int         value;
    private String      name;

    public ChessPiece (boolean isWhite) {
        this.isWhite = isWhite;
    }

    public String getName() { return name; }

}

Bishop.java主教.java

public class Bishop extends ChessPiece {

    public Bishop(boolean isWhite) {
        super(isWhite);
        this.name = "Bishop";
        this.value = 3;
    }
    ...
}

In other words, let "class inheritance" do as much of the work as possible for you.换句话说,让“类继承”为您做尽可能多的工作。

To answer your question: it's possible to share a variable between the parent and subclasses (as shown above).回答您的问题:可以在父类和子类之间共享变量(如上所示)。

It's also possible to override member variables in the child.也可以覆盖子级中的成员变量。 Look here: Overriding member variables in Java ( Variable Hiding)看这里: 覆盖 Java 中的成员变量(变量隐藏)

It all depends on your design - on your requirements, and on how you choose to implement those requirements.这一切都取决于您的设计——取决于您的要求,以及您选择如何实现这些要求。

You shouldn't create chess for every piece type, there isn't enough difference between them.您不应该为每种棋子类型创建国际象棋,它们之间没有足够的区别。 Use enum instead改用enum

public class ChessPiece {
    private boolean isWhite;
    private ChessPieceType pieceType;

    enum ChessPieceType {
        Pawn(1),
        Knight(2),
        Bishop(3),
        //...
        King(1000);

        private int value;

        private ChessPieceType(int value) { 
            this.value = value; 
        }

        public int getValue() {
            return this.value;
        }
   }

    public ChessPiece (ChessPieceType type, boolean isWhite) {
        this.pieceType = type;
        this.isWhite = isWhite;
    }

    public ChessPieceType getPieceType() {
        return this.pieceType;
    }
}

Uses用途

ChessPiece piece = new ChessPiece(ChessPiece.ChessPieceType.Bishop, true);
ChessPiece.ChessPieceType pieceType = piece.getPieceType();

System.out.println(pieceType); //Bishop
System.out.println(pieceType.getValue()); //3

You can do this, but not the way you're coding it.你可以这样做,但不是你编码的方式。

I would suggest having the method return a type of ChessPiece instead of a String, and then instantiating the type with calling that method and a number.我建议让该方法返回一种 ChessPiece 而不是 String 类型,然后通过调用该方法和一个数字来实例化该类型。

  public class ChessPiece {
private boolean     isWhite;
private int         value;

public ChessPiece (boolean isWhite, int value) {
    this.isWhite = isWhite;
    this.value = value;
}
public ChessPiece getNamebyValue(int value) {
    switch (value) {
      case 1:
        return new Pawn();
        break;
      case 2:
        return new Knight();
      break;
       case 3:
        return new Bishop();
        break;
      case 5:
        return new Rook();
       break;
      case 9:
        return new Queen();
       break;
      case 1000:
        return new King();
      break;
      default:
      return new ChessPiece();
      break;
    }
    return null;
} 

and then....进而....

       ChessPiece cp= new getNameByValue(1000);

That will return an object of type King.这将返回一个 King 类型的对象。

Also, if you don't have break statements in your switch, it will simply return the last value as it goes through each statement until there is a break.此外,如果您的 switch 中没有 break 语句,它只会在遍历每个语句时返回最后一个值,直到出现中断。

如果我理解正确,您可能需要查看工厂设计模式,您将发送一个函数“配方”来创建您的对象(在您的情况下是值)并返回一个适当的对象

In that case change the getNamebyValue() as below:在这种情况下,更改getNamebyValue()如下:

public ChessPiece getNamebyValue() {
    switch (value) {
      case 1:
        return new Pawn();
      case 2:
        return new Knight();
      case 3:
        return new Bishop();
      case 5:
        return new Rook()";
      case 9:
        return new Queen();
      case 1000:
        return new King();
    }
    return null;
  }

and create the subclasses as you shown for Bishop并创建您为Bishop显示的子类

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

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