簡體   English   中英

如何創建抽象構造函數或獲取抽象類的類型並返回它?

[英]How to create an abstract constructor or get type of abstract class and return it?

我無法在抽象類內部正常工作的方法或構造函數。

本質上,我有幾個數據協定類擴展了我的抽象類,並且在它們內部有一個幾乎相同的簡單方法,我試圖弄清楚如何轉移到我的抽象類。

我敢肯定,構造函數最有意義,但我無法弄清楚正確的語法。 要當前使用它,我可以這樣稱呼:

OrderLine orderLine = new OrderLine();

orderLine = orderLine.createFromJsonString("MyJsonString");

我正在嘗試將標記為(1)和(2)的方法作為方法或構造函數移至(0)位置。

abstract class Pagination<T>
{
    public int _offset { get; set; }
    public int _total { get; set; }
    public string previous { get; set; }
    public string next { get; set; }
    public abstract List<T> items { get; set; }

    public int getItemCount()
    {
        return items != null ? items.Count() : 0;
    }

    // (0)
    // Each of the child objects that extend this class are created from
    // a Json that is deserialized. So I'd rather some method that would
    // construct or return a new instance of the abstract object 
    /*
    public object createFromJsonString(string _json)
    {
     * // The main issue here is the "this" keyword
        return JsonConvert.DeserializeObject<this>(_json);
    }
     **/

}

class OrderHeader : Pagination<OrderLine>
{
    public int orderId { get; set; }
    public List<OrderLine> items { get; set; }

    // (1)
    // How can I move this into the abstract class?
    // Or should it be written as constructor?
    public OrderHeader createFromJsonString(string _json)
    {
        return JsonConvert.DeserializeObject<OrderHeader>(_json);
    }
}

class OrderLine : Pagination<OrderLineDetails>
{
    public string sku { get; set; }
    public int qty { get; set; }
    public List<OrderLineDetails> items { get; set; }

    // (2)
    // How can I move this into the abstract class?
    // Or should it be written as constructor?
    public OrderLine createFromJsonString(string _json)
    {
        return JsonConvert.DeserializeObject<OrderLine>(_json);
    }
}

class OrderLineDetails
{
    public string serialNum { get; set; }
}

在這里,您做錯了幾件事:

// You have created object once here, this object would become unused in next line
OrderLine orderLine = new OrderLine();

// Here you are building a new object via Deserialize
orderLine = orderLine.createFromJsonString("MyJsonString");

從您的問題中我了解到的是,您想要一種工廠方法來創建派生的Pagination <>類型的對象。

abstract class Pagination<T>
{
    public int _offset { get; set; }
    public int _total { get; set; }
    public string previous { get; set; }
    public string next { get; set; }
    public abstract List<T> items { get; set; }

    public int getItemCount()
    {
        return items != null ? items.Count() : 0;
    }

    /// <summary>
    /// Factory method to build the pagination object from Json string.
    /// </summary>
    public static TCurrent CreateFromJsonString<TCurrent>(string _json) where TCurrent: Pagination<T>
    {
        return JsonConvert.DeserializeObject<TCurrent>(_json);
    }
}

現在,您可以構建派生類型的對象,例如:

 OrderHeader hdr = Pagination<OrderLine>.CreateFromJsonString<OrderHeader>(json);

 OrderLine line = Pagination<OrderLineDetails>.CreateFromJsonString<OrderLine>(json);

工廠方法還防止執行以下操作,因為我們應用了通用約束,因此僅允許相關項目類型。

// This will throw error of invalid implicit conversion
OrderHeader invalidObj = Pagination<OrderLineDetails>.CreateFromJsonString<OrderHeader>(json);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM