0

我只是想理解一些事情。testClass (z1) 对象的意义何在?

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

下面的代码: -

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();
}

谢谢你的帮助。

4

3 回答 3

2

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();
}
于 2013-04-23T19:22:12.783 回答
1

测试类实际上并不需要它自己的实例,它通常需要的是您正在测试的任何类的实例。通常你会从不同的文件中测试一个类,并在测试文件中创建和调用被测试类的实例。

于 2013-04-23T19:18:45.217 回答
0

testBank() 方法是一个构造函数。当您创建一个新的 testBank 实例时,将调用此函数。

您应该使用此方法来初始化您的不同变量(a1、a2 等...)。

public class testBank
{  
    private creditAccount a1;
    private creditAccount a2;
    private creditAccount a3;
    private currentAccount b1;
    private currentAccount b2;
    private currentAccount b3;
    private depositAccount c1;
    private depositAccount c2;          
    private depositAccount c3;      
    private savingsAccount d1;
    private savingsAccount d2;
    private savingsAccount d3;

    public testBank()
        //Create an array of objects.//
    {
        this.a1 = new creditAccount("Mary Chapple", 2400.41);
        this.a2 = new creditAccount("Jim Smith", 2.56);
        this.a3 = new creditAccount("Henry A Jones", 700.89);
        this.b1 = new currentAccount("Simon Hopkins", 86.01);
        this.b2 = new currentAccount("Jack C Whitheridge", 40000.29);
        this.b3 = new currentAccount("Bill Sutton", 100.23);
        this.c1 = new depositAccount("Theo Gibson", 145.99);
        this.c2 = new depositAccount("Jasper Williams", 3000.29);          
        this.c3 = new depositAccount("Julie Banks", 1000001.99);      
        this.d1 = new savingsAccount("Burnard White", 2400.42);
        this.d2 = new savingsAccount("Richard Bennett", 203.16);
        this.d3 = new savingsAccount("Bob Robinson", 10000.11);
        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();
    }
}

那么,那里发生了什么?

当您打电话时,testBank z1 = new testBank();您正在创建 testBank 类的新实例。因此,调用了默认构造函数(testBank() 函数)。在您的默认构造函数中,所有私有变量都被初始化,然后构造一个数组,最后调用 showAccounts 方法(该方法打印数组内容)。

于 2013-04-23T19:59:31.987 回答