如何解决抽象类中的实例变量 ****Eclipse 发生故障,这个问题毫无意义****
因此,在课堂上的挑战中,我们不得不使用 public abstract Class Cycle
作为父类并从中创建子类。我以 Unicycle Class
为例。
我的教授拒绝让我将 Color color
设为受保护。他想要私人的。他说我解决隐私问题的方法是实现颜色的 getter 和 setter。最初它们被设置为 abstract Color getColor()
和 abstract void setColor(Color color)
,但我尝试通过将它们公开并为它们提供方法主体来在抽象类中实现它们。
代码中的测试是:
cycle.setColor(Color.RED);
assertEquals(Color.RED,cycle.getColor());
我继续收到错误消息,
The field Cycle.color is not visible
我知道它适用于受保护,但我必须使用私有。 任何人都可以在这里向我抛出任何提示?我对所有的研究和失败的试验都快疯了。
public abstract class Cycle
{
//Declare instance variables
private String make;
private Color color;
//Create a constructor that only contains an argument for make
public Cycle(String make)
{
this.make = make;
}
//Create a constructor that contains an argument for make and color
public Cycle(String make,Color color)
{
this.make = make;
this.color = color;
}
//Create getter and setter methods
abstract int getNumberOfWheels();
//*********Was abstract Color getColor();
public Color getColor()
{
return color;
}
//*********Was abstract void setColor(Color color);
public void setColor(Color color)
{
this.color = color;
}
final String getMake()
{
//return the make of the object
return make;
}
独轮车课
public class Unicycle extends Cycle
{
//Create a constructor that only holds the argument make
public Unicycle(String make)
{
//Call on the super (parent) class to create the object with arguments
super(make);
}
public Unicycle(String make,Color color)
{
super(make,color);
}
//Create a method to get the number of wheels and return 1 since a unicycle only has 1 wheel
public int getNumberOfWheels()
{
return 1;
}
}
解决方法
color
对子类不可见,因为 color
在 Cycle
中是私有的,因此 Unicycle
类中的 getter/setter 会导致编译问题。
Cycle
已经为 color
和 Unicycle
is-a Cyle
定义了 getter/setter,因此无需尝试覆盖子类中的 getter/setter。
请记住,在基类中定义的任何公共(或受保护)方法都可用于子类。这是使用继承的好处之一。
,我的教授拒绝让我将 Color 颜色视为受保护的颜色。 他想要 它是私人的。他说我可以解决隐私问题的方式 out 是通过实现颜色的 getter 和 setter 来实现的。
如果你想知道他是不是很难,我可以告诉你,他正试图教你一个非常重要的面向对象编程概念。在这种情况下,这限制了变量的范围。您永远不想直接访问类的数据成员,除非它们是常量。这样做的原因有几个,其中之一是您将来需要在返回值之前添加初步步骤(即从备用源返回值)。
现在,你有这样的事情:
public abstract class Parent {
private String something;
protected Parent() {
something = "N/A";
}
protected String getSomething () {
return something;
}
protected void setSomething (String something) {
this.something = something;
}
}
public class Child extends Parent {
// bad use of override
@Override
public void setSomething (String something) {
super.setSomething(something);
}
// bad use of override
@Override
public String getSomething() {
return something;
}
public static void main (String[] args) {
Child child = new Child();
child.setSomething("New value");
System.out.println(child.getSomething());
}
}
public class Unrelated {
public static void main (String[] args) {
Parent child = new Child();
child.setSomething("Foo");
System.out.println(child.getSomething());
}
}
这有效....抽象类中的字段是私有的。因此,它被阻止直接操作。子类可以覆盖抽象(父)类的 protected
方法,并将其公开以供无关类自由调用。我在子类和不相关的类中都包含了一个 main 方法来说明这一点。
override
不好的原因是因为它没有做任何事情......但是,受保护的方法被限制在包外或由与声明受保护方法的类无关的类调用。因此,如果不相关的类在包之外,它将无法调用这些受保护的方法。因此,您必须通过子类覆盖它们并将它们公开。也就是说,如果是这种情况,您可能会争辩说,最好的办法是在父类中公开受保护的方法,并避免仅仅因为这个原因就强制实现类覆盖受保护的方法。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。