简体   繁体   English

派生类是否更新其父类属性?

[英]Do derived classes update their parents classes properties?

I have several classes that are derived classes from a parent, they all update some values and I don't know if they have their own version of these values or they are actually updating the parent's properties as the app is navigated and different classes are being used, I suspect it's the latter. 我有几个类是从父级派生的类,它们都更新了一些值,并且我不知道它们是否具有自己的这些值的版本,或者它们实际上是在导航应用程序时正在更新父级的属性,并且正在使用不同的类用过,我怀疑是后者。

It's the sideContent that I'm looking at, and any other property for that matter. 这是我正在查看的sideContent,以及与此相关的任何其他属性。

The parent: 父母:

export abstract class EngagementGraphNode {

  public sideContent: IEngagementSideContent;

  constructor(json?: IEngagementGraphNode) {
    if (json) {
      this.id = json.id;
      this.name = json.name;
    }
  }

Example of a child component: 子组件的示例:

export class EngagementProductGroup extends EngagementGraphNode {
  constructor() {
    super();
    this.placeholder = this.getIcon('more');
    this.icon = true;
    this.type = EngagementType.ProductGroup;
  }

  setProperties(json: IEngagementGraphNode, color: string): void {
    this.id = json.id;

      this.sideContent = {
        overview: { isLoading: true, title: 'Overview', id: 0 },
        members: { isLoading: false, title: 'Engaged Individuals', id: 1, individuals: this.individuals }
      }
    }

If this is the case, then I'm guessing at the point that the website is looking at that particular bit of code then that is updating the parent class as it goes along? 如果是这种情况,那么我猜到了这一点,即网站正在查看该特定代码段,那么它正在更新父类吗?

Thanks very much if you can help! 非常感谢您的帮助!

Every object has its own set of properties (instance variables) depending on how the property is declared. 每个对象都有自己的一组属性(实例变量),具体取决于声明该属性的方式。

If the parent property was declared with the static keyword (just like the public access modifier), then modifying the parent property from any of the children object would modify that value among all children objects (since it is being shared). 如果使用static关键字声明了父属性(就像public访问修饰符一样),则从任何子对象中修改父属性都会在所有子对象中修改该值(因为它是共享的)。

Since, they are not static in your case, each children has their own copy of the parent variable, which are safe from change when any of the children change their parent's variable. 由于在您的情况下它们不是静态的,因此每个子代都有自己的父变量副本,当任何一个子代更改其父变量时,可以避免更改。

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

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