在以下情况下是否有效且合法并向下投:
public interface IA {
string Property1 {get;}
}
public class B {
public string Name {get;set;}
}
// class A doesn't have it's own fields, all IA methods/properties are implemented through B methods
public class A:B,IA
{
public string Property1
{
get {return this.Name}
}
}
.....
B b = new B();
A a = (A)b; // will it work ?
还是使用组合/聚合更好?
public class A :IA
{
private B b;
....
public string Property1 {get {return b.Name;} }
public A(B b) {this.b = b}
}
....
B b= new B();
A a = new A(b);