【发布时间】:2015-09-22 09:58:35
【问题描述】:
花了一些时间把我的头发拉出来。似乎是黑白的,但我似乎无法让它工作。在我的班级对 java 中的抽象类所说的内容之上进行了大量挖掘,但无济于事。
我正在尝试做的事情:作为作业的一部分(所以请不要大提示,除非它是我的 IDE 或其他东西),我正在制作一个客户类抽象,然后继续制作一些子类.这样,抽象类将通过创建一个利用抽象类方法/属性的子类来实例化。到目前为止和我在一起?
package customer;
abstract class Customer {
private String id;
private String name;
public Customer(String id, String name) {
this.id = id;
this.name = name;
}
//accessors
public double getDiscount(double amt) {
double discount = 0;
return discount;
}
public String getID() {
return this.id;
}
public String name() {
return this.name;
}
}
Abstract Customer 类,看起来都不错,简单,容易。现在是实体子类 RetailCustomer
package customer;
public class RetailCustomer extends Customer {
private double rateOfDiscount = 0.04;
public RetailCustomer(String id, String name, double rate) {
super(id, name);
this.rateOfDiscount = rate;
}
//mutators
public void setDiscount(double rate) {
this.rateOfDiscount = rate;
}
//accessors
public double getDiscount() {
return this.rateOfDiscount;
}
}
好的。再次,非常简单。 RetailCustomer 扩展了 Customer 抽象类,并且应该使用抽象类构造函数,如
所示public RetailCustomer(String id, String name, double rate) {
super(id, name);
this.rateOfDiscount = rate;
}
但是我的 IDE (Eclipse) 显示错误“构造函数 Customer(String,String) 未定义”。即使它显然存在于抽象类中。
注意:只是从 https://docs.oracle.com/javase/tutorial/java/IandI/abstract.html
另外作为一个附加问题(我很可能通过实验来解决):
抽象的客户类实现了一些只是简单访问器的方法。我的理解是,除非这些方法被 retialCustomer 以某种方式实例化,否则对象不会实例化,因为所有方法都需要实现?
提前感谢您提供的任何提示或指示。就像我说的,这对我来说似乎很简单,但与此同时,得到 zilch :(
【问题讨论】:
-
有可能,如果你重新编译客户,它就解决了
-
关于约定的一个小提示:类名(因此是构造函数)应该是驼峰式,首字母大 - 所以是 RetailCustomer。
-
清理并构建项目
-
点击
Project菜单,点击Clean,选择项目并清理。事情应该没问题。 -
发布的代码很好。要么您在添加该构造函数后没有重新构建项目,或者(不太可能)您导入了另一个没有该构造函数的
Customer类。
标签: java abstract-class