在多个类之间共享属性

发布于 2024-07-23 06:36:23 字数 236 浏览 4 评论 0原文

我有一个具有很少属性 A、B、C 的类。我将其视为输入对象,因为我在逻辑中使用它来基于这些属性的值运行一些规则。 现在,运行规则后,我创建了另一个类作为输出,具有相同的属性和相同的值,此外还有一些特定于输出的其他属性。

现在,我的问题是如何在输入类和输出类之间共享这些属性(A、B、C),而无需手动将输入中的值分配给输出。 您能为我推荐一个我需要使用的有效设计吗? 抽象类的概念在这里出现了吗? 或者还有其他有效的方法吗?

I've a class with few properties A, B, C. I consider this as an input object as I use this in the logic to run some rules based on the values of these properties. Now, after running the rules, I created another class as an output with the same properties and with same values in addition to few other properties specific to the output.

Now, my question is how do I share these properties (A, B, C) among both the input and output classes without having to assign the value from input to the output manually. Can you suggest me an effective design I need to use? Is the abstract class concept comes into picture here? Or is there any other effective way?

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

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

发布评论

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

评论(5

素食主义者 2024-07-30 06:36:23

使用仅具有属性的中间类,然后从输入和输出对象访问该类的实例,但是在大多数情况下,将输入中的值复制到输出会更简单。

Use an intermediate class that only has the properties and then access the instance of that class from your input and output objects, however in most instances it would be simpler to copy the values from your input to your output.

≈。彩虹 2024-07-30 06:36:23

您是否考虑过使用 singleton 设计模式? 我不熟悉你的项目的规范,但看起来它可以帮助你,或者至少会给你方向。

Did you considered to use singleton design pattern? I'm not familiar with specification of your project, but seem like it can help you or at least will give you direction.

花开柳相依 2024-07-30 06:36:23

在不了解更多的情况下,我认为继承和多态性是可行的方法:

class input 
{
   public int A { get; set;}
   public int B { get; set;}
   public int C { get; set;}
}

class output : input {
   public int D { get; set;}
   public int E { get; set;}
   public int F { get; set;}
}

您可以简单地执行以下操作:

void main()
{
    output tpt = new output();
    tpt.A = 3;
    tpt.B = 2;
    tpt.C = 4;

    tpt = dosomething((input)tpt);
}

public output dosomething(input inpt)
{
    //blablabla
    output tpt = (output)inpt;
    tpt.D = 1;
    tpt.E = 2;
    tpt.F = 3;

    return tpt;
}

Without knowing more, i think inheritance and polimorphism is the way to go:

class input 
{
   public int A { get; set;}
   public int B { get; set;}
   public int C { get; set;}
}

class output : input {
   public int D { get; set;}
   public int E { get; set;}
   public int F { get; set;}
}

The you simple do:

void main()
{
    output tpt = new output();
    tpt.A = 3;
    tpt.B = 2;
    tpt.C = 4;

    tpt = dosomething((input)tpt);
}

public output dosomething(input inpt)
{
    //blablabla
    output tpt = (output)inpt;
    tpt.D = 1;
    tpt.E = 2;
    tpt.F = 3;

    return tpt;
}
谎言月老 2024-07-30 06:36:23

您可以在输出类上定义一个隐式运算符 (C#),如下所示:

public static implicit operator OutputClass(InputClass m) 
{
   this.A = m.A;
   this.B = m.B;
   this.C = m.C;
}

然后您所要做的就是:

output = input;

并且您将有效地从输入中引入您想要输出的所有数据。

You could define an implicit operator (C#) on the output class like so:

public static implicit operator OutputClass(InputClass m) 
{
   this.A = m.A;
   this.B = m.B;
   this.C = m.C;
}

Then all you would have to do is:

output = input;

and you would effectively bring over all the data from the input that you want in the output.

叹沉浮 2024-07-30 06:36:23

选项 - 单例 - 有效,但取决于重用场景。
您是否使用除“new MyObj()”之外的任何类型的工厂或实例模式? 如果是这样,工厂就可以完成这项工作 - 这将导致下一个方法 - 注入。 您的语言是否支持注入或某种类型(又名 - spring)。 您可以定义一个 setter 方法,但将其留给注入框架和/或工厂方法来处理/进行设置 - 再次取决于用例。

如果它基于运行时,您将需要使用组合 - 委托对原始对象或中间对象(依次包装 A、BC 等)的引用。

可能有点偏离领域,但我假设你的问题是一个简单的案例,只是为了为更大的问题获得一些建议。

属性是动态编译时还是运行时? 它们会改变吗(即处理来自数据库的动态更新)。 您是否需要在测试工具中运行或使用模拟值运行,然后在完整应用程序的上下文中进行测试?

Options - singleton - works but depends on reuse scenarios.
Are you using any type of factory or instance pattern other than "new MyObj()"? if so the factory can do the work - which leads to the next method - injection. Does your language support injection or some kind (aka - spring). You can define a setter method, but leave it up to the injection framework and or factory methods to handle / do setup - again depends on use cases.

If its runtime based, you will need to use composition - delegate to either a reference to the original objects or an intermediate object (which in turn wraps A, B C etc).

Probably a bit left of field , but i am assuming your question is a simple case just to get some suggestions for a larger problem.

Are the properties to be dynamic - compile time vs runtime ? are they going to change (ie. handle dynamic updates from a db). Do you need to run in a test harness or with mock values and then test in the context of the full application ?

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