C#继承.从基类派生的类

C# inheritance. Derived class from Base class(C#继承.从基类派生的类)
本文介绍了C#继承.从基类派生的类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个基类

public class A   
{
    public string s1;
    public string s2;
}

我也有一个派生类:

public class B : A
{
    public string s3;
}

假设我的程序创建了一个类 A 的实例.

Suppose my program created an instance of class A.

A aClassInstance = new A();

设置了一些参数:

aClassInstance.s1 = "string 1";
aClassInstance.s2 = "string 2";

此时我想创建一个 B 类的实例.但我希望 B 已经拥有我的 A 类实例的值.

At this point I would like to create an instance of class B. But I would like B to already have the values of my instance of class A.

这不起作用:

public B bClassInstance = new B():
bClassInstance = (B)aClassInstance;

没有这样做:

在 A 类中创建了一个克隆方法.

Made a clone method within Class A.

public B cloneA() {    
    A a = new A();
    a = (A)this.MemberwiseClone()
    return(B)a;
}

VS 代码采用上述两种方式 - 但我遇到运行时错误

The VS code takes both of the above - but I get run-time errors

请帮忙

推荐答案

您遇到的基本问题是,您必须构造一个 B 类型的实例(其中包含原因类型的属性A).您克隆 A 实例的方法不起作用,因为这会为您提供 A 类型的实例,您无法将其转换为 B.

The base problem you have is, that you have to construct an instance of type B (which contains of cause the properties of type A). Your approach to clone an A instance won't work, because that gives you an instance of type A, which you can't convert to B.

我会为 A 类和 B 类编写构造函数,它接受 A 类型的参数.B 类的构造函数只是将值传递给它的基类 A.A 类的构造函数知道如何将字段复制到自身:

I would write constructors for class A and B which takes a parameter of type A. The constructor of class B just passes the value to its base class A. The constructor of class A knows how to copy the fields to itself:

class A {
    public A(A copyMe) {
        s1 = copyMe.s1;
        ...
    }

class B : A {

    public B(A aInstance) : base(aInstance) {
    }

}

这样使用:

A a = new A();
a.s1 = "...";

B b = new B(a);

编辑

当您不想在添加新字段或道具时更改 A 的构造函数时,可以使用反射来复制属性.使用自定义属性来装饰您要复制的内容,或仅复制 A 的所有道具/字段:

When you don't want to have to change the constructor of A when adding new fields or props, you could use reflection to copy the properties. Either use a custom attribute to decorate what you want to copy, or copy just all props/fields of A:

public A (A copyMe) {
    Type t = copyMe.GetType();
    foreach (FieldInfo fieldInf in t.GetFields())
    {
        fieldInf.SetValue(this, fieldInf.GetValue(copyMe));
    }
    foreach (PropertyInfo propInf in t.GetProperties())
    {
        propInf.SetValue(this, propInf.GetValue(copyMe));
    }
}

我没有尝试过代码,但重点应该很清楚了.

I havn't tried the code, but the point should become clear.

这篇关于C#继承.从基类派生的类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

DispatcherQueue null when trying to update Ui property in ViewModel(尝试更新ViewModel中的Ui属性时DispatcherQueue为空)
Drawing over all windows on multiple monitors(在多个监视器上绘制所有窗口)
Programmatically show the desktop(以编程方式显示桌面)
c# Generic Setlt;Tgt; implementation to access objects by type(按类型访问对象的C#泛型集实现)
InvalidOperationException When using Context Injection in ASP.Net Core(在ASP.NET核心中使用上下文注入时发生InvalidOperationException)
LINQ many-to-many relationship, how to write a correct WHERE clause?(LINQ多对多关系,如何写一个正确的WHERE子句?)