C# OOP Design
-
I have to design a library of classes. They are to inherit from a common base class. However I want to be able to call members of the class without making a distinction of which class it particularly is. I was thinking of an interface for the last point I mentioned. Question : Would I'll be able to make my classes inherit this interface without directly inheriting the base class (i.e. the base class is inherited by interface)??:wtf:
-
I have to design a library of classes. They are to inherit from a common base class. However I want to be able to call members of the class without making a distinction of which class it particularly is. I was thinking of an interface for the last point I mentioned. Question : Would I'll be able to make my classes inherit this interface without directly inheriting the base class (i.e. the base class is inherited by interface)??:wtf:
An interface is just a contract of members, that a class that inherits the interface has to implement. An interface can not inherit from a class. What do you mean by calling a member without making a distinction of which class it is? You have to specify in what class a method is, or the compiler won't know where to look for it. What are you trying to do really? --- b { font-weight: normal; }
-
I have to design a library of classes. They are to inherit from a common base class. However I want to be able to call members of the class without making a distinction of which class it particularly is. I was thinking of an interface for the last point I mentioned. Question : Would I'll be able to make my classes inherit this interface without directly inheriting the base class (i.e. the base class is inherited by interface)??:wtf:
XeoN-Kc wrote:
They are to inherit from a common base class. However I want to be able to call members of the class without making a distinction of which class it particularly is.
Surely, this already does what you want? Kevin
-
I have to design a library of classes. They are to inherit from a common base class. However I want to be able to call members of the class without making a distinction of which class it particularly is. I was thinking of an interface for the last point I mentioned. Question : Would I'll be able to make my classes inherit this interface without directly inheriting the base class (i.e. the base class is inherited by interface)??:wtf:
XeoN-Kc wrote:
They are to inherit from a common base class. However I want to be able to call members of the class without making a distinction of which class it particularly is.
Well if you have a set of classes like this:
public class A
{
public virtual void SomeMethod()
{
Console.WriteLine("Method from A");
}
}
class B : A
{
public override void SomeMethod()
{
Console.WriteLine("Method from B");
}
}
class C : A
{
public override void SomeMethod()
{
Console.WriteLine("Method from C");
}
}Now, if run this little program:
static void Main()
{
A actuallyB = new B();
A actuallyC = new C();
A actiallyD = new D();
actuallyB.SomeMethod();
actuallyC.SomeMethod();
actuallyD.SomeMethod();
}Then the output is:
Method from B
Method from CYou are referencing all as a reference of type A.
XeoN-Kc wrote:
Would I'll be able to make my classes inherit this interface without directly inheriting the base class
If you are going to do it by interfaces then the base class no longer matters as you can reference it with the interface type instead. They can share the same base class, or have completely different base classes. Look at methods that implement the IDisposable interface. What do they have in common? Nothing, except for the IDisposable interface. Does this help? ColinMackay.net "Man who stand on hill with mouth open will wait long time for roast duck to drop in." -- Confucius "If a man empties his purse into his head, no man can take it away from him, for an investment in knowledge pays the best interest." -- Joseph E. O'Donnell