答案:A
错误:1.右上处,调用有参构造方法错误,因为MyClass(int value)方法为default权限类型;
2.右上处,调用value时错误,value为private;
3.左下处,调用value时错误,value为private;
修改:1.把程序改到相同包中;
2和3使用get方法替代value变量;
package work;
//银行账户
public class Account {
private long Id;//账户
private double balance;//账户余额
private String password;//账户密码
Account(long Id,double balance,String password)
{
this.Id=Id;
this.balance=balance;
this.password=password;
}
public long getId() {
return Id;
}
public double getBalance() {
return balance;
}
public void setBalance(double balance) {
this.balance = balance;
}
public String getPassword() {
return null;
}
public void setPassword(String password) {
this.password = password;
}
}
package work;
//储蓄账户
public class SavingAccount extends Account{
private double interesrtRate;//存款利率
SavingAccount(long Id,double balance,String password,double interesrtRate)
{
super(Id,balance,password);
this.interesrtRate=interesrtRate;
}
public double getInteresrtRate() {
return interesrtRate;
}
public void setInteresrtRate(double interesrtRate) {
if(interesrtRate<0.1&&interesrtRate>0)
this.interesrtRate = interesrtRate;
else
System.out.println("利率范围不正确,不予修改");
}
public void setPassword(String password)//修改
{
if(password.length()==6)
super.setPassword(password);
else
System.out.println("不予修改");
}
}
package work;
//信用账户
public class CreditAccount extends Account{
private double creditLine;//信用额度
CreditAccount(long Id,double balance,String password,double creditLine)
{
super(Id,balance,password);
this.creditLine=creditLine;
}
public double getCreditLine() {
return creditLine;
}
public void setCreditLine(double creditLine) {
this.creditLine = creditLine;
}
public void setPassword(String password)//修改
{
if(password.length()==6)
super.setPassword(password);
else
System.out.println("不予修改");
}
}
package work;
//测试类
public class Text {
public static void main(String[] args) {
Account a=new Account(123,123,"123");//Account类
System.out.println(a.getBalance());
System.out.println(a.getId());
System.out.println(a.getPassword());
System.out.println();
SavingAccount s=new SavingAccount(123,123,"123",0.1);//SavingAccount类
System.out.println(s.getBalance());
System.out.println(s.getId());
System.out.println(s.getPassword());
System.out.println(s.getInteresrtRate());
s.setPassword("1234");
s.setInteresrtRate(0.2);
System.out.println();
CreditAccount c=new CreditAccount(123,123,"123",0.9);//CreditAccount类
System.out.println(c.getBalance());
System.out.println(c.getId());
System.out.println(c.getPassword());
c.setCreditLine(0.5);
System.out.println(c.getCreditLine());
c.setPassword("123");
}
}