2013-04-23 4 views
0

나는 무엇인가를 이해하려고 노력하고 있습니다. testClass (z1) 객체의 요점은 무엇입니까?testClass 객체를 만드는 이유

내가 이해하는 방식은 모든 다른 개체의 출발점입니다. 나는 이것이 무엇을 의미하는지 정말로 묻고 있는데, 왜/어떻게 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(); 
} 

감사합니다.

+3

다른 모든 개체는 testClass 개체에서 시작합니까? 나는 당신의 질문을 정말로 이해하지 못합니다. – Keppil

+0

여기에 묻는 일반적인 질문에 따라, 내 대답은 인스턴스 메서드를 호출하는 인스턴스가 필요하다는 것입니다. 그렇지 않으면'main' 메쏘드에서'static' 메쏘드 만 호출 할 수 있습니다. – jlordo

+0

최근에 나는 superClass, 다양한 subClasses (A, B, C)와 testClass를 설정했다. testClass에서 여러 가지 subclass 객체를 만들었다. AClass a1 = new ACLass – user2292173

답변

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() 함수). 기본 생성자에서 모든 private 변수가 초기화 된 다음 배열이 구성되고 마지막으로 showAccounts 메서드가 호출됩니다 (이 메서드는 배열 내용을 인쇄합니다).

1

테스트 클래스는 실제로 자체의 인스턴스를 필요로하지 않습니다. 일반적으로 테스트 클래스는 테스트 할 클래스의 인스턴스입니다. 대개 다른 파일의 클래스를 테스트하고 테스트 할 호출 클래스 인 테스트 파일 작성 및 인스턴스를 테스트합니다.

2

당신의 TestClass의 지점은 표준 출력에 클래스는 BankAccount의 인수 및 방법 결과를 인쇄하여 계정을 테스트하는 것입니다 : (

수업 creditAccount, currentAccount, depositAccount과에서 SavingsAccount이 클래스는 BankAccount를 확장을하는 클래스를 은행 계좌의 계승자).

: 당신이 testBank 클래스를 사용하지 않으려면

, 당신은 또한 다음이를 사용하여 계정을 테스트하는 정보

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

를 인쇄는 BankAccount 클래스의 메소드 인쇄를 만들 수 있습니다

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

나는 상속과 테스트 클래스의 요점을 이해한다. 하지만 왜 테스트 클래스 객체가 필요한지 모르겠지만 어떤 방식 으로든 호출되거나 사용되지는 않지만 내 프로그램은이 클래스없이 실행되지 않습니다. (코드 맨 아래에 (z1)) – user2292173

+0

이 클래스에는 프로그램의 진입 점인 주요 기능이 있기 때문에. –

관련 문제