Java 子类不继承所有方法
我有一个名为“账户”的课程: 然后我创建了两个不同的子类“PreferredCustomer”和“CommercialCustomer”。这两个类应该继承主“Account”类的所有方法(存款、取款、月利率以及所有getter和setter)。与子类的唯一区别在于,它们有一个预先确定的利率Java 子类不继承所有方法,java,class,inheritance,subclass,super,Java,Class,Inheritance,Subclass,Super,我有一个名为“账户”的课程: 然后我创建了两个不同的子类“PreferredCustomer”和“CommercialCustomer”。这两个类应该继承主“Account”类的所有方法(存款、取款、月利率以及所有getter和setter)。与子类的唯一区别在于,它们有一个预先确定的利率 public class PreferredCustomer extends Account { public double annualInterestRate; public Pref
public class PreferredCustomer extends Account {
public double annualInterestRate;
public PreferredCustomer() {
}
public PreferredCustomer(int id, double balance) {
super();
this.annualInterestRate = .04;
}
} //end PreferredCustomer Class
我有一种感觉,我目前的设置方式是不准确的。测试时,取款和存款方法有效,但尽管输入了20000美元的起始余额,它仍然将起始余额设置为0美元,并且不计算利率
我正在测试这个类:
public class TestingAccountClass {
public static void main(String[] args) {
//Create accounts
CommercialCustomer myCommercialCustomerAccount = new CommercialCustomer(1124,
20000.00);
//Invoking deposit method from account class
myCommercialCustomerAccount.deposit(3000.00);
//Display account balance, monthly interest, and date created
System.out.println("\n\n----Commercial Account---");
System.out.println("Account Created On: "
+ myCommercialCustomerAccount.getDateCreated());
System.out.printf("Balance: $%.2f", myCommercialCustomerAccount.getBalance());
System.out.printf("\nMonthly Interest: $%.2f"
,myCommercialCustomerAccount.getMonthlyInterestRate());
当以这种方式测试该类时,deposit方法起作用,但是account类中的任何其他方法(除了Draw)似乎都不起作用。如有任何建议,将不胜感激。谢谢大家! 在
首选客户中
没有设置余额的机制;您正在忽略balance
构造函数参数。您没有直接分配balance
实例变量,也没有调用超类构造函数。所以余额是0
在
PreferredCustomer
构造函数中,调用设置余额的超类构造函数,或者在构造函数本身中设置它,或者在PreferredCustomer
中调用setBalance没有设置余额的机制;您正在忽略balance
构造函数参数。您没有直接分配balance
实例变量,也没有调用超类构造函数。所以余额是0
在PreferredCustomer
构造函数中,调用设置余额的超类构造函数,或者在构造函数中设置余额,或者调用setBalance
执行以下操作:
CommercialCustomer myCommercialCustomerAccount = new CommercialCustomer(1124, 20000.00);
但是,
public PreferredCustomer(int id, double balance) {
super();
this.annualInterestRate = .04;
}
你不能用天平做任何事
您可以将其更改为:
public PreferredCustomer(int id, double balance) {
super();
this.balance = balance;
this.annualInterestRate = .04;
}
但您将向余额写入两次
此外,使用两个同名变量(基变量与子变量)->annualInterestate
也是一个坏主意
编辑-------------------------------------------编辑
我建议您做如下操作:
public Account() {
this(0, 0d, 0d);
}
public Account(int id, double balance, double interestRate) {
this.id = id;
this.balance = balance;
this.annualInterestRate = interestRate;
}
public PreferredCustomer(int id, double balance) {
super(id, balance, 0.04d);
}
EDIT2-------------------------------------------EDIT2
这是错误的。你在做整数除法
return (balance * annualInterestRate) / 12;
更改为:
return (balance * annualInterestRate) / 12d;
或者这个:
return (balance * annualInterestRate) / 12.0;
你可以:
CommercialCustomer myCommercialCustomerAccount = new CommercialCustomer(1124, 20000.00);
但是,
public PreferredCustomer(int id, double balance) {
super();
this.annualInterestRate = .04;
}
你不能用天平做任何事
您可以将其更改为:
public PreferredCustomer(int id, double balance) {
super();
this.balance = balance;
this.annualInterestRate = .04;
}
但您将向余额写入两次
此外,使用两个同名变量(基变量与子变量)->annualInterestate
也是一个坏主意
编辑-------------------------------------------编辑
我想推荐如下:
public Account() {
this(0, 0d, 0d);
}
public Account(int id, double balance, double interestRate) {
this.id = id;
this.balance = balance;
this.annualInterestRate = interestRate;
}
public PreferredCustomer(int id, double balance) {
super(id, balance, 0.04d);
}
EDIT2-------------------------------------------EDIT2
这是错误的。你在做整数除法
return (balance * annualInterestRate) / 12;
更改为:
return (balance * annualInterestRate) / 12d;
或者这个:
return (balance * annualInterestRate) / 12.0;
我认为问题就在这里:
public PreferredCustomer(int id, double balance) {
super();
this.annualInterestRate = .04;
}
您不应该在super()调用中添加一些内容吗?(默认值)我认为问题出在这里:
public PreferredCustomer(int id, double balance) {
super();
this.annualInterestRate = .04;
}
您不应该在super()调用中添加一些内容吗?(默认值)警报
对于开始,您从不公开非最终成员变量。我差点心脏病发作
你打字的时候
this.foo
作用域继续向上爬升固有树,直到找到可访问的成员。所以
此-->super-->super.super-->super.super.super--。。。等
我可以告诉你如何在语法上解决你的问题,或者告诉你如何做得更好
我选择后者
申报
public abstract double getAnnualInterestRate();
在你的基础课上
然后更改getMonthlyInterestate的实现(调用此新方法)
在您的子类中,只需实现这个抽象方法并返回您的利率
这将允许您以多态方式改变速率,并使您的实现经得起未来的考验。函数可以做任何事情来产生它们的返回值,其中作为成员变量的只是一点数据,而不是更多
并且,请让您的所有成员变量保持私有状态警惕
对于开始,您从不公开非最终成员变量。我差点心脏病发作
你打字的时候
this.foo
作用域继续向上爬升固有树,直到找到可访问的成员。所以
此-->super-->super.super-->super.super.super--。。。等
我可以告诉你如何在语法上解决你的问题,或者告诉你如何做得更好
我选择后者
申报
public abstract double getAnnualInterestRate();
在你的基础课上
然后更改getMonthlyInterestate的实现(调用此新方法)
在您的子类中,只需实现这个抽象方法并返回您的利率
这将允许您以多态方式改变速率,并使您的实现经得起未来的考验。函数可以做任何事情来产生它们的返回值,其中作为成员变量的只是一点数据,而不是更多
并且,请将所有成员变量设为私有首选客户
不应定义年度利率策略
-它继承帐户的年度利率策略
。此外,PreferredCustomer
此时应该调用super(id,balance.04)
如果我取出年度利率代码,它仍然不会吐出正确的余额:(问题是,子类需要有自己的利率,而不是继承主类的利率。这不可能吗?PreferredCustomer
不应该定义annualinterestate
——它继承帐户的annualinterestate
。此外,PreferredCustomer
应该调用super(id,balance.04)
目前,如果我取出AnnualInterestate代码,它仍然不起作用