|
/**************************************************************************************************************/
/* */
/* Polydemo.cs */
/* */
/* Demonstrates polymorphism. */
/* */
/* Modification history: */
/* 2008-12-14 Sir John E. Boucher Created */
/* */
/**************************************************************************************************************/
namespace Template
{
public class PolyBase
{
public virtual string
Name
{
get
{
return ( "PolyBase" ) ;
}
}
}
public class PolyChild : PolyBase
{
public override string
Name
{
get
{
return ( "PolyChild" ) ;
}
}
}
public class NoPolyBase
{
public string
Name
{
get
{
return ( "NoPolyBase" ) ;
}
}
}
public class NoPolyChild : NoPolyBase
{
public new string
Name
{
get
{
return ( "NoPolyChild" ) ;
}
}
}
public static class Template
{
[System.STAThreadAttribute()]
public static int
Main
(
string[] args
)
{
PolyBase a = new PolyChild() ;
PolyChild b = new PolyChild() ;
NoPolyBase c = new NoPolyChild() ;
NoPolyChild d = new NoPolyChild() ;
System.Console.WriteLine ( a.Name ) ;
System.Console.WriteLine ( b.Name ) ;
System.Console.WriteLine ( c.Name ) ; // This executes the "wrong" method
System.Console.WriteLine ( ((NoPolyChild) c).Name ) ; // Must cast
System.Console.WriteLine ( d.Name ) ;
return ( 0 ) ;
}
}
}
|
By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.
If a file you wish to view isn't highlighted, and is a text file (not binary), please
let us know and we'll add colourisation support for it.