Suppose I write a class with a implementation of a dozen methods, but one of the methods must be implemented by a user of my class. In other words, I need to force the heir to implement a certain method, otherwise my class should not work.
How to implement this in C #?
I was told to use the interfaces, but with them "the stone flower does not come out."
- "my class should not work" - the heir should not work? - Igor
- @Igor My class is the base class, and the heir is not mine. - Grizlov
- @Igor if you want to answer the question - write the answer. Answers-links in the comments - evil. - PashaPash ♦
- abstract methods - so normal? - Igor
- @Igor Answers-not links in the comments - also evil. write a detailed answer if you want to answer. do not limit yourself to a suggestive comment :) - PashaPash ♦
|
1 answer
Mark the class and method that is required to implement as abstract:
abstract class Base { // обязателен к реализации в не-абстрактных наследниках protected abstract void SomeMethod(); // не обязателен к переопределению в наследнике protected virtual void SomeNonAbstractMethod() { } } // не-абстрактный наследник class Child : Base { // если метод не реализован - компилятор выдаст ошибку protected override void SomeMethod() { } }
private override
? - interesting - Igor- @Igor studio reprinted :) corrected. - PashaPash ♦
- the second method and should not implement the heir. Or it should also be
virtual
, then it can be implemented, but not necessarily - rdorn - I know about
new
, but this is in an extreme case, it seems like when it is impossible, but I really want to. - rdorn - @rdorn added virtual. but in general the idea was to show that there could be non-abstract methods in abstract classes - PashaPash ♦
|