对象不是抽象的,也不会覆盖抽象方法
Object is not abstract and does not override abstract method
如果之前有人问过这个问题,我深表歉意,尽管我确实环顾四周,但找不到适用于我的具体问题的答案。无论如何,我正在尝试为一家销售甜点的商店建模,这些甜点都来自特定的甜点超级class。
当我尝试 运行 程序来测试我的 classes 时,我得到一个错误提示“Cookie
不是抽象的并且没有抽象方法 getPrice()
在 Dessert
public class Cookie extends Dessert
中。我在另一个名为 Fruit 的 class 中遇到了同样的错误,但它或多或少与 Cookie 完全相同具有一些不同的成员变量和方法名称。
这里是甜品超级class:
public abstract class Dessert {
/** Name of the dessert item. */
protected final String name;
/**
* Constructs a new dessert item.
* @param name Name of the dessert.
*/
public Dessert(String name) {
this.name = name;
}
/**
* Get name of the dessert.
* @return dessert name
*/
public final String getName() {
return name;
}
/**
* Get the price of the dessert.
* @return Dessert price
*/
public abstract double getPrice();
}
这是 Cookie 子class:
public class Cookie extends Dessert {
private int number;
private double price;
public Cookie (String name, int number, double price) {
super(name);
this.number = number;
this.price = price;
}
public int getItemCount() {
return number;
}
public double getPricePerDozen() {
return (price / 12) * number;
}
}
我似乎无法正确设置格式,但紧跟在冒号后面的行应该是代码块的一部分。以及块后面的花括号。
在此先感谢您的帮助!
你的Dessert
class有一个抽象方法double getPrice()
,Cookie
扩展了它,所以Cookie
需要实现getPrice()
或者也可以是抽象的,以消除此错误。
代码显然不能在当前状态下编译,但可以这样想——如果我们要实例化一个 Cookie
,它的 double getPrice()
方法是从它的 super class Dessert
,因此该方法将存在以供调用,但它在 Cookie
或 Dessert
中都没有实现,因此调用它的结果将是未指定的。 Java 在编译时看到这一点,因此可以防止您尝试生成定义错误的代码。
因为 Cookie.java
扩展了 Dessert.java
而后者有一个
abstract
方法 getPrice()
Cookie.java
必须为其提供定义。 (除非它本身是抽象的。
如果我们有 OatmealCookie
这样的东西,人们就会这样做
ChocolateChipCookie
那将是确定的 class。)
由于 git 降价问题,我很快重新输入了 material 并得到了
这些要编译。我假设 Cookie
的价格应该只是
每打价格并为其添加了适当的定义:
public abstract class Dessert {
protected final String name;
public Dessert (String name) {
this.name = name;
}
public final String getName() {
return name;
}
public abstract double getPrice ();
}
public class Cookie extends Dessert {
private int number;
private double price;
public Cookie (String name, int number, double price) {
super (name);
this.number = number;
this.price = price;
}
public int getItemCount() {
return number;
}
public double getPricePerDozen() {
return (price/12) * number;
}
public double getPrice() {
return getPricePerDozen();
}
}
如果之前有人问过这个问题,我深表歉意,尽管我确实环顾四周,但找不到适用于我的具体问题的答案。无论如何,我正在尝试为一家销售甜点的商店建模,这些甜点都来自特定的甜点超级class。
当我尝试 运行 程序来测试我的 classes 时,我得到一个错误提示“Cookie
不是抽象的并且没有抽象方法 getPrice()
在 Dessert
public class Cookie extends Dessert
中。我在另一个名为 Fruit 的 class 中遇到了同样的错误,但它或多或少与 Cookie 完全相同具有一些不同的成员变量和方法名称。
这里是甜品超级class:
public abstract class Dessert {
/** Name of the dessert item. */
protected final String name;
/**
* Constructs a new dessert item.
* @param name Name of the dessert.
*/
public Dessert(String name) {
this.name = name;
}
/**
* Get name of the dessert.
* @return dessert name
*/
public final String getName() {
return name;
}
/**
* Get the price of the dessert.
* @return Dessert price
*/
public abstract double getPrice();
}
这是 Cookie 子class:
public class Cookie extends Dessert {
private int number;
private double price;
public Cookie (String name, int number, double price) {
super(name);
this.number = number;
this.price = price;
}
public int getItemCount() {
return number;
}
public double getPricePerDozen() {
return (price / 12) * number;
}
}
我似乎无法正确设置格式,但紧跟在冒号后面的行应该是代码块的一部分。以及块后面的花括号。
在此先感谢您的帮助!
你的Dessert
class有一个抽象方法double getPrice()
,Cookie
扩展了它,所以Cookie
需要实现getPrice()
或者也可以是抽象的,以消除此错误。
代码显然不能在当前状态下编译,但可以这样想——如果我们要实例化一个 Cookie
,它的 double getPrice()
方法是从它的 super class Dessert
,因此该方法将存在以供调用,但它在 Cookie
或 Dessert
中都没有实现,因此调用它的结果将是未指定的。 Java 在编译时看到这一点,因此可以防止您尝试生成定义错误的代码。
因为 Cookie.java
扩展了 Dessert.java
而后者有一个
abstract
方法 getPrice()
Cookie.java
必须为其提供定义。 (除非它本身是抽象的。
如果我们有 OatmealCookie
这样的东西,人们就会这样做
ChocolateChipCookie
那将是确定的 class。)
由于 git 降价问题,我很快重新输入了 material 并得到了
这些要编译。我假设 Cookie
的价格应该只是
每打价格并为其添加了适当的定义:
public abstract class Dessert {
protected final String name;
public Dessert (String name) {
this.name = name;
}
public final String getName() {
return name;
}
public abstract double getPrice ();
}
public class Cookie extends Dessert {
private int number;
private double price;
public Cookie (String name, int number, double price) {
super (name);
this.number = number;
this.price = price;
}
public int getItemCount() {
return number;
}
public double getPricePerDozen() {
return (price/12) * number;
}
public double getPrice() {
return getPricePerDozen();
}
}