繁体   English   中英

从派生类访问基类属性

[英]Access base class properties from a derived class

所以我遇到了一个问题,我创建了一个类的对象,这个类还有一个派生类,我需要从中访问这些属性。 对于这个问题,我实际上已经有两个解决方案,我稍后会在这里介绍。 我不确定这是否会被视为有效问题或只是代码审查,因为我已经有了针对此特定问题的解决方案。 我想我正在寻找更好的解决方案。 那么让 jmp 来回答这个问题:

所以在我的基类中,我有这个代码:

public class MyBaseClass : IMyInterface
{
    public string myProperty { get; set; }
    public string myProperty2 { get; set; }
    public MyBaseClass(string prop1, string prop2)
    {
       myProperty = prop1;
       myProperty2 = prop2;
    }

    public virtual void PrintProps()
    {
       Console.WriteLine(myProperty + " " + myProperty2);
    }
}

我现在从 Main 创建一个这个类的对象,参数为“Hello”和“World!” 然后我调用 PrintProps 方法,我应该在我的屏幕上看到“Hello World!

代码:

MyBaseClass myBase = new MyBaseClass("Hello!", "World");
myBase.PrintProps();

我现在想要做的是拥有一个具有完全相同方法的 Child 类,该方法应该打印出在我的父类中创建的属性。

我想做这样的事情:

public class MyChildClass: MyBaseClass
{
    public override void PrintProps()
    {
        Console.WriteLine(myProperty + " " + myProperty2);
    }
}

然后从 Main 调用它:

MyChildClass myChild = new MyChildClass ();
myChild.PrintProps();

当我调用子类的 printProp 方法时,我应该期待“Hello World!” 要打印在屏幕上,这显然不起作用。 我知道有两种方法可以解决这个问题。 解决此问题的第一种方法(不是最面向对象的方法):

public class MyChildClass
{
    MyBaseClass myBase = default;
    public MyChildClass(MyBaseClass myBase)
    {
        this.myBase = myBase;
    }

    public void PrintProps()
    {
        Console.WriteLine(myBase.myProperty + " " + myBase.myProperty2);
    }
}

然后像这样在 main 中调用它:

MyBaseClass myBase = new MyBaseClass("Hello!", "World");
var myChild = new MyChildClass(myBase);
myChild.PrintProps();

这就像一个魅力,因为我传递了基类的创建对象,但通过这种方式我没有使用任何继承,我认为这是一种不好的方式。

解决此问题的另一种方法是在我的子类中调用基类构造函数,如下所示:

public class MyChildClass : MyBaseClass
{
    public MyChildClass() : base("Hello", "World!") { }
    public override void PrintProps()
    {
        Console.WriteLine(myProperty + " " + myProperty2);
    }
}

然后像这样从 main 调用它:

MyBaseClass myBase = new MyBaseClass("Hello!", "World");
MyChildClass myChild = new MySubClass();
myChild.PrintProps();

这也解决了问题,这样我就使用了继承和多态。 但是正如您所看到的,我在 Main 中创建了基类的一个对象,在派生类中创建了另一个对象。 但是由于两个对象是相同的,我认为没有必要创建两个对象。 所以以某种方式,我想在我的派生类中使用在 Main 中创建的对象,而不是将对象作为参数传递进来。 我想使用继承和多态来做到这一点。 所以总结一下,当我继承基类并在我的子类中使用它时,是否可以不“复制”基类的对象? 或者我必须在 main 中创建一个对象,然后在我的子类中创建另一个对象? 我想我可以以某种方式使用 PropertyInfo 类来解决这个问题,但我在这方面不是很有经验,所以我可能只是要求不可能的事情。 很多书我知道。 任何建议将被认真考虑 :)。

编辑:我需要在 Main 中创建基类对象,因为我在 Main 中同时调用基类 PrintProp 方法和子类 PrintProp 方法,并且我希望这两种方法得到相同的结果。 代码:

MyBaseClass myBase = new MyBaseClass("Hello", "World!");
myBase.PrintProps(); //Expected "Hello World!" to be printed to the screen

MyChildClass myChild = new MyChildClass();
myChild.PrintProps(); //Also expecting "Hello World!" to be printed to the screen

编辑:更具体地说,我想在 Main 方法中创建基类的对象。 然后我想从子类访问这个对象的属性,不将基类作为参数传递给子类,也不从我的子类调用基类构造函数,从而创建基类的第二个对象。

因此,如果我从 Main 在基类构造函数中传入“123”,那么我希望能够从我的子类访问此属性。 所以在我的孩子类中,属性也应该是“123”。 我不想像这样在我的子类中调用基类构造函数:

 public class MyChildClass : MyBaseClass
 {
    public MyChildClass() : base("123") { }
 }

我认为 PropertyInfo 是我正在寻找的,但我不确定,因为我从未使用过该类。 就像我说的那样,我可能(而且很可能是)要求不可能的事情。

好的,这里是交易:您不能将基类对象传递给子类变量,因为编译器不知道如何“填充”子类的额外部分。 我认为“最好”的方法是在子类中创建一个解析函数:

using System;

public class Base
{
    public string prop1 { get; set; }
    public string prop2 { get; set; }
    public Base(string p1, string p2)
    {
        prop1 = p1;
        prop2 = p2;
    }
    public virtual void print()
    {
        Console.WriteLine(prop1 + " " + prop2);
    }
}

public class cChild : Base
{
    public cChild() : base("", "")
    {
    }
    public override void print()
    {
        Console.WriteLine(Base.prop1 + "......" + Base.prop2);
    }
    public static cChild ofBase(Base papa)
    {
        cChild r = new cChild();
        r.prop1 = papa.prop1;
        r.prop2 = papa.prop2;
        return r;
    }
}

根据课程的不同,这可能会变得复杂,但我认为这是最好的解决方案。

但是你真的应该尝试在你的主函数中创建一个子类对象。

或者也许只是使用接口并让您的孩子可以铸造:

using System;

public interface printable
{
    string prop1 { get; set; }
    string prop2 { get; set; }
    void print();
}

public class Base : printable
{
    public string prop1 { get; set; }
    public string prop2 { get; set; }
    public Base(string p1, string p2)
    {
        prop1 = p1;
        prop2 = p2;
    }
    public virtual void print()
    {
        Console.WriteLine(prop1 + " " + prop2);
    }
}

public class cChild : printable
{
    public new static explicit operator cChild(Base A)
    {
        cChild r = new cChild();
        r.prop1 = A.prop1;
        r.prop2 = A.prop2;
        return r;
    }
    public string prop1 { get; set; }
    public string prop2 { get; set; }
    public void print()
    {
        Console.WriteLine(prop1 + "......" + prop2);
    }
}

如果您不在主函数中创建子对象,它只会变得复杂;)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM