如果我在一個主類實例化一個對象,說:初學者對象引用質疑
SomeObject aRef = new SomeObject();
然後我實例從主類的另一個對象,說:
AnotherObject xRef = new AnotherObject();
哪有實例AnotherObject使用aRef引用訪問SomeObject中的方法? (要使用SomeObject的同一個實例)
如果我在一個主類實例化一個對象,說:初學者對象引用質疑
SomeObject aRef = new SomeObject();
然後我實例從主類的另一個對象,說:
AnotherObject xRef = new AnotherObject();
哪有實例AnotherObject使用aRef引用訪問SomeObject中的方法? (要使用SomeObject的同一個實例)
爲什麼不與原來的SomeObject
參考實例AnotherObject
?
例如
SomeObject obj = new SomeObject();
AnotherObject obj2 = new AnotherObject(obj);
和AnotherObject
會是什麼樣子:
// final used to avoid misreferencing variables and enforcing immutability
private final SomeObject obj;
public AnotherObject(final SomeObject obj) {
this.obj = obj;
}
所以AnotherObject
有先前創建的SomeObject
參考。然後它可以使用該引用來調用方法。如果原始對象不在AnotherObject
的範圍之外,則在AnotherObject
內部創建它,然後執行封裝。
xRef.SetSomeObject(aRef);
其中SetSomeObject具有像
public void SetSomeObject(SomeObject obj)
{
obj.DoStuff();
}
簽名,並且是類型AnotherObject
的成員函數。
的戰略設計模式和裝飾設計模式有兩種不同的方式,你可以做到這一點。
例如,你可以有:
class AnotherObject
{
private SomeObject mySomeObject;
public AnotherObject(SomeObject mySomeObject)
{
this.mySomeObject = mySomeObject;
}
function doSomethingUsingStrategy()
{
mySomeObject.doItTheMySomeObjectWay();
}
function setMySomeObject(SomeObject mySomeObject)
{
this.mySomeObject = mySomeObject;
}
}
再後來,你可以使用不同的策略:
myAnotherObject.setMySomeObject(new ExtendsSomeObject);
myAnotherObject.doSomethingUsingStrategy()
我想你問是關於範圍的問題。你問的是xRef在執行過程中如何使用aRef?答案是,在AREF參考需要傳遞到外部參照對象時,它被實例化
xRef = new AnotherObject(aRef)
或實例化之後,你可以有
xRef.setSomeObject(aRef)
是的,這是一個關於範圍的問題。例如main方法創建一個類的實例並使用它的mutator方法,然後創建另一個類的實例,這也需要使用這些mutator方法。 – Kari
能AnotherObject有一個成員或屬性,有一個SomeObject的類型?那也是處理這個問題的另一種方法。
所以,如果有是AnotherObject類的「SomeObjectMember」成員:
xRef.SomeObjectMember = aRef;
您需要提供的AnotherObject
無論是在構造函數實例的引用AREF:AnotherObject xRef = new AnotherObject(aRef)
或使用setter方法:xRex.setSomeObject(aRef)
。在這種情況下AnotherObject
需要有一個實例變量來存儲阿里夫可以在內部使用,如:
class AnotherObject {
SomeObject aRef;
public AnotherObject(SomeObject aRef) {
this.aRef = aRef;
}
public void doSomethingWithSomeObject() {
aRef.doSomething();
}
}
你也可以的SomeObject
實例傳遞到方法上,要求他們像xRef.doSomethingWithSomeObject(aRef)
AnotherObject
。
class AnotherObject {
public void doSomethingWithSomeObject(SomeObject aRef) {
aRef.doSomething();
}
}
有很多方法可以做到這一點(正如其他人指出的那樣)。你真的想考慮你的對象結構,雖然...
也許你的主要方法甚至不應該實例化aRef,也許它應該在xRef的構造函數中實例化(這是xRef往往是一個「部分「的阿里夫。
的功能,如果AREF可以在某一時刻有多個實例,你可能不想保存它關閉所有,您可能希望將其傳遞每當外部參照方法使用它。
這是您需要在業務邏輯級考慮您的對象模型的地方。對象之間的關係是什麼等等。
(我的猜測是你想要xRef實例化aRef並保留引用本身,那麼如果你的「main」確實需要與aRef交談,它可以要求xRef轉發消息或者請求xRef爲它的aRef實例。)
謝謝。邏輯是這樣的:主要方法創建一個類的實例,並使用它的增變器方法在其中設置數據,然後在某一時刻它創建另一個類的實例,這也需要使用這些增變器方法 – Kari
這兩個對象表示什麼?小狗?樹?數據庫?一些計算的操作數?兩個不同對象之間的關係是什麼?他們不是很同儕,因爲一方在另一方不知情的情況下使用另一方。爲什麼你必須在main中創建兩個對象?是否有一些主要信息在其他地方不可用? –
我知道這似乎很奇怪,但回答這些問題並思考這樣的關係是什麼使得您的OO模型可以隨着它的增長和變形而變得可用,否則對象是沒用的。 –
你必須通過ref然後做一些事情。
class AnotherObject {
SomeObject someObject;
public void setSomeObject(SomeObject some) {
this.someObject = some;
}
public void doSomethingWithSomeObject() {
this.someObject.someMethod();
}
..... rest of your code
}
你可以在main方法
public static void main(String [] args) {
SomeObject xRef = new SomeObject();
AnotherObject aRef = new AnotherObject();
// pass the ref...
aRef.setSomeObject(xRef);
// use it
aRef.doSomethingWithSomeObject();
}
使用像這樣的方法是,你需要什麼?
他的問題的答案是使第一類成爲一個靜態類。
我所做的是將第一個類的字段設置爲static,並使訪問者和mutator方法公開。這樣我可以創建同一類的兩個實例,但數據將是相同的。但我認爲會有更好的方式來做到這一點! – Kari
要求某人做一些調用者可以輕鬆做到的事情是很奇怪的。尤其是在一個名稱暗含「setter」的方法中(當您將給定的引用或對象分配給某個專用字段時) – Dmitry
爲true。我更多地展示瞭如何將引用傳遞到目標對象。在我的角色中選擇不好的名字。 – dkackman