san*_*p22 2 c# properties class
假设A类为:
public class A
{
private string _str;
private int _int;
public A(string str)
{
this._str = str;
}
public A(int num)
{
this._int = num;
}
public int Num
{
get
{
return this._int;
}
}
public string Str
{
get
{
return this._str;
}
}
}
Run Code Online (Sandbox Code Playgroud)
我想隐藏Str属性时,我构建类A如
new A(2)
Run Code Online (Sandbox Code Playgroud)
并希望隐藏Num财产时,我构建类A如
new A("car").
Run Code Online (Sandbox Code Playgroud)
我该怎么办?
单个班级不可能做到这一点.An A是一个A,具有相同的属性 - 无论它是如何构造的.
你可以有2个亚类的abstract A,以及工厂方法...
public abstract class A
{
class A_Impl<T> : A
{
private T val;
public A_Impl(T val) { this.val = val; }
public T Value { get { return val; } }
}
public static A Create(int i) { return new A_Impl<int>(i); }
public static A Create(string str) { return new A_Impl<string>(str); }
}
Run Code Online (Sandbox Code Playgroud)
但是:除非投出,否则来电者不会知道价值.
| 归档时间: |
|
| 查看次数: |
49 次 |
| 最近记录: |