Is base method able to use derived base data members?
Posted
by iTayb
on Stack Overflow
See other posts from Stack Overflow
or by iTayb
Published on 2010-03-17T13:31:03Z
Indexed on
2010/03/17
13:51 UTC
Read the original article
Hit count: 209
c#
Lets assume we have the following code:
abstract class Base1 {
protected int num;
}
class Der1:Base1 {
protected Color color;
protected string name;
}
class Der2:Base1 {
protected DateTime dthen;
}
and so on. An array of base1 type exists and includes many objects created out of classes that are derived from base1.
Is it possible to define the toString() method in the base class only?
something like:
public override string toString()
{
if (this is Der1)
return "num = " + this.num + "color = " + this.color.toString() + " name = " this.name;
if (this is Der2)
return "num = " + this.num + "dthen = " + this.dthen.toString();
// and so on ...
}
Thank you very much :)
P.S. This is not an homework question. I've just wondered about.
© Stack Overflow or respective owner