abstract properties help
Is this how you get to my abstract class prope开发者_高级运维rties?
public interface IMyObj(
void myMethod();
)
public abstract class MyObjBase
(
public string myProperty{get; set;}
)
public class myObj : MyObjBase, IMyObj
(
)
public void SomeMethod(IMyObj myobj)
{
// is there another way to do this, seems awkward
(myobj as myobjBase).myProperty = "value";
}
Because myProperty
is declared public you can just use it as if it were local in the derived class, you do not need to cast to the base class type.
Where you would need to cast is if you had it declared as virtual
in the base class and overridden it in the derived class, then you need to be explicit if you want to call the base version.
It doesn't matter if the base class is implementing an interface and the derived class wants to call that version - it is still declared public (protected
would work too), and there is no override supersceding it.
The type of your parameter needs to be of type "myObj" (as the class implements both interface and abstract class) if you want to access both interface and abstract class members. Your intellisense rightfully tells you that if you pass in an interface, it expects any derived classes that implement that interface but has no way to determine what other abstract classes or interfaces you are implementing. I would suggest that:
public interface IMyObj
{
void myMethod();
}
public abstract class MyObjBase
{
public string myProperty { get; set; }
}
public class myObj : MyObjBase,IMyObj
{
public void myMethod()
{ }
}
public void SomeMethod(myObj myobj)
{
myobj.myProperty = "value";
}
Assuming, as in your example, that myobj
is an instance of IMyObj
, you would indeed need to cast myobj
in order to reference myProperty
. That's because an instance of IMyObj
knows nothing about the members of the classes MyObjBase
or MyObj
. It's kind of a strange thing to do though, and probably indicates a design error because code that wants a IMyObj
interface really shouldn't be aware of what the type of the object instance is. If you want to get at myProperty
from an instance of IMyObj
, that's a pretty good indication that the property needs to be defined in the interface itself.
However, for the purpose of understanding how the type cast would work in your example, note that you could cast to either the base or the derived class:
MyObjBase asBase = myobj as MyObjBase;
MyObj asDerived = myobj as MyObj;
asBase.myProperty = "Hello ";
asDerived.myProperty = "World"; // refers to the same property as previous line
If you concatenate the last two, you get:
string x = asBase.myProperty + asDerived.myProperty; // x = "WorldWorld"
Now, suppose that you have a virtual / override pair of property definitions, like this:
public virtual string myProperty { get; set; } // in MyObjBase
public override string myProperty { get; set; } // in MyObj
Then both asBase.myProperty
and asDerived.myProperty
still refer to the same property, but now it is myProperty
of the derived class. In fact, given the instance myobj
, there is no way to get at myProperty
of the base class (except through reflection). Moreover, given myobj
, code within the base class itself will no longer refer to its own myProperty
. The only code that can get to the property of the base class, is some method (or constructor) in the derived class.
For example:
class MyObjBase
{
public virtual string myProperty { get; set; }
public void Foo()
{
myProperty = "something"; // refers to derived class property, if
// it overrides myProperty
}
...
}
class MyObj: MyObjBase, IMyObj
{
public override string myProperty { get; set; }
public string Bar()
{
base.myProperty = "Hello ";
myProperty = "World";
return base.myProperty + myProperty; // returns "Hello World"
}
...
}
精彩评论