Created
May 27, 2024 09:14
-
-
Save sunmeat/5bf7729a0887cde7f1955b2d8585e84e to your computer and use it in GitHub Desktop.
inheritance problems
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
abstract class Duck // абстрактная утка | |
{ | |
public virtual void Quack() | |
{ | |
Console.WriteLine("утка крякает!"); | |
} | |
public virtual void Swim() | |
{ | |
Console.WriteLine("утка плавает!"); | |
} | |
public virtual void Fly() | |
{ | |
Console.WriteLine("утка летает!"); | |
} | |
public abstract void Display(); // метод абстрактный, так как все утки будут отображаться по-разному | |
} | |
class MallardDuck : Duck // дикая утка | |
{ | |
public override void Display() | |
{ | |
Console.WriteLine("https://www.ndow.org/wp-content/uploads/2021/10/anas_platyrhynchos.jpg"); | |
} | |
} | |
class RedheadDuck : Duck | |
{ | |
public override void Display() | |
{ | |
Console.WriteLine("https://www.ducks.org/files/live/sites/ducksorg/files/Waterfowl%20ID/Diving%20Ducks/Redhead/Redhead%20drake.jpg"); | |
} | |
} | |
class RubberDuck : Duck // резиновая уточка | |
{ | |
public override void Display() | |
{ | |
Console.WriteLine("https://asiantigersgroup.com/wp-content/uploads/2023/06/AT-Duck-1.jpeg"); | |
} | |
public override void Fly() | |
{ | |
Console.WriteLine("УПС! резиновая уточка не летает!!!"); | |
throw new Exception(); | |
} | |
} | |
class Program | |
{ | |
static Duck GetSomeDuck() // генератор уток | |
{ | |
var rand = new Random(); | |
if (rand.Next(2) == 0) | |
return new RedheadDuck(); | |
else | |
return new MallardDuck(); | |
} | |
static void Main() | |
{ | |
Console.WriteLine("Наследование - это не всегда хорошо!"); | |
Duck duck = GetSomeDuck(); | |
duck.Quack(); | |
duck.Swim(); | |
duck.Fly(); | |
duck.Display(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment