简体   繁体   English

从Base继承的基类属性

[英]Base Class Property Inheriting from Base

I am doing a code review and have stumbled upon a situation where an abstract class is inheriting from an abstract base class that has a property of the inheriting class. 我正在进行代码审查,并偶然发现一个抽象类继承自具有继承类属性的抽象基类的情况。

At the moment I am unable to debug the code to see how it would work, but I am assured that this has worked in production for many years. 目前我无法调试代码以查看它是如何工作的,但我确信这已经在生产中工作了很多年。

As such it is a mystery to me how the following logic would work, step by step. 因此,对于我来说,以下逻辑如何逐步发挥作用是一个谜。

Inheritor: 传承:

public class Inheritor : Base {
  ...
  properties
  ...
}

Base: 基础:

public class Base {
  public Inheritor { get; set; }
  ...
  other properties
  ...
}

How is this possible? 这怎么可能?

An example in which it will hopefully make sense at a conceptual level: 希望在概念层面上有意义的一个例子:

public class Boss : Employee {
  ...
  properties
  ...
}

public class Employee {
  public Boss { get; set; }
  ...
  other properties
  ...
}

There might be many kinds of employees, but every one might have a boss (who is a particular kind of employee). 可能有很多类型的员工,但每个人都可能有一个老板(谁是一个特定的员工)。 Recall that properties of non-primitive types are references , so it's not the case that an Employee contains a Boss which (because it is also an Employee ) contains a Boss and so on. 回想一下非原始类型的属性是引用 ,因此不是Employee包含Boss的情况(因为它也是一个Employee )包含一个Boss等等。 An Employee may refer to a Boss , which may refer to a Boss and so on. Employee 可以参考 Boss可以指 Boss等。 At some point, it stops at a Boss who doesn't have a Boss (the reference is null ), or it goes in circle. 在某些时候,它停在一个Boss谁没有一个Boss (基准为null ),或者它会在圈子。

I think you may be tripping over the relational concept of is-a versus has-a , where you are thinking that has-a somehow is constrained in the same way as is-a. 我认为你可能会绊倒is-ahas-a的关系概念,你认为它有某种方式与a-a一样受到约束。 An Inheritor is a Base and it would be very strange if the reciprocal were true: if a Base is also an Inheritor . Inheritor是一个Base ,如果倒数是真的,那将是非常奇怪的:如果一个Base也是一个Inheritor But no reason why it cannot be that Base has an Inheritor . 但没有理由不能让Base拥有一个Inheritor

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

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