First of all, in learning abstract methods, we must understand why there are abstract methods in C #, or why abstract methods are needed.
We know that an interface can contain the declaration and properties of a method, but that the method does not contain code, and that the method that implements the interface must contain all the methods and properties in the interface, but now this situation is assumed to require an "interface" that requires the class to implement certain methods and properties, but this "interface" contains some code. Instead of having to implement some methods repeatedly in each derived class, you need an abstract class, not an interface, for this case.
By definition, abstract classes and general classes have similarities and differences, and interfaces have similarities and differences. The following author will be revealed to you.
The same point of the abstract class and the general class: You can inherit other classes or interfaces, you can derive subclasses, and there are specific methods;
Different points: Abstract classes have abstract methods, the general class is not, abstract classes can not be instantiated, the general class is possible;
Second, abstract class and interface of the same point: can not be instantiated, inheriting the subclass of the abstract class must implement abstract methods in the abstract class, the implementation of the interface subclasses must implement all the methods and properties in the interface.
Different points: Abstract classes In addition to abstract methods, there are specific methods.
Abstract classes differ from generic classes and interfaces in code representations:
1: abstract class Class_abstract_name;
2, General class: Class Class_name;
3, interface: interface interface_name;
4. Abstract method: The public is an abstract void test (), or public is the only method that must use the public modifier.
5, General method: public/private/protected/. void/string/int/. Method_name ();
6. Virtual method in parent class: public virtual void test_1 (), or virtual public void test_1 ();//virtual method must use the public modifier.
7. The virtual method or abstract method that implements the parent class in the subclass: public override void Test_1 (), or override public void test_1 ();
Attention:
Only abstract classes can have abstract methods, but abstract classes can have specific methods. If you put an abstract method in a class, you must identify the class as an abstract class.
The instance code is as follows:
[CSharp]View PlainCopy
- Using System;
- Using System.Collections.Generic;
- Using System.Linq;
- Using System.Text;
- Using System.Threading.Tasks;
- Namespace Testoverride
- {
- Class Program
- {
- static void Main (string[] args)
- {
- Test test = new test ();
- Test.test ();
- Test.test_1 ();
- Test.test_2 (); //Parent class, there are no methods in the subclass, subclasses can still use
- Console.readkey ();
- }
- }
- abstract class Method_abstract
- {
- abstract public void Test ();
- virtual public void Test_1 () {
- Console.WriteLine ("This is theTest 2.");
- }
- public void Test_2 () {
- Console.WriteLine ("This is theTest 3.");
- }
- }
- Interface Interface
- {
- //The methods and properties in the interface are public, so you do not have to enter public
- void Test ();
- string Test_string { get; set;} //This is an attribute in the interface
- }
- Class Test:method_abstract,interface//Abstract classes can certainly be used as a parent class, except for the above differences, there is no difference between abstract and generic classes
- {
- public override void Test ()//Implement abstract method of abstract class
- {
- Console.WriteLine ("This is theTest 1.");
- }
- override public void Test_1 () {//implements the virtual method in the parent class
- Console.WriteLine ("This is theTest 1.");
- }
- public string test_string { get; set;} //If all properties in the interface are not implemented in the subclass, the code will error.
- }
- }
The results of the code run are as follows:
Abstract classes and abstract methods are explained to this end!
C # Abstract classes and abstract methods