C# 当子类从父类继承时,是否有方法对继承的字段使用不同的数据类型来添加更多功能?
当子类从父类继承时,是否有方法对继承的字段使用不同的数据类型,以向不同子类中的字段添加更多功能?然后将这些子类用作单个数据类型,可以用作函数中的参数 我需要创建两个对象,它们有一些相似之处,但不同程度足以保证拥有不同的类。所以我认为为他们建立一个基类是合适的。我在基类“ParentBase”中创建了两个道具,其中包含供子类使用的共享内容,子类需要为这些共享道具添加更多功能 例如,ParentBase中的设置字段应在Parent1和Parent2类中扩展,以满足其自身的独特需要。我觉得我需要创建新的数据类型来扩展Parent1和Parent2类的Settings字段C# 当子类从父类继承时,是否有方法对继承的字段使用不同的数据类型来添加更多功能?,c#,oop,inheritance,C#,Oop,Inheritance,当子类从父类继承时,是否有方法对继承的字段使用不同的数据类型,以向不同子类中的字段添加更多功能?然后将这些子类用作单个数据类型,可以用作函数中的参数 我需要创建两个对象,它们有一些相似之处,但不同程度足以保证拥有不同的类。所以我认为为他们建立一个基类是合适的。我在基类“ParentBase”中创建了两个道具,其中包含供子类使用的共享内容,子类需要为这些共享道具添加更多功能 例如,ParentBase中的设置字段应在Parent1和Parent2类中扩展,以满足其自身的独特需要。我觉得我需要创建新
class ParentBase
{
public ChildA Settings { get; set; }
public ChildX MoreSettings { get; set; }
// lots of shared props here that won't be extended in inheriting classes
public void SomeFunction()
{
// The inheriting class's Settings and MoreSettings props should be available to access here
// even though their data types are different to the base class's Settings and MoreSettings
// data types
}
}
class Parent1 : ParentBase
{
public ChildB Settings { get; set; }
public ChildY MoreSettings { get; set; }
}
class Parent2 : ParentBase
{
public ChildC Settings { get; set; }
public ChildZ MoreSettings { get; set; }
}
class ChildA { // base props and methods in here }
class ChildB : ChildA { // Parent1 specific functionality }
class ChildC : ChildA { // Parent2 specific functionality }
class ChildX { // base props and methods in here }
class ChildY : ChildX { // Parent1 specific functionality }
class ChildZ : ChildX { // Parent2 specific functionality }
我还需要在基类之外创建函数,将Parent1或Parent2对象作为参数。例如:
public void Calculate(SomeSharedType Parent1/Parent2 instance)
{
// need to access the Settings and MoreSettings properties here, and the base class's Setting should suffice,
// although it would be nice to access the inheriting class's Settings and MoreSettings properties
}
我有没有办法通过继承或接口来实现这一点?这是否回答了您的问题
class ParentBase<T,U>
{
public virtual T Settings { get; set; }
public virtual U MoreSettings { get; set; }
}
class Parent1 : ParentBase<ChildB, ChildY>
{
public override ChildB Settings { get; set; }
public override ChildY MoreSettings { get; set; }
}
class Parent2 : ParentBase<ChildC, ChildZ>
{
public override ChildC Settings { get; set; }
public override ChildZ MoreSettings { get; set; }
}
类ParentBase
{
公共虚拟T设置{get;set;}
公共虚拟用户设置{get;set;}
}
类Parent1:ParentBase
{
公共覆盖ChildB设置{get;set;}
公共覆盖儿童设置{get;set;}
}
类Parent2:ParentBase
{
公共覆盖ChildC设置{get;set;}
公共覆盖ChildZ MoreSettings{get;set;}
}
尽管您应该注意,只有当您想要更改属性行为时才需要重写,但为了仅更改类型,以下代码就足够了:
class ParentBase<T,U>
{
public T Settings { get; set; }
public U MoreSettings { get; set; }
}
class Parent1 : ParentBase<ChildB, ChildY>
{
}
class Parent2 : ParentBase<ChildC, ChildZ>
{
}
类ParentBase
{
公共T设置{get;set;}
公共设置{get;set;}
}
类Parent1:ParentBase
{
}
类Parent2:ParentBase
{
}
您可能应该将父类定义为generic@Mahsa,你是说ParentBase类吗?听起来你想要多态行为。泛型允许您注入类型。不要忘记字符串化Json。作为字符串,任何类模型都可以建模。这意味着字符串最终可以是任何东西。感谢您花时间回答我的问题,并帮助我更好地使用泛型。我对你的答案投了更高的票,但它不会被注册,因为我还没有15%的声誉。至于我的问题,我已经在代码中创建了泛型地狱。我要回到绘图板上,找到另一种方法。