interface BankTransaction
{
void transcation();
}
class Customer
{
protected double rate;
//do something
}
class CurrentAccount extends Customer implements BankTransaction
{
private double x;
// do something
public void transcation()
{
// x is not able to communicate with rate
x = rate * 1 / 100;
}
}
当我这样做时程序工作
interface BankTransaction
{
void transcation(Customer C);
}
class Customer
{
protected double rate;
//do something
}
class CurrentAccount extends Customer implements BankTransaction
{
private double x;
// do something
public void transcation(Customer C)
{
x = C.rate * 1 / 100;
}
}
我知道如果我通过Customer
功能致电transcation
,它就能正常工作。
问题是,为什么CurrentAccount
班级不能与Customer
班级进行沟通,即使我已经扩展了它?
为什么第一个程序不起作用?
设率= 10,我想在费率的帮助下计算x
,答案应该是0.1,但每次都是0。
Java文件:
interface BankTransaction
{
void transcation();
}
class Customer
{
Scanner KB = new Scanner(System.in);
private int code;
private String name;
protected double balance;
void openAccount()
{
System.out.print("Enter Code:");
code = KB.nextInt();
System.out.print("Enter Name:");
Scanner KB = new Scanner(System.in);
name = KB.nextLine();
System.out.print("Enter balance:");
balance = KB.nextDouble();
}
public void showAccount()
{
System.out.println("Code is "+code);
System.out.println("Name is "+name);
System.out.println("Balance is "+balance);
}
}
class CurrentAccount extends Customer implements BankTransaction
{
private double totaltransaction;
private double totaltax;
public void transcation()
{ totaltransaction++;
totaltax=balance*1/100;
}
public void showAccount()
{
System.out.println("Total Tax is "+totaltax);
System.out.println("Total Transaction is "+totaltransaction);
}
}
class Test
{
public static void main(String arg[])
{
Customer C=new Customer();
C.openAccount();
C.showAccount();
BankTransaction B;
CurrentAccount CA=new CurrentAccount();
B=CA;
B.transcation();
CA.showAccount();
}
}
答案 0 :(得分:0)
代码的问题在于主要功能。
class Test {
public static void main(String arg[]) {
Customer C=new Customer();
C.openAccount();
C.showAccount();
BankTransaction B;
CurrentAccount CA=new CurrentAccount();
B=CA;
B.transcation();
CA.showAccount();
}
}
您正在创建的Customer C对象未链接到您正在创建的CurrentAccount CA对象。由于CA继承了Customer对象,因此它还继承了公共openAccount()方法。所以你可以直接在CA对象上调用它。 transcation()方法也是如此。
只需将您的主要功能更改为此功能,即可使用:
class Test {
public static void main(String arg[]) {
CurrentAccount CA=new CurrentAccount();
CA.openAccount();
CA.transcation();
CA.showAccount();
}
}
输出:
Enter Code:123
Enter Name:ABC
Enter balance:10
Total Tax is 0.1
Total Transaction is 1.0
答案 1 :(得分:0)
您的main
方法是您的错误所在:
Customer C=new Customer();
C.openAccount();
C.showAccount();
BankTransaction B;
CurrentAccount CA=new CurrentAccount();
B=CA;
B.transcation();
CA.showAccount();
在这里,您创建了一个Customer
对象和一个CurrentAccount
对象。虽然CurrentAccount
继承自Customer
,但这两个对象完全独立。对一个对象的更改不会影响另一个对象。
这意味着当您致电CA
时,code
的字段(name
,balance
和openAccount
)将无法设置C
。相反,将设置C
个字段。
当您执行B=CA
时,B
和CA
现在引用相同的CurrentAccount
对象。因此,B.transaction
将使用totalTax
对象的字段更改CurrentAccount
对象的CurrentAccount
,这些字段未设置!
要解决此问题,请不要创建额外的Customer
对象,只使用一个CurrentAccount
对象:
CurrentAccount C=new CurrentAccount();
C.openAccount();
C.showAccount();
BankTransaction B;
B=C;
B.transcation();
C.showAccount();