2013-04-26 92 views
3

我正在用Java創建一個基類/父類,它將有幾個方法來創建類本身,我想知道是否有任何方法讓父類返回實例的子類,而不是返回父類的實例,然後必須投給孩子?創建一個返回子類對象的父類

例如,這裏是我的父類:

public abstract class SFObject 
{ 
    // Variables 
    protected String mID; 
    protected String mName; 

    // Function called to create ourselves from a DiffObject 
    public abstract SFObject CreateFromDiffObject(DiffObject object); 

    // Function called to create a list of ourselves from a query 
    public List<SFObject> CreateListFromQuery(Connection connection, String query) 
    { 
    // Run the query and loop through the results 
    ArrayList<SFObject> objects = new ArrayList<SFObject>(); 
    for (DiffObject object : connection.Query(query)) 
     objects.add(CreateFromDiffObject(object)); 

    return objects; 
    } 
} 

如果我創建基於我SFObject類的子類,在我的子類仍然會返回一個SFObject(需要被轉換爲兩種功能我的子類類型)或SFObjects列表(需要單獨轉換爲我的子類類型)。有什麼辦法(可能使用反射)讓我的子類返回自身的實例,而不是SFObjects?

+0

也許你正在尋找創建一個工廠或抽象工廠,而不是在這裏。 – 2013-04-26 20:50:21

回答

0

如果將子類對象置於父對象的內部,則調用的方法將從子類運行。但它看起來像父對象表面上

public class A{ 
    method 1(){//do some stuff} 
    method 2(){//do some stuff} 
} 

public class B extends A{ 
    method 1(){super.method 1() 
    //do some other stuff} 
    method 2(){super.method 2() 
    //do some other stuff} 
} 

public class test{ 
    A a = new B(); 

//any method called on 'a' will come from the child class 
// But 'a' is the parent object 

} 
+0

'但是'a'是父對象'我認爲你的意思是''但'a'是父引用',因爲''a'上的任何方法都會來自子類',因爲'a' *是*兒童班的對象 – Pshemo 2013-04-26 20:58:47

0

不知道如果我真的能理解你的問題正確的,因爲它的聲音,我艾克這樣的:

class p 
{ 
    public static p createParent() 
    { 
     return new p(); 
    } 

    public static c createChild() 
    { 
     return new c(); 
    } 
} 

當然也不必靜靜的,只是想到某種工廠。

0

正如您已經實施的那樣,正是針對此功能提出了factory methods。在子類中,您可以更改返回類型而不會違反方法聲明。用於你的情況樣品將是這樣的:

public abstract class SFObject { 
    // Variables 
    protected String mID; 
    protected String mName; 

    // Function called to create ourselves from a DiffObject 
    public abstract SFObject CreateFromDiffObject(DiffObject object); 

    // Function called to create a list of ourselves from a query 
    public List<? extends SFObject> CreateListFromQuery(Connection connection, String query) { 
     // Run the query and loop through the results 
     ArrayList<SFObject> objects = new ArrayList<SFObject>(); 
     for (DiffObject object : connection.Query(query)) 
      objects.add(CreateFromDiffObject(object)); 

     return objects; 
    } 

} 

class SFObjectChild extends SFObject { 

    @Override 
    public SFObjectChild CreateFromDiffObject(DiffObject object) { 
     SFObjectChild result = new SFObjectChild(); 
     //... 
     return result; 
    } 

    @Override 
    public List<? extends SFObjectChild> CreateListFromQuery(Connection connection, 
      String query) { 
     return null;//..; 
    } 

} 

這是不可接受的,因爲兒童類的返回類型仍然是一種(分層演講)的母公司。

請注意Java代碼約定(駱駝案例中的方法從低開始,例如createFromDiffObject)。

1

你所描述的是被稱爲covariant return type

Class A { 
    A getInstance() { ... } 
} 

Class B extends A { 
    @Override 
    B getInstance() { ... } 
} 

自Java 1.5以來,這已被允許。