gpt4 book ai didi

c# - 什么术语用于描述两个类何时相互依赖?

转载 作者:行者123 更新时间:2023-11-30 20:13:25 24 4
gpt4 key购买 nike

我在 C# 中有以下两个类:

public class MyFirstClass : IMyFirstClass
{
MySecondClass mySecondClass;
public MyFirstClass(IMySecondClass mySecondClass)
{
this.mySecondClass = mySecondClass;
}

public MyFirstClass() : this(new MySecondClass()){}
}

public class MySecondClass : IMySecondClass
{
MyFirstClass myFirstClass;
public MySecondClass(IMyFirstClass myFirstClass)
{
this.myFirstClass = myFirstClass;
}

public MySecondClass() : this(new MyFirstClass()){}
}

您会注意到,当这些类中的任何一个的默认构造函数被实例化时,系统将会崩溃,因为需要进行无限次实例化。

是否有官方术语用于描述此问题?

最佳答案

这被称为 circular reference :

A circular reference, sometimes referred to as a run-around, is a series of references where the last object references the first, thus causing the whole series of references to be unusable.

关于c# - 什么术语用于描述两个类何时相互依赖?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1592672/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com