有一个接口
public interface i<T>
{
void aa()
{
....
}}
继承接口 定下 下面 类
public class c1: i<string>
{
......
}
public class c2: i<int>
{
......
}
......//T的值不定 不局限于string ,int .怎么定义 下面属性
public class c
{
public i<T> p //这样不行,没有T
//public i<> p //这样也不行,不能直接<>
//public i p //这样也不行,没有i只有i<T>
{
get
{
if(1=1)
{
return new c1();
}
else if(1=2)
{
return new c2();
}
.......
}
set
{
....
}
}
//下面是调用
void b()
{
p.aa();
}}
public interface i<T>
{
void aa()
{
....
}}
继承接口 定下 下面 类
public class c1: i<string>
{
......
}
public class c2: i<int>
{
......
}
......//T的值不定 不局限于string ,int .怎么定义 下面属性
public class c
{
public i<T> p //这样不行,没有T
//public i<> p //这样也不行,不能直接<>
//public i p //这样也不行,没有i只有i<T>
{
get
{
if(1=1)
{
return new c1();
}
else if(1=2)
{
return new c2();
}
.......
}
set
{
....
}
}
//下面是调用
void b()
{
p.aa();
}}
{}
public interface I<T>
{
T P { get;set;}
} public class c1 : I<String>
{ #region I<string> 成员 public string P
{
get
{
throw new Exception("The method or operation is not implemented.");
}
set
{
throw new Exception("The method or operation is not implemented.");
}
} #endregion
}
public interface I<T>
{
T aa();
} public class c1 : I<String>
{ #region I<string> 成员 public string aa()
{
throw new Exception("The method or operation is not implemented.");
} #endregion
} public class c2 : I<int>
{ #region I<int> 成员 public int aa()
{
throw new Exception("The method or operation is not implemented.");
} #endregion
} public class c<T>
{
public I<T> p //这样不行,没有T
{
get
{
if (1 == 1)
{
return (I<T>)(new c1());
}
else if (2 == 2)
{
return (I<T>)(new c2());
}
else
{
return null;
}
}
} public T b()
{
return p.aa();
}
}
public interface I<T>
{
void aa()
{
}
} public interface IType
{
string Type {get;}
} public class c1 : I<String>,IType
{
public IType.Type
{
get{ return "c1"; }
}
public I<String>.aa()
{
}
public c1();
} public class c2 : I<int>,IType
{
public IType.Type
{
get { return "c2"; }
}
public I<int>.aa()
{
}
public c2();
} public class c
{
public object p
{
get {
if(1=1)
{
return new c1();
}
else if(1=2)
{
return new c2();
}
.......
}
set {
......
}
}
void b()
{
if(((IType)p).Type=="c1")
{
((I<String>)p).aa();
}
else if(((IType)p).Type=="c2")
{
((I<int>)p).aa();
}
}
}
这个方法很好可是 我总不能 void b()
{
if(((IType)p).Type=="c1")
{
((I<String>)p).aa();
}
else if(((IType)p).Type=="c2")
{
((I<int>)p).aa();
}
else if(.....) 这里还有很多很多了 每个都要判断啊
}