gpt4 book ai didi

java - 为什么要创建一个testClass对象

转载 作者:行者123 更新时间:2023-12-01 23:52:06 24 4
gpt4 key购买 nike

我只是想了解一些事情。testClass (z1) 对象有什么意义?

我的理解是,它是所有其他对象的起点。我真的在问这意味着什么,为什么/如何测试类需要它自身的实例?还有其他方法可以达到相同的结果吗?

代码如下:-

public class testBank {  

creditAccount a1 = new creditAccount("Mary Chapple", 2400.41);
creditAccount a2 = new creditAccount("Jim Smith", 2.56);
creditAccount a3 = new creditAccount("Henry A Jones", 700.89);
currentAccount b1 = new currentAccount("Simon Hopkins", 86.01);
currentAccount b2 = new currentAccount("Jack C Whitheridge", 40000.29);
currentAccount b3 = new currentAccount("Bill Sutton", 100.23);
depositAccount c1 = new depositAccount("Theo Gibson", 145.99);
depositAccount c2 = new depositAccount("Jasper Williams", 3000.29);
depositAccount c3 = new depositAccount("Julie Banks", 1000001.99);
savingsAccount d1 = new savingsAccount("Burnard White", 2400.42);
savingsAccount d2 = new savingsAccount("Richard Bennett", 203.16);
savingsAccount d3 = new savingsAccount("Bob Robinson", 10000.11);





public testBank()
//Create an array of objects.//
{
bankAccount[]theAccounts = {a1,a2,a3,b1,b2,b3,c1,c2,c3,d1,d2,d3};

showAccounts (theAccounts);
}
private void showAccounts(bankAccount[] aa)
{
for (int i = 0;i <aa.length;i++)
{


System.out.println("Account Holder: " +aa[i].getAccountName());
System.out.println("Balance = £" +aa[i].getBalance());
System.out.println("Balance pluss APR = £" +aa[i].calculateInterest());


}
}

public static void main(String[]args)
{

testBank z1 = new testBank();
}

感谢您的帮助。

最佳答案

testClass 的重点是通过在标准输出上打印类bankAccount 的参数和方法结果来测试您的帐户:

您的creditAccount、currentAccount、depositAccount 和 savingAccount 类扩展了bankAccount 类(这些类继承了bankAccount 类)。

如果您不想使用 testBank 类,您还可以在bankAccount 类中创建一个 print 方法来打印这些信息

public void print ()
{
System.out.println("Account Holder: " + this.getAccountName());
System.out.println("Balance = £" + this.getBalance());
System.out.println("Balance pluss APR = £" + this.calculateInterest());
}

然后您将使用以下方法测试您的帐户:

public static void main(String[]args)
{
creditAccount a1 = new creditAccount("Mary Chapple", 2400.41);
currentAccount b1 = new currentAccount("Simon Hopkins", 86.01);
depositAccount c1 = new depositAccount("Theo Gibson", 145.99);
savingsAccount d1 = new savingsAccount("Burnard White", 2400.42);
a1.print();
b1.print();
c1.print();
d1.print();
}

关于java - 为什么要创建一个testClass对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16176661/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com