Page 7 Initialization and Assignment

xiaoxiao2021-03-06  37

2.3.3 Initialization and assignment

Here I have to remind you that initialization and assignment are completely different concepts. When you create a class, you call this class constructor to initialize the properties of the object. And if this object is given to other similar types of objects, it will be less simple. The value is directly assigned directly in Java, because the pointer is canceled in Java, and there is no deep copy of the pointer to the front copy issue. The copy constructor is required and the operator is overloaded in C . Because Java does not involve these things, I don't have excessive introduction. For details, please refer to the relevant books!

2.3.4 Destructor:

The pointer is no longer supported in Java, so you can't feel its importance because the system will automatically release memory for you. Everything in C is manual. In the constructor, New has a pointer, and it is necessary to delete this pointer in the destructor.

2.3.5 Static:

Now let's take a look at "Static" is a matter!

State a variable or function as static requires "static" keyword. Declare static purpose is to "a single storage space for all objects of a class or method". Static data is classified, not any object. Static data is in the statement that the system allocates memory space without waiting for the object. For example to help you better understand it.

Or follow the example above. I still remember the employees I just said able to use the microwave oven. Now we have to find a glove, after all, I want to take a hot meal from the microwave oven directly to pick it up. I define my glove into a Boolean variable, which has two states of clean and dirty. Think about who is the glove? All objects? wrong! Because only methods can belong to all objects. It belongs to the class. Like the method of the microwave oven, there is only one in memory, and all objects can be modified by the method. The next revision is based on the previous revision! I mean: an employee stains the gloves, and the next employee is still dirty. And after this employee was cleaned, it was clean when others used it again! Just do things, understand!

I don't think much about the static function. I feel that it is a class, and the memory is allocated when defined. Call is to call directly using the class name. Other and ordinary member functions have no difference, but here you need to explain that in Java, static member functions can only modify static properties, and static properties can be modified by all member functions. But there is not so much thing in C !

2.4 inheritance

Inherit is very good, its greatest benefit is "code reuse", which greatly improves work efficiency. You will understand for an example. Black and white TV in the world, it has its own principle. However, people have developed a color TV on his basis. Color TV inherits all the features and methods of black and white TV! Because it can display a color image or black and white images. However, there are many differences from black and white TVs in the principle of working. Color TV and multiple matrix circuits, separating color signals (RGB), and he can display colorful images. The black and white TV does not have this circuit, even if it receives a color signal, it does not appear a color image. Color TV is derived from black and white TV. Therefore, the black and white TV is a parent class, and the color TV is both a subclass, a color TV inherits all the characteristics and methods of black and white TV. Look at what it is like again in the computer:

//Bwtv.java definition of parent class bwtv {private int A; public bwtv () {a = 1;}

Public changebWTV (INT i) {a = i;}}} //ctv.java subclass definition Class CTV EXNTENDS BWTV {// Note Keyword "Extends" Private Int B; public CTV () {b = 2;} public ChangeTCV (INT X) {b = x;}} With the above definition, let's take a look at what data they have.

BWTV data includes CTV data including Private Int a private int a private int b public changebwtv (); public changebwtv () public changectv ();

You see, the subclasses have all the methods and properties of the parent class. Pay attention to the keyword "extends", it means inheritance. Use ":" operator in C . It is the same. But there are many problems here, first is the problem of access rights, and the subjects of the subclass have all the properties and methods of the parent class. Yeah? It is definitely right! (However, Java's book is not said so. He said that only the attributes and methods of non-private types, I think it has a mistake!) However, the object of the subclass cannot directly access the parent class private property or method. It can only be accessed by the public member function of the parent class. At this time, if you modify the value of the parent class's properties. That is really modified. I mean: The value of the private attribute of the parent class will change as the sub-object calls the public method of the parent class to change the changes to the corresponding attribute! (There is a problem with a domain, all modifications are made in subclasses, modifying the properties of the parent class inherited by subclasses (in subclavab, at this time, the parent class is copied into the subclass .). The properties of the parent class defined in the program do not change any changes (in the field of the parent class),)

Secondly, the constructor is the first to call the constructor of the parent class when creating a sub-class object, and then call the constructor of the subclass. After all, the constructor of the subclass does not include the initialization function of the attribute of the parent class! (From this point, my point of view is the correct "subclass of objects, all the properties and methods of the parent class") Of course, the sequence of call orders is just the opposite!

Let us now talk about Protected this keyword, it means: For objects, the variable that protected for protected is private, and the sub-parent class, the variable declares that protected is public.

There is a problem now, if we also define an int type variable A in subclasses, then call the child class definition or parent class definition when you create a subclass? This involves the hidden problem of data, I can tell you that it is definitely the variable A of the subclass of the call. Because the subclasses hide the same name variable of the parent class. And if it is a way? This involves the problem of reconstruction. I mentioned above "" Function Reconstruction refers to the method of declaring the same name as the parent class in subclasses, thus covering the method of the parent class. Reconstructing the subclass and the father The difference problem of class. "Here, you must declare that in Java, the subcatencies have a coverage of the hidden and parent class methods for the parent class properties, in the subclass, the subclass object can only call the sub-class itself. Attributes and methods. To call the properties and methods of the parent class, you must use this key. And in C is not like this. Because it has virtual functions.

Virtual functions are very fun things in C . We can declare the functions that need to be rewritten as virtual functions, and declare this keyword with Virtual. such. If we cwinApp such a base class, it defines a member (virtual) function for initInstance () and another (virtual) function initApplication (). If I derive a sub-class from CWINAPP as CMYWINAPP and modify the INITINSTANCE (). We did not modify the INitApplication () member function. Now we create a function of this class of CMYWINAPP, we create a pointer * PAPP points to this object theApp. At this time: PAPP-> InitInstance () // The pointer call is the true method of subclass CMYWINAPP

PAPP-> initApplication () // Pick-up CM CWINAPP

Because the subclass does not modify the parent class, the false method of the parent class is called. This involves the problem of the virtual you. It is not discussed here in the position of this article!

The type of conversion problem with the object of the parent class and subclass is such that the subclass object is converted to a parent class object, and there will be no errors. Because the subclass contains all the properties and methods of the parent class, it is hard to say when the parent class is converted to the subclass, huh, huh. This will also involve the problem of virtual table, nor discuss it!

Multiple inheritances are no longer supported in Java, which is inherited from more than two classes, but it has more interface concept "interfab". I don't have more introductions here!

There is nothing difficult to abstract base categories! One of his big profile is: to do a multi-class parent class, do not define objects, only derived!

I can do it too. If you can understand the six or seven points of the above, it is the biggest return to me, huh, huh! Just like I just started me, I just give you a probably thoughtful thinking, so that you still need your continuity efforts. There are still many things about programming languages, and it is a small personal ability to be limited and cannot be taken care of. But as an initiator, these things are basically. Need me to remind you, don't expect to understand what is first, two times! Come on :)

2004.4.29 Han Jingwei

I am willing to keep in touch with you. If you can make valuable comments and suggestions for this article, I will not be grateful. My email address is ONEGENIUS@126.com or leaves me a message in QQ (86228551 --- garbled soul .h)

转载请注明原文地址:https://www.9cbs.com/read-62397.html

New Post(0)