简体   繁体   English

从派生类访问基类属性

[英]Access base class properties from a derived class

So I have come across a problem I have created a object of a class, this class also have a derived class from where I need to acces these properties.所以我遇到了一个问题,我创建了一个类的对象,这个类还有一个派生类,我需要从中访问这些属性。 I actually already have two solutions to this problem, which I will present here later.对于这个问题,我实际上已经有两个解决方案,我稍后会在这里介绍。 I am not sure if this will be considered a valid question or just a code review since I already have solutions to this specific problem.我不确定这是否会被视为有效问题或只是代码审查,因为我已经有了针对此特定问题的解决方案。 I guess I am looking for a better solution.我想我正在寻找更好的解决方案。 Well lets jmp in to the question:那么让 jmp 来回答这个问题:

So in my base class I have this code:所以在我的基类中,我有这个代码:

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);
    }
}

I if now from Main create an object of this class with the parameters "Hello" and "World!"我现在从 Main 创建一个这个类的对象,参数为“Hello”和“World!” and then I call the PrintProps method I should see "Hello World! on my screen, which I do.然后我调用 PrintProps 方法,我应该在我的屏幕上看到“Hello World!

Code:代码:

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

What I would like to do now is to have a Child class that have the exact same method which should print out the properties that is created in my parent class.我现在想要做的是拥有一个具有完全相同方法的 Child 类,该方法应该打印出在我的父类中创建的属性。

Something like this I would want to do:我想做这样的事情:

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

And then call it from Main:然后从 Main 调用它:

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

When I call the childclass's printProp method I should expect "Hello World!"当我调用子类的 printProp 方法时,我应该期待“Hello World!” to be printed on the screen, this doesn't work obviously.要打印在屏幕上,这显然不起作用。 There is two ways to fix this which I am aware of.我知道有两种方法可以解决这个问题。 First way to fix this (Not the most object oriented way):解决此问题的第一种方法(不是最面向对象的方法):

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

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

and then call it in main like this:然后像这样在 main 中调用它:

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

This works like a charm since I am passing in the created object of the base class but with this way I am not using any inheritance, which I think is a bad way to do it.这就像一个魅力,因为我传递了基类的创建对象,但通过这种方式我没有使用任何继承,我认为这是一种不好的方式。

The other way to fix this problem would be to call the base class constructor in my child class like this:解决此问题的另一种方法是在我的子类中调用基类构造函数,如下所示:

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

and then call it from main like this:然后像这样从 main 调用它:

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

This also solves the problem, and this way I am using inheritance and polymorphism.这也解决了问题,这样我就使用了继承和多态。 But as you see I am creating one object of the base class in Main, and Another object in the derived class.但是正如您所看到的,我在 Main 中创建了基类的一个对象,在派生类中创建了另一个对象。 But since both objects is identical I figure it would be unnecessary to create two objects.但是由于两个对象是相同的,我认为没有必要创建两个对象。 So in some way I would want to use the object that is created in Main in my derived class, without passing the object in as a argumnet.所以以某种方式,我想在我的派生类中使用在 Main 中创建的对象,而不是将对象作为参数传递进来。 I want to do it using Inheritance and polymorphism.我想使用继承和多态来做到这一点。 So to sum this up is it possible to not "duplicate" the object of the base class when I am inheriting the base class and using it in my child class?所以总结一下,当我继承基类并在我的子类中使用它时,是否可以不“复制”基类的对象? Or will I have to create one object in main and then another one in my childclass?或者我必须在 main 中创建一个对象,然后在我的子类中创建另一个对象? I think I could use the PropertyInfo class in some way to fix this problem, but I am not very experienced in this so I might just bee asking for the impossible.我想我可以以某种方式使用 PropertyInfo 类来解决这个问题,但我在这方面不是很有经验,所以我可能只是要求不可能的事情。 A lot to read I know.很多书我知道。 Any advice will be appreciated :).任何建议将被认真考虑 :)。

Edit: I need to create the baseclass object in Main because I am calling both the base class PrintProp method and the child class PrintProp method in Main and I want the same result with both methods.编辑:我需要在 Main 中创建基类对象,因为我在 Main 中同时调用基类 PrintProp 方法和子类 PrintProp 方法,并且我希望这两种方法得到相同的结果。 Code:代码:

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

Edit: To be more specific I want to create an object of the base class in the Main method.编辑:更具体地说,我想在 Main 方法中创建基类的对象。 And then I want to access the properties of this object from the child class, without passing the base class as an argument to the child class and without Calling the base classs constructor from my child class and thus create a second object of the base class.然后我想从子类访问这个对象的属性,不将基类作为参数传递给子类,也不从我的子类调用基类构造函数,从而创建基类的第二个对象。

So if I from Main pass in "123" in the base class constructor I would then want to be able to access this property from my child class.因此,如果我从 Main 在基类构造函数中传入“123”,那么我希望能够从我的子类访问此属性。 So in my child class the property should also be "123".所以在我的孩子类中,属性也应该是“123”。 I do NOT want to call the base class constructor in my child class like this:我不想像这样在我的子类中调用基类构造函数:

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

I think PropertyInfo is what I am looking for but I am not sure since I have never used that class.我认为 PropertyInfo 是我正在寻找的,但我不确定,因为我从未使用过该类。 Like I said I might (and most possibly is) asking for the impossible.就像我说的那样,我可能(而且很可能是)要求不可能的事情。

ok so here is the deal: you can't pass a base class object into a child class variable because the compiler dosen't know how to "fill up" the extra parts of the child class.好的,这里是交易:您不能将基类对象传递给子类变量,因为编译器不知道如何“填充”子类的额外部分。 I think the "best" way would be to create a parsing function inside the child class:我认为“最好”的方法是在子类中创建一个解析函数:

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;
    }
}

Depending on the class this could get complicated but i think is the best solution.根据课程的不同,这可能会变得复杂,但我认为这是最好的解决方案。

BUT you should really just try to create a child class object in you main function.但是你真的应该尝试在你的主函数中创建一个子类对象。

Or maybe just use interfaces and make your child castable:或者也许只是使用接口并让您的孩子可以铸造:

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);
    }
}

if you don't create a child object inside you main function it just gets complicated ;)如果您不在主函数中创建子对象,它只会变得复杂;)

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

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