简体   繁体   English

如何使用与封装相同的方法实现多个子类?

[英]How to implement multiple subclasses with the same methods that use encapsulation?

I want to create a simple game in Java. 我想用Java创建一个简单的游戏。 I'm struggling to understand how to use inheritance to accomplish how to implement subclasses that use encapsulation without needing to write out the same methods in the subclasses. 我正在努力理解如何使用继承来实现如何实现使用封装的子类,而无需在子类中写出相同的方法。

Ideally I'd like to make one base class "character" with a bunch of methods that use encapsulation, so that I can just declare different values for the private members of the subclasses. 理想情况下,我想用一堆使用封装的方法创建一个基类“字符”,这样我就可以为子类的私有成员声明不同的值。 So something like, 所以,像,

public class Character {
private int hitPoints;
public int getHitPoints(){return hitPoints;}
}

And then just declare different values for the variables. 然后只为变量声明不同的值。

public class subCharacter extends Character {
private int hitPoints=100;
//getHitPoints() already inherited and should return 100
}

But to properly get the hit points of the subclass. 但要正确获得子类的生命值。 I have to declare the same method in the subclass to actually get the method to work. 我必须在子类中声明相同的方法以实际使方法工作。

So isn't encapsulation incompatible with inheritance? 那么封装是否与继承不兼容? Is there something basic here I'm misunderstanding or completely overlooking? 这里有什么基本的东西我误解或完全忽视?

You should make the variable hitPoints protected in you Character class, and set it to 100 in the constructor of the subCharacter class. 您应该在Character类中保护变量hitPoints ,并在subCharacter类的构造函数中将其设置为100 There is no need for the declaration of the getHitPoints method in the subclass. 子类中不需要声明getHitPoints方法。 The code would look like this: 代码如下所示:

public class Character {
    protected int hitPoints;
    public int getHitPoints(){return hitPoints;}

}

public class subCharacter extends Character {
    public subCharacter () {
        hitPoints = 100;
    }
}

Example of a subCharacter object: subCharacter对象的示例:

subCharacter sub = new subCharacter();
System.out.println(sub.getHitPoints()); // prints 100

The reason this doesn't work like you think it should is because the subclass's hitpoints field is different from the superclass's hitpoints field. 这不起作用的原因是因为子类的hitpoints字段与超类的hitpoints字段不同。 So while the superclass method is defined, it's trying to refer to a variable that you never actually initialized because it's not the same variable named hitpoints . 因此,虽然定义了超类方法,但它试图引用一个你从未实际初始化的变量,因为它不是名为命中点的变量

As others have already said, you should use the protected access modifier instead of the private access modifier on fields you want to have inherited to a subclass. 正如其他人已经说过的那样,您应该在要继承到子类的字段上使用protected访问修饰符而不是private访问修饰符。

Then again, you probably don't actually need the SubCharacter class to begin with, if this is what you're actually writing for. 然后,你可能实际上并不需要 SubCharacter类开始,如果这是你实际编写的。 You just need to have a constructor that takes a variable argument for hitpoints, or any other field in Character that needs to take different values. 您只需要一个构造函数,该构造函数为命中点或Character中需要采用不同值的任何其他字段获取变量参数。

//I'm not going to reproduce everything.
Character(int hp, String nm, boolean someBooleanThatIJustMadeUpToGetTheConceptAcross){
    hitpoints = hp;
    name = nm;
    randomBoolean = someBooleanThatIJustMadeUpToGetTheConceptAcross;
}

This is not to say, however, that you don't need a superclass/subclass if, say, you're using this Character class for both enemies and player characters, for instance. 但是,这并不是说你不需要超类/子类,比如说,你正在为敌人和玩家角色使用这个Character类。

For an example of when you'd use inheritance... 有关何时使用继承的示例...

public class Circle{
protected int radius;
Circle(){//It's always a good idea to have default constructors, by the way.
        radius = 1;
    }
Circle(int rad){
        radius = rad;
    }
}

public class Wheel extends Circle{
    protected int numspokes;
    Wheel(){
        super(); //Calls the constructor for Circle, instead of reimplementing the wheel.  badpuns++;.
        numspokes = 0;
    }
    Wheel(int rad, int spokes){
         super(rad); //This passes the radius up to the Circle this Wheel also is, so that any calls to this Wheel AS IF IT WAS a Circle, like an array or ArrayList of Circles, will function, which is the point of inheritance.
         numspokes = spokes;
    }
}

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

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