父类需要了解他的子类

发布于 2024-09-14 13:49:12 字数 528 浏览 3 评论 0原文

问题:父抽象类必须了解其所有现有的子类。父类还必须能够通过一个方法列出他的子类的所有类名。

上下文:在我的情况下,父类用于表示 Web 表单的任何输入数据类型(例如电子邮件、文本、布尔值、整数、图像等)。子类代表每个特定的数据类型。因此有一个 Boolean 类、一个 Image 类、一个 Integer 类等。

我使用的语言是 C#。

你会怎么做?

编辑:我需要知道子类的原因是因为我需要知道所有可用的输入数据类型才能列出它们。另外,我需要访问每种数据类型(子类)的方法以获取其属性。

因此,例如,我需要简单地使用存储在数据库中的字符串“Image”来获取 Image 类的实例。如果基类知道它的所有子类,我将能够要求它获取由字符串“Image”表示的子类,并且它将返回 Image 类的实例。

我想通过使每个子项成为 Singleton 并将其自身添加到子项列表(在构造时)来实现它,该子项列表将是 DataType 基类的私有字段。

Probem: A parent abstract class must be aware of all his existing child classes. The parent class must also be able to list all the class names of his child classes through a method.

Context: In my situation, the parent class is used to represent any input data type for a web form (such as email, text, boolean, integer, image, and so on). The child classes represent each specific data types. So there is a Boolean class, an Image class, an Integer class, etc.

The language I'm using is C#.

How would you do that?

edit: The reason why I need to know the child classes is because I need to know all the input data types available in order to list them. Also, I need to access the methods of each data type (child class) to get its properties.

So, for example, I need to get an instance of the Image class simply with the string "Image" that is stored in a database. If the base class knowns all its children, I will be able to ask it to get me the child represented by the string "Image", and it will return an instance of the Image class.

I thought of implementing it by making each child a Singleton, and adding itself to a list of children (at construction) that would be a private field of the base DataType class.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

冧九 2024-09-21 13:49:12

您可以查看这篇文章。看起来您需要使用 Type.IsSubclassOf 迭代已知类型并查看它们是否是父类的子类

You can check out this post. Looks like you need to iterate through known types and see if they are a subclass of the parent class using Type.IsSubclassOf

尴尬癌患者 2024-09-21 13:49:12

您可以在构造函数代码中编写通知某些静态类已创建新内容的代码。

类似于:

class A {
 A() {
  myBuilder.notifyMe(this);
 }
}

myBuilder 是一个静态类,其代码:

void notifyMe(Object o) {
 // do whatever you want here
 // maybe something like
 someList.Add(typeof(o).Name)
}

编辑:
好吧,这个解决方案当然需要先实例化子类。
如果您想在程序启动时了解所有子类,我认为唯一的方法是扫描整个类路径并检查每个类是否是所需类的子类。

You can write in the constructor code that will notify some static class that something new was created.

Something like:

class A {
 A() {
  myBuilder.notifyMe(this);
 }
}

myBuilder is a static class with code:

void notifyMe(Object o) {
 // do whatever you want here
 // maybe something like
 someList.Add(typeof(o).Name)
}

edit:
Ok, this solution, of course, requires that the subclasses are instantiated first.
If you want to know all the subclasses when program starts I think the only way is to scan entire class path and check for each class is it subclass of wanted one.

简单 2024-09-21 13:49:12

让父母意识到自己的孩子是一种强烈的代码味道。它引入了一种令人讨厌的循环依赖。它尽可能公然地遵循依赖倒置原则(依赖于抽象,而不是具体)(使抽象类实际上依赖于其自身的具体实现)。它消除了作为继承核心的关注点分离。

简而言之,您应该进行重新设计。我必须更多地了解问题背景才能就此提供具体建议。

Making a parent aware of its children is a strong code smell. It introduces a nasty type of circular dependency. It treads on the Dependency Inversion Principle (depend on abstractions, not concretions) about as blatantly as possible (making an abstract class actually depend on concrete implementations of itself). It does away with the separation of concerns which is at the heart of inheritance.

In short, you should be in for a redesign. I would have to know more of the problem context to give specific advice on that.

快乐很简单 2024-09-21 13:49:12

很抱歉复活这个,但我认为您正在寻找的是一个了解所有实现的工厂,而不是一个了解其所有子级的基类。工厂基本上会知道它可以基于字符串构造的所有类型。

请原谅我,因为我不再熟悉 C# 语法,但它会类似于以下内容:

public class Control // (could be base class or whatever is needed)
{

}

public class ImageControl extends Control
{

}

public class ControlFactory
{
    public static Control createControl(String control)
    {
        if(control == "IMAGE")
        {
            return new ImageControl();
        }
        // error situation, control was unknown.
    } 
}

就像其他人评论的那样,父级了解其子级通常是一个非常糟糕的主意。

Sorry to resurrect this but I think what you are looking for is a factory that is aware of all of the implementations, not a base class that is aware of all of its children. The factory would basically know all types that it can construct based on the string.

Forgive me because I'm not too familiar with C# syntax anymore, but it would be something like the following:

public class Control // (could be base class or whatever is needed)
{

}

public class ImageControl extends Control
{

}

public class ControlFactory
{
    public static Control createControl(String control)
    {
        if(control == "IMAGE")
        {
            return new ImageControl();
        }
        // error situation, control was unknown.
    } 
}

Like other people have commented, a parent knowing about its children is typically a really bad idea.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文