2015-09-26 77 views
0

我的Java包中有四個類。只有一個類有main()方法。從另一個類運行類的方法

當我運行main()方法時,如何運行四個類中的所有方法?

這裏是我的課:

Cat1.java

package test; 

public class Cat1 { 

    public Cat1() { 
     System.out.println("100"); 
    } 

    public Cat1(int i){ 
     i = 10; 
     System.out.println(" cat1 i :"+ i); 
    } 
} 

Cat2.java

package test; 

public class Cat2 extends Cat1 { 

    public Cat2() { 
     System.out.println("200"); 
    } 

    public Cat2(int i){ 
     super(); 
     System.out.println("Cat 2 ; "+ i); 
    } 
} 

Cat3.java

package test; 

public class Cat3 extends Cat2{ 

    public Cat3() { 
     System.out.println("300"); 
    } 

    public Cat3(int i){ 
     super(); 
     System.out.println("Cat 3 : "+ i); 
    } 

} 

Sample.java

package test; 

import java.text.DateFormat; 
import java.util.Calendar; 
import java.util.Locale; 

public class Sample { 

    /** 
    * @param args 
    */ 
    public static void main(String[] args) { 

     Calendar cal = Calendar.getInstance(); 

     //Date d = cal.getTime(); 
     Locale locale = new Locale("en"); 
     DateFormat dateFormat = DateFormat.getDateInstance(DateFormat.FULL, locale); 
     System.out.println(" LONg date : " + dateFormat.format(cal.getTime())); 

     String incomeData = "200,2,2,2,2"; 


     int expense = 0; 
     String expenseSplit = incomeData; 
     System.out.println(" e"); 
     String expenseArray[] = expenseSplit.split(","); 
     String income = expenseArray[0]; 
     for (int i = 1; i < expenseArray.length; i++) { 
      expense = expense+Integer.parseInt(expenseArray[i]); 
     } 
     System.out.println(" Income "+ income); 
     System.out.println(" expense "+ expense); 


    } 

} 
+0

你想究竟如何運行呢?從'main'方法開始順序? –

+3

您在'Cat *'類中沒有任何方法。 –

+0

@TAsk我同意 - 他們只是構造函數。 –

回答

0

看起來像你剛纔說的新對象,並調用你所需要的功能。

Cat3 cat3 = new Cat3(); 

或者你想自動調用,當main()方法運行?

+0

main()方法運行時需要自動調用 –

0

java是一種面向對象的語言,所以如果你不引用對象,它們只會坐在那裏,什麼也不做。在主要方法中,您可以將其視爲創建對象的起點,並使用它們訪問方法。

裏面你main()方法,你可以創建一些物體..

Cat1 sampleCat1 = new Cat1(); 
Cat1 anotherSampleCat1 = new Cat1(4); 
0
package test; 
import java.text.DateFormat; 
import java.util.Calendar; 
import java.util.Locale; 

public class Sample { 

    public static void main(String[] args) { 
     Cat1 cat1 = new Cat1(1); 
     Cat2 cat2 = new Cat2(2); 
     Cat3 cat3 = new Cat3(3); 
     Calendar cal = Calendar.getInstance(); 

     //Date d = cal.getTime(); 
     Locale locale = new Locale("en"); 
     DateFormat dateFormat = DateFormat.getDateInstance(DateFormat.FULL, locale); 
     System.out.println(" LONg date : " + dateFormat.format(cal.getTime())); 

     String incomeData = "200,2,2,2,2"; 


     int expense = 0; 
     String expenseSplit = incomeData; 
     System.out.println(" e"); 
     String expenseArray[] = expenseSplit.split(","); 
     String income = expenseArray[0]; 
     for (int i = 1; i < expenseArray.length; i++) { 
      expense = expense+Integer.parseInt(expenseArray[i]); 
     } 
     System.out.println(" Income "+ income); 
     System.out.println(" expense "+ expense); 

    } 
} 
+0

如果我只想從其他類調用某些選定的方法,應該做什麼? –