第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

C#訪問器和對象類繼承

C#訪問器和對象類繼承

C#
Qyouu 2021-03-31 13:10:03
因此,關(guān)于對象繼承和構(gòu)造函數(shù),我可能有一個幼稚的問題。基本上,一個類具有一個對象:public class ParentClass{protected Parent item;訪問器如下:public Parent ItemValue{    set    {        item = value;    }    get    {        return item;    }}現(xiàn)在,我想繼承該類:public class ChildClass:ParentClass    {    public new Child item;    }現(xiàn)在,每當(dāng)我Child item通過繼承的訪問器訪問時,它當(dāng)然都會將該項目作為Parent類而不是Child類返回。有沒有一種方法可以使它返回itemasChild類而不會覆蓋the訪問器ChildClass呢?
查看完整描述

2 回答

?
江戶川亂折騰

TA貢獻(xiàn)1851條經(jīng)驗 獲得超5個贊

不可以,您不能將基本屬性的類型更改為返回不同的(派生的)類型。


如果不需要繼承,則采用標(biāo)準(zhǔn)解決方法-通用類:


public class ParentClass<T> {

      public T ItemValue { get; set; }

...

}


public class ChildClass : ParentClass<ChildClass> 

{

  ...

}

請注意,如果您只需要訪問自己類中的item,則可以擁有virtual屬性:


public class Parent { }

public class Child:Parent { public string ChildProperty; }


public abstract class ParentClass

{

    public abstract Parent ItemValue { get; }

}


public class ChildClass : ParentClass

{

    Child item;

    public override Parent ItemValue { get {return item;} }


    public void Method()

    {

       // use item's child class properties

       Console.Write(item.ChildProperty);

    }

}


查看完整回答
反對 回復(fù) 2021-04-10
  • 2 回答
  • 0 關(guān)注
  • 188 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號