This article is original. If you need to reprint it, please indicate the author and the source. Thank you!
All week, in any object-oriented language (includingJava,C #Must be used to define abstract classes.AbstractKeyword. Although this is already commonAbstractIt is necessary to avoid disagreements when implementing interfaces or inheriting abstract classes.
See the followingCode:
Abstract Class Class1
{
Abstract Void Method ();
}
The above code is a typical abstract class. It is used when defining classes and methods.Abstract. But from the compiler's point of view, it is completely usable when defining a class.Abstract, As shown in the following code:
ClassClass1
{
Abstract VoidMethod ();
}
For the above Code, the compiler will not generate any singularity during compilation, as long as one of the classes is detectedAbstractKeyword code.Class1AddAbstractIn other wordsClass1AddAbstractShould be done by the compiler.
Although the above process does not seem to be a problem, it is not difficult to implement, but you should not forget that there is another way to implement the abstract class in addition to the above method. This is the implementation interface, instead of implementing all methods in the interface. See the following code:
Interface Myinterface
{
Public Void Method1 ();
Public Void Method2 ();
}
Abstract Class Myclass Implements Myinterface
{
Public Void Method1 ()
{
}
}
In the above CodeMyclassClass is not implementedMethod2Method, and shows how to use it when defining a methodAbstractKeyword, and then,Method2The method is actuallyAbstractMethod.
As you can imagine, if the abstract class is not definedAbstractWhat about the keyword? See the following code:
interface myinterface
{< br> Public void Method1 ();
Public void method2 ();
}< br> class myclass implements myinterface
{< br> Public void Method1 ()
{< br>
}< BR >}
The above code will definitely cause compilation errors because the compiler is blind. In object-oriented language, it is stipulated that a common class must implement all methods in the interface. In the above Code,Method2Method is not implemented. The compiler cannot determineMyclassClass is an abstract class or a common class. If the class is followed by a common class, an error occurs during compilation. If the class is followed by an abstract class, it fully complies with the object-oriented rules. Therefore, there is a disagreement. When the compiler is compilingSource codeOtherwise, it may be compiled into a binary target file with different meanings of the source code.
Of course, the above Code can also set the default rule, that is, when the common class cannot be processed, it will be processed according to the abstract class. However, this poses another problem. If a developer forgets the method to implement an interface, will the compiler consider this class an abstract class? Therefore, for the sake of insurance, the compiler designer specifically specifiesAbstractKeyword, that is, whether the class is an abstract class should be specified by the developer by encoding, rather than the compiler.
From the above description, we can see thatAbstractThe keyword is mainly used to avoid comments of common classes when implementing interfaces. If there is no interface in the object-oriented language,AbstractThe keyword can be removed. Of course, the inherited abstract class is similar to the implemented interface.
many static methods in object-oriented languages do not have the concept of static classes ( JAVA NO, C # yes ). Therefore, adding static when defining a class does not produce any singularity, therefore, static keywords are not required when defining static classes.