2014-11-03 29 views
-1

我有一個用戶定義的類Fraction其中包含幾個構造函數和成員方法。我想創建一個獨立的「獨立函數」類,它使用Fraction的兩個實例作爲參數來創建第三個Fraction或者修改並返回它傳遞的實例之一。我在尋找哪些構造函數和訪問修飾符的組合?

class MyFractionProject{ 
public static void main(String[] args){ 
    class Fraction{ 
     private int numerator; 
     private int denominator; 
     public Fraction(){ 
      numerator = 0; 
      denominator = 1; 
     }//default constructor 

     public Fraction (int num, int denom){ 
      numerator = num; 
      denominator = denom; 
     }//sample constructor 

     //other constructors 


     //accessors and mutators (setters and getters) are here 

     public Fraction multiply(Fraction otherFraction){ 
      Fraction result = new Fraction(//multiply the Fractions); 
      return result; 
     }//sample member method, calls a constructor and accessor 
     //a bunch of other member methods are here 

    }//end Fraction 

    //New standalone utility class goes here: 
    class FractionUtility{ 
     //suite of ~5 functions which take two Fraction objects as arguments 
     //and return a Fraction object, either one of the arguments or a new 
     //instance 
     public static FractionUtility UtilityMultiply(Fraction fr1, Fraction fr2){ 
      //lots of stuff 
     }//Does this HAVE to return a FractionUtility, or can it return a Fraction? 
     //Can I declare variables outside the static FractionUtility methods if they 
     //will be needed every time the method is called? Will they be accessible 
     //inside the static methods? Do I need to instead declare them inside every 
     //static method so that they're visible each time I call the method? 
    }//end FractionUtility 

    //bunch of other stuff in main(), successfully uses Fraction but not 
    //FractionUtility 

}//end main() 
} 

Utility類別需要與Fraction正文分開定義。我需要有幾個不同的分數實例,但從不需要實例化FractionUtility。這使得它看起來像是一個靜態類會起作用,但是當我這樣做時會拋出錯誤 - 通常不能從靜態上下文訪問非靜態分數變量。

我可以看到在main()之外定義兩個類然後導入它們是有意義的,但我不知道如何去做,或者如果我這樣做了什麼規則。

+0

在'MyUtilityClass' *的方法應該*是靜態的。這隻意味着你不能在沒有實例的情況下訪問'MyUtilityClass'的實例成員。這並不意味着你不能訪問其他類中的實例成員,只要你有一個實例可以訪問它們。請提供一個你需要做的事情的具體例子*和* MyClass'的所有成員來完成這個任務。正如所寫的,你並不真正問一個可回答的問題。 – cdhowie 2014-11-03 20:51:22

+2

爲什麼選擇C++標籤? – 2014-11-03 20:52:47

+1

這個'main()'方法應該屬於'MyClass'類嗎?它必須屬於* some *類。如果您嘗試編譯僅包含您所呈現內容的Java源文件,那麼編譯器肯定會發出錯誤。 – 2014-11-03 20:57:27

回答

1

好吧,你似乎只想在同一個文件中聲明幾個不相關的類。

這就是靜態內部類的用途。

例如,你可以這樣做:

public class Something { 

static class MyClass { 
    private int data = 0; 

    public MyClass() { 
    } 
    public MyClass(int data) { 
     this.data = data; 
    } 

    public MyClass makeNew(MyClass otherinstance) { 
     MyClass result = new MyClass(this.data + otherinstance.data); 
     return result; 
    } 
} 


static class MyUtilityClass { 
} 

public static void main(String[] args) { 
    MyClass myClass = new MyClass(); 
    MyClass copy = myClass.makeNew(new MyClass()); 
    MyUtilityClass utilityClass = new MyUtilityClass(); 
} 
} 
+0

如果您未指定Java中的某個元素的作用域,則默認情況下它將是包私有的。這意味着,例如,同一包中的任何其他類將能夠看到MyUtilityClass。如果您想讓任何其他課程中的其他課程可見,請將其公開。如果您只希望它們在該文件中可見,請將它們設爲私人。這同樣適用於方法。當然,課堂可見度優先於方法可見性,因爲當然,如果你甚至不能看到一個不公開的類,即使它是公開的,也不能調用任何方法。 – Renato 2014-11-03 21:12:57

+0

請注意,JLS術語是「靜態嵌套」類。 「內部類」是一個非靜態的嵌套類。 – 2014-11-03 21:13:00

+0

當我以這種方式聲明MyClass時,當我想用​​各種不同的值來實例化它時,這不會導致問題,比如兩個MyClass實例,一個是int數據== 10,另一個是數據== 20?當我在該場景中實例化MyUtilityClass時,它將返回一個MyUtilityClass,而不是MyClass,對嗎? 我要編輯我的原始帖子以提供更多信息,我原本擔心發佈的任務過於詳細,但如果不這樣做,顯然不會完成任何操作。更新很快。 – Pete 2014-11-04 00:39:24