抽象数据类型问题
我对抽象数据类型的概念相当陌生,并且正在寻求澄清,因为我在网上找不到任何好的示例。
根据我的理解,子类继承了抽象的所有方法和变量,但我认为我误解了这一点。例如,我正在使用抽象数据类型 MenuItem 创建一个菜单,
import javax.swing.*;
public abstract class MenuItem{
private String itemName;
private int ct;
private double costPer;
public String getItemName()
{
return itemName;
}
public int getCt()
{
return ct;
}
public double getCostPer()
{
return costPer;
}
}
public class Hamburger extends MenuItem{
itemName = "Hamburger";
ct = 0;
costPer = 4.99;
}
我知道这是不正确的,但有人可以告诉我为什么吗?子类hamburger是只继承方法还是什么?
I am fairly new to the concept of abstract data types an was looking for clarification because I could not find any good examples online.
From my understanding, the sub class inherits all methods and variables from the abstract but I think I am misunderstanding this. For example, I am creating a menu using the abstract data type MenuItem
import javax.swing.*;
public abstract class MenuItem{
private String itemName;
private int ct;
private double costPer;
public String getItemName()
{
return itemName;
}
public int getCt()
{
return ct;
}
public double getCostPer()
{
return costPer;
}
}
public class Hamburger extends MenuItem{
itemName = "Hamburger";
ct = 0;
costPer = 4.99;
}
I know this is incorrect but can someone tell me why? Does the subclass hamburger only inherit the methods or what?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
有几个问题:
itemName
等都是私有
,因此即使它们被继承,它们也不可见 em> 到子类。Hamburger
中使用的语法无效。以下是修复代码的方法:
最后,我想说,最好不要使用抽象基类和一堆具体类,而是为
MenuItem
使用单个具体类,并且制作该类的Hamburger
等实例。There are several issues:
itemName
et al areprivate
, so even though they're inherited, they're not visible to the subclass.Hamburger
is invalid.Here is how you could fix your code:
Finally, I'd say that instead of using an abstract base class and a bunch of concrete classes, it would be better to use a single concrete class for
MenuItem
and makeHamburger
etc instances of that class.问题在于
MenuItem
父类中字段的可见性。私有
可见性意味着它们对任何其他类(包括自己的子类)都不可见。为了使您的文件对子类可见,您必须将其可见性更改为
protected
。请注意,这也会使这些字段对同一包中的所有类都可见。本文中更详细地介绍了所有成员可见性问题< /strong>
The problem lies in the visibility of the fields in your
MenuItem
parent class.private
visibility means, that they are not visible to any other class including own subclasses.In oreder to make your fileds visible to subclasses, you have to change their visibility to
protected
. Be aware that this makes the fields visible to all classes in the same package as well.All the memeber visibility issues are covered in greater detail in this article
是的,您对误解的理解是正确的。 :-)
java中的子类不继承私有成员变量。他们只获得公共和受保护的成员。
yes, your understanding about your mis-understanding is correct. :-)
sub classes in java do not inherit the private member variables. they get public and protected members only.
itemName
、costPer
和ct
被声明为私有访问字段。它们只能从定义它们的类中访问。如果您使用受保护的访问权限声明它们,您将能够访问它们。正如 Java 语言规范中所定义的 6.6 访问控制< /a>
itemName
,costPer
andct
are declared as private access fields. They are only accessible from within the class they are defined in. If you declare them withprotected
access, you'll be able to access them.As defined in the Java Language Specification, section 6.6 Access Control
在开始研究抽象类型之前,请从封装的概念开始,并尝试理解它,因为它被(许多人)认为是面向对象设计中最重要的概念,其次是多态性和继承。如果类成员是私有的,则任何子类都无法直接访问它们。
Before you start looking into abstract types, start with the concept of encapsulation, and try to understand it as it is considered (by many) as the most important concept in the object-oriented design, followed by polymorphism, and inheritance. If class members are private, no subclass will be able to access them directly.
是的,
Hamburger
仅继承方法。那是因为它们是公开的。如果您将它们设为私有(如字段),它们也不会被继承。以下是解决问题的方法。Yes,
Hamburger
only inherits the methods. That's because they're public. If you made them private (like the fields) they wouldn't be inherited either. Here's how to fix the problems.抽象类永远不能被实例化。它的唯一目的是扩展。在抽象类中,如果您将至少一个方法指定为抽象,则需要将整个类指定为抽象。抽象类允许您在同一个类中实现和未实现(抽象)方法。如果类中的所有方法都是抽象的,那么您实际上拥有一个接口,并且接口中声明的任何变量都被视为常量。您问题中的变量不会被继承,因为它们是抽象类的私有变量。您必须通过抽象类的方法访问它们。
An abstract class can never be instantiated. Its sole purpose is to be extended. In an abstract class, if you specify atleast one method as abstract, then the whole class needs to be specified as abstract. An abstract class allows you to have implemented and unimplemented (abstract) methods all in the same class. If all methods in the class are abstract, then you effectively have a interface, and any variables declared in an interface are treated as constants. The variables in your question are not inherited as they are private to the abstract class. You must access them through the methods of the abstract class.