The front touches the abstract class, which is characterized by the way that subclasses must implement the abstract modification. As well as virtual modification methods, the virtual-modified method subclasses can be overridden or can be used without rewriting. However, because C # is also single-inheritance, it is not appropriate to define a parent class and then inherit to extend some classes. Because we programmers use programming language to describe the world, so our project organization class, naming class, writing methods, and so on, the best and the real world similar, so that the development is not laborious, modify the bug when it is easier.
For example, in a project, many forms are used, some forms need to be re-processed for their shutdown events, and some need to be handled for resizing events. However, it is always annoying to write one form at a time by writing down events or changing the size of events. Can I use inheritance at this time?
public class IControlAble1 {public event EventHandler oncloseevent;//close public void Control_close (object sender, EventArgs e) { if (oncloseevent! = null) { oncloseevent (sender, E) ; }}} public class IControlAble2 {public event EventHandler onsizechangeevent;//change size public void Control_ Sizechange (object sender, EventArgs e) { if (onsizechangeevent! = null) { onsizechangeevent ( sender, E);}}}
Imagine that every time you write a form class, it is always strange to inherit IControlAble1 or IControlAble2, because these two classes are not form classes. Then there is one thing that can be achieved, and that is the interface.
interface, as the book says, an interface is a code of conduct. And for the interface life is also very common, for example, we often say USB2.0 interface, USB2.0 is a criterion, all the production of USB cable manufacturers are not directly related, but we all abide by the principle of USB2.0 to produce, each line of the connection, the length of the height is how much, each data line how to row ... Have to abide by this rule. In this way, the lines that we produce can be used universally.
For programs, it's like different classes have the same superclass, and they all implement the abstract method of the superclass. So all classes have some sort of rule. However, C # is single-inheritance, which makes it difficult to implement. So there is the concept of the interface in C #. Look at the standard form and features of the interface:
<summary>/// 1, interface does not allow Public,private,interna, protected modification, because all the interfaces are common; ///2, Members without code body; ///3, Interface cannot define field members;/// 4, cannot use keyword static,virtual,abstract, sealed decoration/// 5, a class can implement multiple interfaces// </summary> Interface imyinterfaceable { //method, field int myvalue { get; Set; } void Method1 (); void Method2 (); }
The naming of interfaces in C # is generally: I***able,i represents interfaces, able suffixes. So the interface can be seen as: there are ... Ability.
This will solve the problem above. Modify the two classes to an interface, and then the other form classes are implemented.
Interface IControlAble1 {//No Code body, and do not decorate event EventHandler oncloseevent;//close void control_cl OSE (object sender, EventArgs e); } interface IControlAble2 {event EventHandler onsizechangeevent; void Control_sizechange (object sender, EventArgs e); The public class mainform:icontrolable1,icontrolable2//can implement multiple interface {/* form code */P Ublic event EventHandler Oncloseevent; public void Control_close (object sender, EventArgs e) {Console.WriteLine ("Override Shutdown event. "); } public event EventHandler onsizechangeevent; public void Control_sizechange (object sender, EventArgs e) {Console.WriteLine ("Overrides the size Change event. "); }} public class Childform1:icontrolable1 {/* * form code */public event Eventhandle R Oncloseevent; public void Control_close (object sender, EventArgs e) {Console.WriteLine ("OverrideCloses the event. "); }} public class Childform2:icontrolable2 {/* form code */public event EventHandler Onsizechangeevent; public void Control_sizechange (object sender, EventArgs e) {Console.WriteLine ("Overrides the size Change event. "); } }
In this way, the code is easier to write and does not leak.
The above is the basic knowledge of C #: Basic knowledge (8) interface content, more relevant content please pay attention to topic.alibabacloud.com (www.php.cn)!