C # abstract classes and virtual methods

Abstract classes and abstract methods:
1. Classes modified with the keyword abstract are called abstract classes.
2. Abstract classes only use the behavior of a class and cannot be used by creating objects alone. Using new is wrong.
3 The abstract class may have abstract methods, or there may not be any abstract methods.
4. The abstract class can not be static (static) or sealed (sealed)

The concept and use points of abstract methods:
1. Use abstract modified methods in abstract classes, called abstract methods.
2. Abstract methods must be defined in abstract classes and cannot be used in ordinary classes.
3. Abstract methods can only be The declaration of a method cannot have any method body.
4. An abstract method only represents a behavior that should be performed. The specific implementation is implemented by a subclass.
5. The abstract method is implemented (overridden) in the subclass and must use the keyword override .
All the abstract methods of the parent class 6. subclass must override, unless the subclass is itself an abstract class.


Polymorphism:
Different objects that receive the same message and produce different behaviors are called polymorphisms.

Use inheritance to achieve polymorphism:
there must be abstract methods or virtual methods in the
parent class . Subclasses must override abstract methods or virtual methods
in the parent class . Subclass objects must be converted into parent class objects to use.

LSP: Li's replacement principle:
1. Subclass objects can replace their parent classes.
2. Parent class objects cannot replace subclasses.
3. Parent class methods must be implemented or rewritten in subclasses.

is and as operators:
is, checks whether the object is compatible with the specified type, and the program is interrupted if the conversion fails.
as is used to perform conversion between compatible reference types, and returns null if the conversion fails.

The abstract method is just a statement, without any implementation content.
Abstract methods must be rewritten in subclasses before they have use value.

To the abstract method:
If you need to provide a method in the parent class, the method has its own method body, and the subclass decides whether to rewrite the method according to its own needs, rather than having to rewrite it.

The parent class method is defined as: virtual, the subclass can be rewritten or not. The
virtual method can be polymorphic like the abstract method. When the
subclass does not rewrite the virtual method, directly call the virtual method of the parent class.

Constructor, used for initialization during object creation.

Equals supports reference equality by default, if you want the objects to be the same, override Equals

Virtual methods and abstract methods
are decorated with virtual . To have a method body, even if it is a semicolon, it can be overridden by subclasses. Except for sealed classes, you can write
abstract modifiers. Method bodies are not allowed. They must be overridden by subclasses. Class.

About Equals () virtual method:
for string and value types can automatically compare whether they are equal.
For object comparison must be rewritten to make sense.

About ToString () virtual method:
By default, the fully qualified type name of the object is returned, which can be rewritten as needed.
For the value type, because of the rewritten method, the string representation of the variable value is returned.

 

Guess you like

Origin www.cnblogs.com/sunliyuan/p/12687999.html