C
Incentives are needed primarily for general public members of classes, when there is a common parent between classes. This avoids duplication of the code.The inheritance is useful when you want to create a collection from different classes and use it as a common method from the parent without a specific type. There's more polymorphysics coming here.The inheritance comes from abstract to specific.When you inherit a specific type from abstraction, he takes over all the parent's performance. If you suddenly need two parents for some purpose, for example:class A1 { }
class A2 { }
class B : A1, A2 { } // ошибка компиляции
Then you will face the restriction of the C# language because only one parent can inherit.If you need members of both basic classes in the heir, if necessary, this restriction can be circumvented by inheriting basic classes one from the second:class A1 { }
class A2 : A1 { }
class B : A2 { }
But this bypass is not always appropriate, depending on the specific task. Or as in your case, when it's technically impossible to change parents' performance so they can inherit each other.The inheritance is used to collect a holistic object that can do anything. But this classroom usually takes into account the principle of shared responsibility (SRP from SOLID) when the class can do one thing and one thing.I mean, if it's like "Bomba" class, he can only tick and explode, he can't start or stop, move from one corner of the room to another, mug or wash his pants. Just ticking and blowing.I mean, in other words, it's just a desire to bury everything in one class - not a reason to inherit anything. Inheritance is not used. If you need a bomb before the explosion, create a Meukalo class, transfer it to the Bumba designer and use it before the explosion. Such an approach would make it easier to refine the application, for example, you once changed your mind and decided that it would not be in the console, but in the headphones. And, uh, if you've done it right, there's no need to change the Bomb class at all. That is the best of the principle of a single identity.By the way, the transfer of a class to another class designer-- it's called dependence (DI = Dependency Injection) and, to be more specific, it's "constructor Injection." And if you're one day... not if, but... when you're interested in one day, how from this pile of classes, you're going to get a whole whole whole and nowhere, and as fast and simple as possible, add new components, you're about to go to a class that can get addicted to designers (and not just them) automatically. When a class decides where to take the copy required for the current class in the designer and creates this copy for you automatically-- it's called he took control of the equators. So the non-current class creates copies by newand some other thing, it's called "control inversion" (IoC = Inversion of Control), and the class manager is called "container" above in the commentary and wrote @EvgeniyZ. Take your time on this subject, it's not for starters, just as long as you've got enough to know what it is.Oh, yeah, there are other interfaces. So they're very indirect to inherit. Their task is to establish a public implementation template for the current class, you can't identify private members in the interface. The same interface can be carried out by completely different classes. One class may run several interfaces at the same time.