2015-06-16 25 views
0

我不知道這種傳遞對象作爲參數的方式被調用。所以我不知道該在網站上搜索什麼。我很抱歉,如果之前已經問過這個問題,我確信它已經有了。這段Java代表什麼意思?

有人可以告訴我這兩種傳遞對象作爲參數的方式有什麼區別嗎?爲什麼我不能以第一種方式重寫屬性和方法?

myObject.doSomethingWithOtherObject((new OtherObject() { 
    // why can't I override properties here? 
    public String toString() { 
    return "I am the other object."; 
    } 
    ... 
})); 
myOtherObject = new OtherObject(); 
myObject.doSomethingWithOtherObject(myOtherObject); 

然後我有第二個問題,但也許回答第一個問題也將回答這個問題。 爲什麼這個不行:

public class OtherObject { 
    public OtherObject (String str) { 
     super(); 
    } 
} 

myObject.doSomethingWithOtherObject((new OtherObject("string arg") { 
    public void overrideSomething() { 

    } 
})); 

字符串「字符串Arg」不是傳遞給構造函數,因爲我本來期望。而是抱怨沒有找到構造函數OtherObject()。 爲什麼Java不能識別我試圖發送一個參數給構造函數?

感謝您的閱讀!

+3

*「然後,我有第二個問題」 * ** **的一個問題每個問題,請。詳細地說,請參閱[遊覽]和[幫助],特別是[*我如何提出一個好問題?](/ help/how-to-ask) –

+0

閱讀什麼是匿名課程...... – Garry

+1

這是最好的如果你將兩個問題分解爲單獨的問題。這樣的答案將永遠是關於當前的問題:)像其他人所說的,在另一個類中創建一個類(就像你在這裏所做的那樣)被稱爲匿名內部類。如果你仍然在學習基礎知識,我會盡量保持這一點,直到你對基本要素感到滿意爲止。 – miva2

回答

3

當你做new OtherObject然後打開大括號{ ...你正在創建一個新的(annonymous)類。不是已定義的OtherObject類的新對象。看看Java的匿名類,以更好地理解

1

第一個被稱爲匿名類。這意味着你實際上實例化一個沒有名字的子類,以覆蓋某些東西。

在第二個,編譯器抱怨你沒有一個構造函數。正如你在第一個問題的答案中所描述的那樣,你實際上是在這裏進行子類化,這意味着如果你需要定義一個構造函數。

+0

第二個例子有一個構造函數...'public OtherObject(String str)' –

0

用花括號跟隨的新對象調用方法就像擴展該類,它在java中稱爲匿名類。

myObject.doSomethingWithOtherObject(new OtherObject() { 
    public String toString() { 
    return "I am the other object."; 
    } 

}); 

您無法重寫屬性的原因可能是因爲它們的訪問修飾符。 爲了覆蓋類成員,它應該具有至少受保護的訪問修飾符。

public class OtherObject { 
    protected String name; 
} 

您現在可以在您的匿名類中覆蓋名稱。

1

您的代碼:

myObject.doSomethingWithOtherObject((new OtherObject() { 
    // why can't I override properties here? 
    public String toString() { 
    return "I am the other object."; 
    } 
... 
})); 

大致等同於:

class OtherSubObject extends OtherObject { 
    public String toString() { 
    return "I am the other object."; 
    } 
} 

... 
myObject.doSomethingWithOtherObject(new OtherSubObject()); 

除了OtherObject的子類已經沒有名字創建(即都是匿名的),所以不能被稱爲任何地方其他。

您的代碼:

myOtherObject = new OtherObject(); 
myObject.doSomethingWithOtherObject(myOtherObject); 

不會創建一個新的類,它只是傳遞OtherObject的實例作爲參數。

1

所有我想證實什麼我前面的發言者說,首先:

當您添加花括號對象初始化,您將創建該類的一個匿名子類型,你可以重寫方法爲您在你的例子中做了。

但現在對您的評論問題:爲什麼我不能在這裏覆蓋屬性?

您可以:通過添加另一對大括號,這將創建一個構造匿名類型,例如:

public class OtherObject { 
    protected String name; 

    public OtherObject(String name) { 
     super(); 
     this.name = name; 
    } 

    public String toString() { 
     return getClass() + ": " + name; 
    } 

    public static void main(String[] args) { 
     OtherObject o1 = new OtherObject("bar"); 
     OtherObject o2 = new OtherObject("bar") { // will call parent constructor first -> name = "bar" 
      { 
       // constructor for the anonymous type 
       name = "foo"; // overwrite the name 
      }   
     }; 

     System.out.println(o1); // prints "class OtherObject: bar" 
     System.out.println(o2); // prints "class OtherObject$1: foo" 
    } 
}