1. Interface-interface
1.1 Interface declarations start with I by default
1.2 Interface definition only declares methods, does not implement concrete method body
1.3 Interface defaults to public
1.4 Interface definition behavior (constraint behavior)
Public Interface // Defining Interfaces { void usermobile (); } Public class // Inheriting Interfaces { publicvoid// Implement Interface { Console.WriteLine (); } }
2. Abstract class-abstract
2.1 You can define abstract classes without implementing a concrete method body (abstract classes can have attributes, implemented methods, non-implemented methods)
There are abstract methods in class 2.2, and this class must be an abstract class
Class 2.3 and abstract classes can inherit abstract classes
2.4 Abstract methods in abstract classes if you inherit an abstract class
Public Abstract classTestName//Abstract class { Public stringName {Get;Set; }//Properties Public Abstract voidPlay ();//abstract method, method body not implemented Public voidEat ()//implemented Methods{Console.WriteLine (); } } Public classTextnamechild:testname//inheriting abstract classes (inheriting abstract classes must implement abstract methods) { Public Override voidPlay ()//Rewrite play class{Console.WriteLine (); } }
3. Differences
3.1 Abstract class can inherit only one abstract class, interface can inherit multiple interfaces
4. Instantiation
New Imeimobile (); // instantiation of the interface * * = new implements the class name of the interface (); New textnamechild (); // instantiation of an abstract class = new implements the class name of the abstract class ();
Interface and abstract class comparison