如何使用封装的相同方法实现多个子类?
How to implement multiple subclasses with the same methods that use encapsulation?
我想在 Java 中创建一个简单的游戏。
我正在努力理解如何使用继承来完成如何实现使用封装的子classes,而不需要在子classes.
中写出相同的方法
理想情况下,我想用一堆使用封装的方法创建一个基础class "character",这样我就可以为子[=的私有成员声明不同的值24=]es。所以像,
public class Character {
private int hitPoints;
public int getHitPoints(){return hitPoints;}
}
然后只需为变量声明不同的值。
public class subCharacter extends Character {
private int hitPoints=100;
//getHitPoints() already inherited and should return 100
}
但要正确获得潜艇的生命值class。我必须在 subclass 中声明相同的方法才能使该方法真正起作用。
那么封装和继承不是不兼容吗?我误解或完全忽略了这里的基本内容吗?
你应该在Character
class中保护变量hitPoints
,并在subCharacter
[=]的构造函数中将其设置为100
21=]。 subclass中的getHitPoints
方法不需要声明。代码如下所示:
public class Character {
protected int hitPoints;
public int getHitPoints(){return hitPoints;}
}
public class subCharacter extends Character {
public subCharacter () {
hitPoints = 100;
}
}
子字符对象示例:
subCharacter sub = new subCharacter();
System.out.println(sub.getHitPoints()); // prints 100
这不像你认为的那样工作的原因是子class的hitpoints
字段与超级class的hitpoints
场地。因此,虽然定义了 superclass 方法,但它试图引用一个您从未实际初始化的变量,因为它 与名为 hitpoints.
的变量不同
正如其他人已经说过的,您应该在要继承给子 class 的字段上使用 protected
访问修饰符而不是 private
访问修饰符。
话又说回来,你可能实际上并不需要 SubCharacter
class 开始,如果这是你真正写的.您只需要有一个构造函数,该构造函数为生命值或 Character
中需要采用不同值的任何其他字段采用可变参数。
//I'm not going to reproduce everything.
Character(int hp, String nm, boolean someBooleanThatIJustMadeUpToGetTheConceptAcross){
hitpoints = hp;
name = nm;
randomBoolean = someBooleanThatIJustMadeUpToGetTheConceptAcross;
}
然而,这并不是说您不需要 superclass/subclass,如果您对敌人和玩家角色都使用 Character
class ,例如。
关于何时使用继承的示例...
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;
}
}
我想在 Java 中创建一个简单的游戏。 我正在努力理解如何使用继承来完成如何实现使用封装的子classes,而不需要在子classes.
中写出相同的方法理想情况下,我想用一堆使用封装的方法创建一个基础class "character",这样我就可以为子[=的私有成员声明不同的值24=]es。所以像,
public class Character {
private int hitPoints;
public int getHitPoints(){return hitPoints;}
}
然后只需为变量声明不同的值。
public class subCharacter extends Character {
private int hitPoints=100;
//getHitPoints() already inherited and should return 100
}
但要正确获得潜艇的生命值class。我必须在 subclass 中声明相同的方法才能使该方法真正起作用。
那么封装和继承不是不兼容吗?我误解或完全忽略了这里的基本内容吗?
你应该在Character
class中保护变量hitPoints
,并在subCharacter
[=]的构造函数中将其设置为100
21=]。 subclass中的getHitPoints
方法不需要声明。代码如下所示:
public class Character {
protected int hitPoints;
public int getHitPoints(){return hitPoints;}
}
public class subCharacter extends Character {
public subCharacter () {
hitPoints = 100;
}
}
子字符对象示例:
subCharacter sub = new subCharacter();
System.out.println(sub.getHitPoints()); // prints 100
这不像你认为的那样工作的原因是子class的hitpoints
字段与超级class的hitpoints
场地。因此,虽然定义了 superclass 方法,但它试图引用一个您从未实际初始化的变量,因为它 与名为 hitpoints.
正如其他人已经说过的,您应该在要继承给子 class 的字段上使用 protected
访问修饰符而不是 private
访问修饰符。
话又说回来,你可能实际上并不需要 SubCharacter
class 开始,如果这是你真正写的.您只需要有一个构造函数,该构造函数为生命值或 Character
中需要采用不同值的任何其他字段采用可变参数。
//I'm not going to reproduce everything.
Character(int hp, String nm, boolean someBooleanThatIJustMadeUpToGetTheConceptAcross){
hitpoints = hp;
name = nm;
randomBoolean = someBooleanThatIJustMadeUpToGetTheConceptAcross;
}
然而,这并不是说您不需要 superclass/subclass,如果您对敌人和玩家角色都使用 Character
class ,例如。
关于何时使用继承的示例...
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;
}
}