如何访问实例成员在静态方法? [英] How to have access to instance members in a static method?
问题描述
我试图创建类来封装像电子邮件,网址,电话号码等验证对象和逻辑。在第一次尝试,我发现我重复同样的code在所有类特殊静态的IsValid
和构造
。所以我决定创建一个基类把所有相同的codeS在里面。所以有基类其他类继承它。它的摘要为我不希望它被使用的直接。
公共抽象类的BaseClass
{
受保护的字符串值;
私人BOOL的isValid; 公共BOOL的IsValid {{返回this.isValid;}} 受保护的虚拟串RegexPattern {获得;} 受保护的虚拟RegexOptions RegexOption {搞定;} 私人BaseClase(){} 受保护的BaseClass(字符串值)
{
this.isValid = Validator.IsValid(价值,RegexPattern,RegexOption);
THIS.VALUE = this.isValid?值:的String.Empty;
} 公共静态布尔验证(字符串值)
{
返回Validator.IsValid(价值,RegexPattern,RegexOption); // RROR
}}公共类电子邮件
{
私人重写字符串RegexPattern
{
得到
{
返回*;
}
} 私人覆盖RegexOptions RegexOption
{
得到
{
返回RegexOptions.SingleLine;
}
} 公共字符串地址{{返回THIS.VALUE; }} 公共电子邮件(字符串的地址):基(地址){}
}
问题是与静态的BaseClass的的IsValid
方法。在目前的code将其抛出错误,因为 RegexPattern
和 RegexOption
是实例成员。我不想例如和静态方法定义的相同的值的两倍。它的工作原理,如果我用
常量字符串RegexPattern =*;
常量RegexOptions RegexOption = RegexOptions.SingleLine;
但我需要能够在子类来覆盖这些值,所以这是不适用的。正如我所定义的的BaseClass
为摘要
我不能初始化它的静态方法里面访问性能。
和我想用静态方法就像 Email.IsValid(foo@bar.com);
,我不知道如何通过一个实例通过它。
所以,我怎么能有机会获得 RegexOption
和 RegexPattern
在这个静态方法?
所以,我怎么能有机会获得PropOne和PropTwo在这个静态方法?
块引用>您需要有一个实例的不知何故的,否则它是一个毫无意义的操作。所以,问题是 - 你将如何希望的识别您感兴趣的实例呢?你的真正的需要
验证
是静态的呢?请注意,而不是有抽象属性,如果预计值在一个类的实例来始终保持不变,你可能希望构造函数的值部分用于
的BaseClass
代替,只是让他们在各个领域。如果你想达到什么样的是,每个子类都有一个单独的验证,我想分开的两个问题 - 给每个子类不同类型的的的静态属性的。你就不能调用此多态,但它听起来像你真的不想做了。
我们真的不能告诉你的班是为了重新在这里present - 如果你可以给我们更多的具体语境,我们大概可以提供更多的帮助。
。I'm trying to create classes to encapsulate validation and logic for objects like Email , URL , phone number and so on . in the first try I found that I'm repeating the same code in all classes specially static
IsValid
and theConstructor
. so I decided to create a base class to put all the same codes in it . so there is a base class that other classes inherit it . it's abstract as I don't want it to be used directly .public abstract class BaseClass { protected string value; private bool isValid; public bool IsValid{get { return this.isValid;}} protected virtual string RegexPattern{get;} protected virtual RegexOptions RegexOption{get;} private BaseClase(){} protected BaseClass(string value) { this.isValid = Validator.IsValid(value , RegexPattern, RegexOption); this.value = this.isValid ? value : string.Empty; } public static bool Validate(string value) { return Validator.IsValid(value ,RegexPattern, RegexOption); // rror } } public class Email { private override string RegexPattern { get { return ".*"; } } private override RegexOptions RegexOption { get { return RegexOptions.SingleLine; } } public string Address{get {return this.value; }} public Email(string address) : base(address){} }
the problem is with the static
IsValid
method of BaseClass . in the current code it throws errors asRegexPattern
andRegexOption
are instance members . I don't want to defined the same value twice for instance and static methods . it works if I useconst string RegexPattern= ".*"; const RegexOptions RegexOption =RegexOptions.SingleLine;
but I need to be able to override these values in sub classes so this is not applicable . and as I have defined the
BaseClass
asabstract
I cant instantiate it inside static method to have access to properties .and as I want to use static method just like
Email.IsValid("foo@bar.com");
, I don't know how to pass an instance through it .so , how can I have access to
RegexOption
andRegexPattern
in this static method ?解决方案so , how can I have access to PropOne and PropTwo in this static method ?
You need to have an instance somehow, otherwise it's a meaningless operation. So the question is - how would you want to identify the instance that you're interested in? Do you really need
Validate
to be static at all?Note that instead of having abstract properties, if you expect the values to always stay the same throughout an instance of the class, you might want to make the values part of the constructor for
BaseClass
instead, and just keep them in fields.If what you're trying to achieve is that each subclass has a single separate validator, I would separate the two concerns - give each subclass a static property of a different type. You wouldn't be able to call this polymorphically, but it sounds like you don't really want to anyway.
We can't really tell what your classes are meant to represent here - if you can give us more concrete context, we can probably be more helpful.
这篇关于如何访问实例成员在静态方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!