抽象数据类型问题

发布于 2024-12-13 02:27:51 字数 638 浏览 0 评论 0原文

我对抽象数据类型的概念相当陌生,并且正在寻求澄清,因为我在网上找不到任何好的示例。

根据我的理解,子类继承了抽象的所有方法和变量,但我认为我误解了这一点。例如,我正在使用抽象数据类型 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

仙女山的月亮 2024-12-20 02:27:51

有几个问题:

  1. itemName 等都是私有,因此即使它们被继承,它们也不可见 em> 到子类。
  2. 您在 Hamburger 中使用的语法无效。

以下是修复代码的方法:

public abstract class MenuItem{

  public MenuItem(String itemName, int ct, double costPer) {
    this.itemName = itemName;
    this.ct = ct;
    this.costPer = costPer;
  }
  ...
}

public class Hamburger extends MenuItem{

  public Hamburger() {
    super("Hamburger", 0, 4.99)
  }

}  

最后,我想说,最好不要使用抽象基类和一堆具体类,而是为 MenuItem 使用单个具体类,并且制作该类的 Hamburger实例

There are several issues:

  1. itemName et al are private, so even though they're inherited, they're not visible to the subclass.
  2. The syntax you use in Hamburger is invalid.

Here is how you could fix your code:

public abstract class MenuItem{

  public MenuItem(String itemName, int ct, double costPer) {
    this.itemName = itemName;
    this.ct = ct;
    this.costPer = costPer;
  }
  ...
}

public class Hamburger extends MenuItem{

  public Hamburger() {
    super("Hamburger", 0, 4.99)
  }

}  

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 make Hamburger etc instances of that class.

忆伤 2024-12-20 02:27:51

问题在于 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

对岸观火 2024-12-20 02:27:51

根据我的理解,子类继承了抽象的所有方法和变量,但我认为我误解了这一点

是的,您对误解的理解是正确的。 :-)

java中的子类不继承私有成员变量。他们只获得公共和受保护的成员。

From my understanding, the sub class inherits all methods and variables from the abstract but I think I am misunderstanding this

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.

擦肩而过的背影 2024-12-20 02:27:51

itemNamecostPerct 被声明为私有访问字段。它们只能从定义它们的类中访问。如果您使用受保护的访问权限声明它们,您将能够访问它们。

正如 Java 语言规范中所定义的 6.6 访问控制< /a>

引用(类、接口或数组)类型的成员(类、接口、字段或方法)或类类型的构造函数仅当类型可访问且成员或构造函数声明为允许访问:

  • ...
  • (否则,)如果成员或构造函数被声明为私有,则当且仅当访问发生在包含成员或构造函数声明的顶级类(第 7.6 节)的主体内时,才允许访问。
  • ...

itemName, costPer and ct are declared as private access fields. They are only accessible from within the class they are defined in. If you declare them with protected access, you'll be able to access them.

As defined in the Java Language Specification, section 6.6 Access Control

A member (class, interface, field, or method) of a reference (class, interface, or array) type or a constructor of a class type is accessible only if the type is accessible and the member or constructor is declared to permit access:

  • ...
  • (Otherwise,) if the member or constructor is declared private, then access is permitted if and only if it occurs within the body of the top level class (§7.6) that encloses the declaration of the member or constructor.
  • ...
捂风挽笑 2024-12-20 02:27:51

在开始研究抽象类型之前,请从封装的概念开始,并尝试理解它,因为它被(许多人)认为是面向对象设计中最重要的概念,其次是多态性和继承。如果类成员是私有的,则任何子类都无法直接访问它们。

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.

八巷 2024-12-20 02:27:51

是的,Hamburger 仅继承方法。那是因为它们是公开的。如果您将它们设为私有(如字段),它们也不会被继承。以下是解决问题的方法。

import javax.swing.*;

public abstract class MenuItem {

    //To be visible to subclasses, these need to be public, package-private, or protected
    protected String itemName;
    protected int ct;
    protected double costPer;

    public String getItemName() {
        return itemName;
    }

    public int getCt() {
        return ct;
    }

    public double getCostPer() {
        return costPer;
    }

}

public class Hamburger extends MenuItem {

    //These assignments need to be inside a block, like a constructor
    public Hamburger() {
        itemName = "Hamburger";
        ct = 0;
        costPer = 4.99;
    }

}  

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.

import javax.swing.*;

public abstract class MenuItem {

    //To be visible to subclasses, these need to be public, package-private, or protected
    protected String itemName;
    protected int ct;
    protected double costPer;

    public String getItemName() {
        return itemName;
    }

    public int getCt() {
        return ct;
    }

    public double getCostPer() {
        return costPer;
    }

}

public class Hamburger extends MenuItem {

    //These assignments need to be inside a block, like a constructor
    public Hamburger() {
        itemName = "Hamburger";
        ct = 0;
        costPer = 4.99;
    }

}  
送你一个梦 2024-12-20 02:27:51

抽象类永远不能被实例化。它的唯一目的是扩展。在抽象类中,如果您将至少一个方法指定为抽象,则需要将整个类指定为抽象。抽象类允许您在同一个类中实现和未实现(抽象)方法。如果类中的所有方法都是抽象的,那么您实际上拥有一个接口,并且接口中声明的任何变量都被视为常量。您问题中的变量不会被继承,因为它们是抽象类的私有变量。您必须通过抽象类的方法访问它们。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文