加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c# – 属性的获取是否可以是抽象的并且集合是虚拟的?

发布时间:2020-12-15 17:14:35 所属栏目:百科 来源:网络整理
导读:我有这样一个基类: public class Trajectory{ public int Count { get; set; } public double Initial { get; set { Count = 1; } } public double Current { get; set { Count ++ ; } }} 所以,我在基类中有代码,这使得set-s成为虚拟,但是get-s必须保持抽象
我有这样一个基类:
public class Trajectory{
    public int Count { get; set; }
    public double Initial { get; set { Count = 1; } }
    public double Current { get; set { Count ++ ; } }
}

所以,我在基类中有代码,这使得set-s成为虚拟,但是get-s必须保持抽象.所以我需要这样的东西:

...
public double Initial { abstract get; virtual set { Count = 1; } }
...

但是这段代码会出错.
重点是在基类中实现计数器功能,而不是在所有派生类中实现.
那么,我如何使用不同的修饰符来创建属性的get和set?

解决方法

将其分为两个功能:
public double Initial
{
    get { return GetInitial(); }
    set { SetInitial(value); }
}

protected virtual void SetInitial(double value)
{
    Count = 1;
}

protected abstract double GetInitial();

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读