制作一个 class 将包含一个 subclass 可以根据需要不同 classes
Making a class that will contain a subclass that can be different classes depending on the need
我不确定如何实现这个特定的想法我有一个 class 让我们称之为 EnhancedUserInput 它将具有所有输入类型都将具有的一些变量和一个特定的 subclass 取决于操作过程中的需要,所以一些额外的变量和一个列表,例如它的子 classes 将是 MultipleChoice,它将具有 MinSelection、MaxSelection 和一个名为 option 的类型的列表,带有自己的变量 ect 和然后另一个可能的子 class 称为 ExplicitAgreement,它将具有变量 inputLabel1、inputLabel2 和 BinaryInput 类型的列表,它们将具有自己的变量。
据我所知,最好的解决方法是拥有某种类型的通用变量?我将展示一些代码来尝试帮助获得我需要的东西,但我只是想知道是否有一种我不知道的简单方法来做到这一点?
public class EnhancedCustomerInput
{
public string Title { get; set;}
public bool ResponseOptional { get; set;}
public string CancelLabel { get; set;}
public string SubmitLabel { get; set}
// this is where I am unsure of how to go about it
public object inputType
{
MultipleChoice
ExplicitAgreement
}
}
public class MultipleChoice
{
public List<MultipleChoiceOption> Options { get; set; }
public int MinSelected { get; set; }
public int MaxSelected { get; set; }
}
public class ExplicitAgreement
{
public List<BinaryInputOption> Buttons { get; set; }
public string InputLabel1 { get; set; }
public string InputLabel2 { get; set; }
}
这个解决方案的最佳途径是什么我能想到一些可能的方法,但它们有点虚构,想知道是否有任何简单的方法?
在我看来,您可能理解错了。也许你想要的只是使用 class 继承?
public class EnhancedCustomerInput
{
public string Title { get; set;}
public bool ResponseOptional { get; set;}
public string CancelLabel { get; set;}
public string SubmitLabel { get; set}
}
public class MultipleChoice : EnhancedCustomerInput
{
public List<MultipleChoiceOption> Options { get; set; }
public int MinSelected { get; set; }
public int MaxSelected { get; set; }
}
public class ExplicitAgreement : EnhancedCustomerInput
{
public List<BinaryInputOption> Buttons { get; set; }
public string InputLabel1 { get; set; }
public string InputLabel2 { get; set; }
}
Steve Harris 的继承建议很好。您最初使用 Composition 的选项也可以正常工作:
public class EnhancedCustomerInput
{
public string Title { get; set;}
public bool ResponseOptional { get; set;}
public string CancelLabel { get; set;}
public string SubmitLabel { get; set; }
public object InputData { get; set; }
}
唯一的问题是您的代码的使用者需要知道 InputData
可以是几种不同类型中的一种,并且您可能需要针对其类型 switch
的逻辑。您可以向 属性 添加注释以给人们提示,或者您可以使用像 LanguageExt 这样的库,它提供 Either
类型:
public class EnhancedCustomerInput
{
public string Title { get; set;}
public bool ResponseOptional { get; set;}
public string CancelLabel { get; set;}
public string SubmitLabel { get; set; }
public Either<MultipleChoice, ExplicitAgreement> InputData { get; set; }
}
这使得 InputData
可以是哪些类型变得更加明显,但如果您有两种以上的可能性,将会变得非常笨拙。
您还可以声明一个 InputData
必须实现的接口,这将使开发人员更容易找到打算在那里使用的所有类型。但是空接口被认为是一种代码味道,因为它表明您正在将接口用于它们并非真正打算用于的东西。
另一个我发现效果很好的选项是定义一个 enum
类型来帮助识别您可以拥有哪些不同类型的输入数据:
public class EnhancedCustomerInput
{
public string Title { get; set;}
public bool ResponseOptional { get; set;}
public string CancelLabel { get; set;}
public string SubmitLabel { get; set; }
public InputType InputType { get; set; }
public object InputData { get; set; }
}
public enum InputType { MultipleChoice, ExplicitAgreement }
这为您的业务逻辑提供了一组特定的可能类型,您可以 switch
您的逻辑,并且在 class 将被序列化和反序列化时效果特别好,因为那样您可以告诉反序列化器将 InputData
反序列化为哪种特定类型的对象。
有很多选择,各有优缺点。
我不确定如何实现这个特定的想法我有一个 class 让我们称之为 EnhancedUserInput 它将具有所有输入类型都将具有的一些变量和一个特定的 subclass 取决于操作过程中的需要,所以一些额外的变量和一个列表,例如它的子 classes 将是 MultipleChoice,它将具有 MinSelection、MaxSelection 和一个名为 option 的类型的列表,带有自己的变量 ect 和然后另一个可能的子 class 称为 ExplicitAgreement,它将具有变量 inputLabel1、inputLabel2 和 BinaryInput 类型的列表,它们将具有自己的变量。
据我所知,最好的解决方法是拥有某种类型的通用变量?我将展示一些代码来尝试帮助获得我需要的东西,但我只是想知道是否有一种我不知道的简单方法来做到这一点?
public class EnhancedCustomerInput
{
public string Title { get; set;}
public bool ResponseOptional { get; set;}
public string CancelLabel { get; set;}
public string SubmitLabel { get; set}
// this is where I am unsure of how to go about it
public object inputType
{
MultipleChoice
ExplicitAgreement
}
}
public class MultipleChoice
{
public List<MultipleChoiceOption> Options { get; set; }
public int MinSelected { get; set; }
public int MaxSelected { get; set; }
}
public class ExplicitAgreement
{
public List<BinaryInputOption> Buttons { get; set; }
public string InputLabel1 { get; set; }
public string InputLabel2 { get; set; }
}
这个解决方案的最佳途径是什么我能想到一些可能的方法,但它们有点虚构,想知道是否有任何简单的方法?
在我看来,您可能理解错了。也许你想要的只是使用 class 继承?
public class EnhancedCustomerInput
{
public string Title { get; set;}
public bool ResponseOptional { get; set;}
public string CancelLabel { get; set;}
public string SubmitLabel { get; set}
}
public class MultipleChoice : EnhancedCustomerInput
{
public List<MultipleChoiceOption> Options { get; set; }
public int MinSelected { get; set; }
public int MaxSelected { get; set; }
}
public class ExplicitAgreement : EnhancedCustomerInput
{
public List<BinaryInputOption> Buttons { get; set; }
public string InputLabel1 { get; set; }
public string InputLabel2 { get; set; }
}
Steve Harris 的继承建议很好。您最初使用 Composition 的选项也可以正常工作:
public class EnhancedCustomerInput
{
public string Title { get; set;}
public bool ResponseOptional { get; set;}
public string CancelLabel { get; set;}
public string SubmitLabel { get; set; }
public object InputData { get; set; }
}
唯一的问题是您的代码的使用者需要知道 InputData
可以是几种不同类型中的一种,并且您可能需要针对其类型 switch
的逻辑。您可以向 属性 添加注释以给人们提示,或者您可以使用像 LanguageExt 这样的库,它提供 Either
类型:
public class EnhancedCustomerInput
{
public string Title { get; set;}
public bool ResponseOptional { get; set;}
public string CancelLabel { get; set;}
public string SubmitLabel { get; set; }
public Either<MultipleChoice, ExplicitAgreement> InputData { get; set; }
}
这使得 InputData
可以是哪些类型变得更加明显,但如果您有两种以上的可能性,将会变得非常笨拙。
您还可以声明一个 InputData
必须实现的接口,这将使开发人员更容易找到打算在那里使用的所有类型。但是空接口被认为是一种代码味道,因为它表明您正在将接口用于它们并非真正打算用于的东西。
另一个我发现效果很好的选项是定义一个 enum
类型来帮助识别您可以拥有哪些不同类型的输入数据:
public class EnhancedCustomerInput
{
public string Title { get; set;}
public bool ResponseOptional { get; set;}
public string CancelLabel { get; set;}
public string SubmitLabel { get; set; }
public InputType InputType { get; set; }
public object InputData { get; set; }
}
public enum InputType { MultipleChoice, ExplicitAgreement }
这为您的业务逻辑提供了一组特定的可能类型,您可以 switch
您的逻辑,并且在 class 将被序列化和反序列化时效果特别好,因为那样您可以告诉反序列化器将 InputData
反序列化为哪种特定类型的对象。
有很多选择,各有优缺点。