Last active
August 29, 2015 14:10
-
-
Save arieljannai/877d7c88325bfdba1824 to your computer and use it in GitHub Desktop.
Example for Yoni - abstract class and ctor inheritance
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
namespace Yoni | |
{ | |
static class Program | |
{ | |
static void Main() | |
{ | |
AlmightyKing ariel = new AlmightyKing("Ariel", "Jannai", "Your Majesty", "The Milky Way"); | |
Console.WriteLine("calling SayName() of the base class - SimpleHuman"); | |
Console.WriteLine(ariel.SayNameOriginal()); | |
Console.WriteLine("\ncalling SayName() - the overrided function in AlmightyKing"); | |
Console.WriteLine(ariel.SayName()); | |
Console.WriteLine("\ncalling PresentMyself() - the abstract function which we had to implement"); | |
Console.WriteLine(ariel.PresentMyself()); | |
} | |
} | |
public abstract class SimpleHuman | |
{ | |
public string FirstName; | |
public string LastName; | |
public string NamePrefix; | |
public SimpleHuman() {} | |
public SimpleHuman(string sFirstName, string sLastName, string sNamePrefix) | |
{ | |
this.FirstName = sFirstName; | |
this.LastName = sLastName; | |
this.NamePrefix = sNamePrefix; | |
} | |
// this function can be overrided by class inherit from SimpleHuman | |
public virtual string SayName() | |
{ | |
return String.Format("{0} {1} {2}", this.NamePrefix, this.FirstName, this.LastName); | |
} | |
// this function must be implemented by inheritance classes | |
public abstract string PresentMyself(); | |
} | |
public class AlmightyKing : SimpleHuman | |
{ | |
public string Kingdom; | |
// passing firstname, lastname and birthday to the base constructor (from SimpleHuman) | |
public AlmightyKing(string sFirstName, string sLastName, string sNamePrefix, string sKingdom) : base(sFirstName, sLastName, sNamePrefix) | |
{ | |
this.Kingdom = sKingdom; | |
} | |
public override string SayName() | |
{ | |
// just to add some exclamation marks | |
return String.Format("{0} {1} {2} !!!!!!!!!!!", this.NamePrefix, this.FirstName, this.LastName); | |
} | |
// calling the base class (SimpleHuman) SayName function, in case I'll want this. | |
public string SayNameOriginal() | |
{ | |
return base.SayName(); | |
} | |
public override string PresentMyself() | |
{ | |
return String.Format("You will call me:\n\'{0} {1} {2}\'!\nI'm the supreme ruler of the kingdom named \'{3}\'.\nAdios! XOXO", this.NamePrefix, this.FirstName, this.LastName, this.Kingdom); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
calling SayName() of the base class - SimpleHuman | |
Your Majesty Ariel Jannai | |
calling SayName() - the overrided function in AlmightyKing | |
Your Majesty Ariel Jannai !!!!!!!!!!! | |
calling PresentMyself() - the abstract function which we had to implement | |
You will call me: | |
'Your Majesty Ariel Jannai'! | |
I'm the supreme ruler of the kingdom named 'The Milky Way'. | |
Adios! XOXO |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment