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

C#inherit(kind off)/在单个类中使用多个类的属性

发布时间:2020-12-16 10:39:14 所属栏目:百科 来源:网络整理
导读:假设我有2个这样的类: public class A{ public string P1{ get; set; } public string P2{ get; set; }}public class B{ public string P3{ get; set; } public string P4{ get; set; }} 我需要一个这样的课: public class C{ public string P1{ get; set;
假设我有2个这样的类:

public class A
{
    public string P1{ get; set; }
    public string P2{ get; set; }
}

public class B
{
    public string P3{ get; set; }
    public string P4{ get; set; }
}

我需要一个这样的课:

public class C
{
    public string P1{ get; set; }
    public string P2{ get; set; }
    public string P3{ get; set; }
    public string P4{ get; set; }
}

C类可以使用A类和B类而不是重新声明所有属性吗?
(我需要这个DTO)

解决方法

你不能,因为多重继承仅适用于C#中的接口.

黑客

一世:

public class C : { public A; public B; }

二:

public interface IA { string P1,P2; }    
public interface IB { string P3,P4; }

public class C : IA,IB { string P1,P2,P3,P4; }

三:

public class A { public string P1,P2; }
public class B : A { public string P3,P4; }

public class C : B {}

至于DTO,也许这些链接1; 2可能有用:

如果没有,您可以使用反射动态构建您的类.

Build dynamically一个新的C类,包含A和B的所有公共属性.

(编辑:李大同)

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

    推荐文章
      热点阅读