Created
February 26, 2012 12:52
-
-
Save philipp-spiess/1916564 to your computer and use it in GitHub Desktop.
Polymorphismus C#
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
public class Fussballer : Sportler | |
{ | |
public void DoSport() | |
{ | |
Console.WriteLine("Fussball spielen!"); | |
} | |
} |
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
public class HokeySpieler : Sportler | |
{ | |
public void DoSport() | |
{ | |
Console.WriteLine("Hokey spielen!"); | |
} | |
} |
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
public class Programm | |
{ | |
public static Main() | |
{ | |
Fussballer f = new Fussballer(); | |
HokeySpieler h = new HockeySpieler(); | |
Sportler s; | |
// Polymorphie beschreibt jetzt des Verhalten, dass sich beidem wenn man sie auf Sportler castet, gleich ansoprechen lassen. | |
s = (Sportler) f; | |
s.DoSport(); | |
s = (Sportler) h; | |
s.DoSport(); | |
} | |
} |
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
public class Sportler | |
{ | |
public virtual void DoSport() { } | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment