1. Structural body
For the C + + language, there is little difference between structs and classes. The functionality that the class can implement, most of which can also be used with structs.
In C #, however, we think of structs as a substitute for a lightweight class. It is like a class with constructors, properties, member properties/data, and even operators. Note the struct constructor method must have an incoming parameter.
Of course, structs do not fully support all functions of the class.
First, structs cannot inherit. That is, structs do not want to be as flexible as classes, and code cannot be reused.
Second, it is also a very important point: a struct is a value type , and a class is a reference type . These two types of differences can be viewed in C # learning record 3, inside about class 1th.
In general, structs are used only when you define something that is smaller and simpler.
The declaration of structure is very simple, here is not an example.
2. Interface
C # and Java support structures, while C + + is not supported.
If you know what an abstract class is, then this interface is about the same thing as him.
The place where an interface is better than an abstract class is that when a class inherits an abstract class, the abstract class exists as a base class. The interface, in turn, mixes a contractual contract into the established inheritance tree.
This sentence can be understood as a single inheritance requirement for C # (that is, a subclass can inherit only one parent class), and when you want to "inherit" multiple classes, you cannot implement them through inheritance. And a class is one that can implement multiple interfaces.
1 Abstract class Walk 2 {3 Abstract Public void SomeMethod (); 4 }
1 Interface IWalk//interface naming generally starts with capital I 2 {3 void SomeMethod (); 4 }
The above two pieces of code, one thing abstract class, one is interface. Now consider that if we have another class run, we now need to create a person class that inherits both walk and run (people can run and jump).
Class Person:walk, run{}
This method cannot be implemented because a class cannot be composed of more than one parent class
Public class person:iwalk, run{}
This can only be achieved. In the inheritance tree, run is the parent of the person, and Iwalk is only the contract that is added to the person, without affecting the inheritance tree
Since the introduction of allowing multiple interfaces to be inherited, it naturally raises the question: What if there is a method with the same name in the two inherited interfaces?
1 InterfaceIprogrammer2 {3 voidWork ();4 }5 InterfaceIwriter6 {7 voidWork ();8 }9 Public classPerson1:iprogrammer, IwriterTen { One voidiprogrammer.work ()//Display implementation A { - - } the Public Virtual voidWork ()//implicit implementation - { - - } +}
As shown in the example above. As programmers and writers, there are ways to work, and if someone is programming and publishing books, then he achieves two jobs at the same time, and the best way to differentiate between two jobs is through display.
That is, the interface name. The form description of the method name. However, the display implementation cannot set the access modifier word (i.e., public,private, etc.), and he is implicitly considered public. It is also possible to display the implemented methods without using the Abstract,virtual,override,new modifier word. And for an object of Person1, accessing the work method directly from an object is understood to be a method that calls an implicit implementation. Cast the object to the corresponding interface type: Person1 p = new Person1 (); Iprogrammer Pasprog = p; Pasprog.work ();
It says that forcing an instance of Person1 to be converted to Iprogrammer. In addition to the above method, the other is to use the as operator.
Iprogrammer Pasprog = p as Iprogrammer;
The as operator has a left and right value of two. It converts the lvalue to the type indicated by the right value, and returns NULL if the conversion cannot be successful.
C # Learning Record 4--structure, interface