简体   繁体   English

C#:从子类继承到基类

[英]C#: Inherit from Child-Class to Base-Class

so I have a problem: When I'm trying to hand over a parameter from the Child-Class to the Base-Class, the parameter is not in the Base-Class:所以我有一个问题:当我试图将参数从子类移交给基类时,该参数不在基类中:

public class Zeiteinheit : Shutdown_Time
{
     public int Public_minuten
     {
        get { return _minuten; }
        set { _minuten = value; }
     }

    public void Minuten_Zu_Sekunden_Umrechnung()
    {
        _sekunden = (_minuten * 60); 
    } 
}

public class Shutdown_Time
{
    protected int _sekunden;
    protected string herunterfahrenTimer;

    public string Public_herunterfahrenTimer 
    {
        get { return herunterfahrenTimer; }

        set { herunterfahrenTimer = $"-s -t {_sekunden}"; }
    }
}

MainClass:主类:

//Umrechnung der eingetragenen Zeit findet statt
obj_zeiteinheit.Minuten_Zu_Sekunden_Umrechnung();

//Herunterfahren mit der Umgerechneten Zeit von stunden in sekunden
System.Diagnostics.Process.Start("Shutdown", obj_shutdown_Time.Public_herunterfahrenTimer);
break;

Result of an debug调试结果

My Question is here, did i missunderstood something by the concept of inheriting or what is my mistake?我的问题在这里,我是否误解了继承的概念或者我的错误是什么?

(Sorry for my very bad english, and thanks for the help!) (对不起,我的英语很糟糕,感谢您的帮助!)

You seem to be asking "when I set a property or field in a base class, how do I trigger the updating of a property or field in a derived class?"您似乎在问“当我在基类中设置属性或字段时,如何触发派生类中属性或字段的更新?”

But then you showed code that outlines you want to set ZeitenHeit.Public_Minuten = 60 , which does set _sekunden = 3600 but you then also want to update, in the same class , herunterfahrenTimer so it is -s -t 3600 because right now it isn't updated (the screenshot is null)但是随后您显示了概述要设置ZeitenHeit.Public_Minuten = 60的代码,它确实设置_sekunden = 3600但您还想在同一个类中更新herunterfahrenTimer所以它是-s -t 3600因为现在它不是'未更新(截图为空)

There is a small but critical difference between what you asked and what you appear to be trying to do, so you've essentially asked two questions.您提出的问题与您似乎正在尝试做的事情之间存在微小但关键的差异,因此您基本上提出了两个问题。 I'll deal with that one in the question title first:我将首先处理问题标题中的那个:

how can a base class update a derived(child) class?基类如何更新派生(子)类?

It can't;它不能; it doesn't even know it exists.它甚至不知道它的存在。 The derived class knows the base class exists though, so you can use that instead派生类知道基类存在,所以你可以使用它

One way is to have the derived class override the setting function of the base class:一种方法是让派生类覆盖基类的设置功能:

class MyBase{
  int _myField;

  virtual void SetMyField(int val){
    _myField = val;
  }
}

class MyDerived:MyBase{
  string _myOtherField;

  override void SetMyField(int val){
    base.SetMyField(val);
    _myOtherField = val.ToString();
  }
}

In use, a variable of type MyBase holding an instance of type MyDerived, will set both values when you call SetMyField:在使用中,持有 MyDerived 类型实例的 MyBase 类型变量将在您调用 SetMyField 时设置这两个值:

var mb = new MyDerived();
mb.SetMyField(1); //implementation on MyDerived is used, causing _myOtherField to be set

You might have the base class raise an event (or invoke a delegate) when it's property is set and the derived class subscribes to it当基类的属性设置并且派生类订阅它时,您可能会让基类引发事件(或调用委托)

class MyBase{
  int _myField;
  event EventHandler<int> MyFieldUpdatedTo;

  void SetMyField(int val){
    _myField = val;
    var e = MyFieldUpdatedTo;
    e?.Invoke(this, val);
  }
}

class MyDerived:MyBase{
  string _myOtherField;

  MyDerived(){ //constructor, or can also subscribe to event in the place that instantiates the MyDerived
    base.MyFieldUpdatedTo += val => _myOtherField = val.ToString(); 
  }
}

This is perhaps slightly unusual, though common to see in windows forms这可能有点不寻常,尽管在 Windows 窗体中很常见

However you arrange it, you need to leverage that the derived knows about the base but not the other way round, so anything done to the base needs to go via the derived, or via something that knows about both of them, or by creating a link from base to derived无论您如何安排它,您都需要利用派生了解基础而不是相反,因此对基础所做的任何事情都需要通过派生,或者通过了解它们两者的东西,或者通过创建一个从基础链接到派生


In your specific case you're having a problem because the class that knows about both of your data items (the base class) only sets one of them在您的特定情况下,您遇到了问题,因为知道您的两个数据项(基类)的类只设置其中一个

You could literally trigger the setting of the string by changing the value of the Public_herunterfahrenTimer to anything:您可以通过将Public_herunterfahrenTimer的值更改为任何值来触发字符串的设置:

public void Minuten_Zu_Sekunden_Umrechnung()
{
    _sekunden = (_minuten * 60); 
    Public_herunterfahrenTimer = ""; //any string, the value is not used
} 

You could also put the setting of _sekunden into Public_herunterfahrenTimer and then set that property in the derived method您还可以将 _sekunden 的设置放入 Public_herunterfahrenTimer,然后在派生方法中设置该属性

But structuring the code like this, and actually the code structure in general is very strange- we simply wouldn't write code like that.但是像这样构造代码,实际上一般的代码结构很奇怪——我们根本不会写那样的代码。 There is no need for a string property that duplicates information that is remembered in the _sekunden variable, and does do on the set , which naturally allows data to go out of sync不需要复制在 _sekunden 变量中记住的信息的字符串属性,并且确实在set上执行,这自然允许数据不同步

Instead you should perhaps just calculate the string in a get :相反,您也许应该只计算get中的字符串:

public class Zeiteinheit : ShutdownTime
{
     public int Minuten
     {
        get { return _sekunden / 60; }
        set { _sekunden = (value * 60); }
     }
}

public class ShutdownTime
{
    protected int _sekunden;

    public string HerunterfahrenTimer 
    {
        get { return $"-s -t {_sekunden}"; }
    }
}

There is no possibility now for these to be out of sync;现在它们不可能不同步; setting the minutes means the seconds are updated and the shutdown argument string will be generated when it is requested设置分钟意味着更新秒,并在请求时生成关闭参数字符串

note, I've also fixed your code up to regular C# conventions- strive to only use snake case on CONSTANTS_LIKE_THIS, don't prefix public things with "Public" and be consistent with the leading underscores on field names;请注意,我还将您的代码修复为常规的 C# 约定 - 力求仅在 CONSTANTS_LIKE_THIS 上使用蛇形大小写,不要在公共事物前加上“Public”,并与字段名称的前导下划线保持一致; all or none全部或没有

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

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