In April, Wang C returned from the computer language ranking list and went back to the throne, killing Java. In front.
C ++ is object-oriented and can meet the original intention of C ++: A Better C! C ++ has to be compatible with C, so we can still see the functions and variables written outside the class. C # is different. Combined with the respective advantages of Java and C ++, completely object-oriented. What I want to talk about today is to use C to imitate the object-oriented in C #. Note that it is only to imitate ~~~
1) C # has class, so it should be replaced by the struct in C;
2) If the C # class contains a member function, use a function pointer in C;
The following defines a class and struct to represent a form. When you click a form, the system prompts the Form ID and the Form title:
Class in C:
Class in C #
1 Class Window
2 {
3 Private String Wndtext;
4 Private String Wndid;
5 Public Window ( String Text, String ID)
6 {
7 Wndtext = Text;
8 Wndid = ID;
9 }
10 Public Void Onclick ()
11 {
12 Console. Write ( " Form " + Wndid + " The title is: " + Wndtext );
13 }
14 }
ClientProgram:
1 Window win = New ( " Instance form " , " Zero X 12345678 " );
2 Win. onclick (); // Display Information
Struct in C
1 Struct window
2 {
3 Char * Wndtext;
4 Char * Wndid;
5 Void ( * Onclick) (char * Text, char * ID ); // Function pointer 6 };
Client Program:
Code
Void Fun ( Char * Text, Char * ID)
{
Printf ( " Form % s title: % s " , ID, text );
}
Window win;
Win. wndtext = " Form in C " ;
Win. wndid = " Zero X 87654321 " ;< P>
win. onclick = fun; // function pointer
win. onclick (win. wndtext, win. wndid); // call its member functions like a class "
if you love programming, love C. If you love C, so please love pointers; if you love pointers, please love pointers.