Created
March 24, 2021 01:42
-
-
Save maximgorbatyuk/21ad2b6587658cea0b2600569fe2394d to your computer and use it in GitHub Desktop.
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
class Animal : ICloneable<Animal> | |
{ | |
public virtual Animal Clone() | |
{ | |
return new Animal(this); | |
} | |
} | |
// нет переопределения метода | |
class Dog : Animal | |
{ | |
} | |
// Есть переопределение | |
class Fish : Animal | |
{ | |
public override Animal Clone() | |
{ | |
return new Fish(this); | |
} | |
} | |
class Program | |
{ | |
private static void Main() | |
{ | |
Animal clone = new Fish().Clone(); | |
// Этот каст работает | |
Fish fish = (Fish) clone; | |
Animal cloneDog = new Dog().Clone(); | |
// Этот каст выдаст ошибку, cloneDog - это не класс Dog, а Animal | |
Dog dog = (Dog) cloneDog; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment