Java4android of the re-use class

Source: Internet
Author: User
Tags export class

This section contains notes and summaries of the seventh reuse class in the thinking in Java

1, there are two ways to reuse existing code without destroying the existing code: combination and inheritance.

2,tostring () method. The function is called when the class needs to be transformed into a string object. For example, there is an object A of Class A, a A; Then there is the statement "object:" +A; this time is actually "object:" +a.tostring ();

3, lazy initialization. There are many types of initialization of members, as we know in previous studies that if the base data type is not initialized, a default paper, such as int, will be initialized to 0, and if it is an object reference, it will be initialized to null. The initialization at the definition is earlier than the initialization of the constructor, but it can only implement times initialization. The initialization of a static member is initialized when the class is loaded, which is earlier. The initialization code can also be separated with curly braces, which is an effect of initializing at the definition. The lazy initialization is "initialize and then use" when you want to use the member. The initialization of this delay can often reduce the burden. Initialize it because it's needed.

The inheritance of classes in 4,java is implemented through extends, with inherited derived classes owning all members and methods of the base class.

5, each class can be equipped with a public static void main (string[] args) method, which facilitates unit testing. It is not a problem for multiple classes to have the main function at the same time, only the main function called by the command line will be executed. And even if a class is four, its main function can be arbitrarily called.
6, want to call the members of the base class, need to pass super.xxx (), otherwise if in XXX () again out of xxx () is recursive, obviously you do not mean it.

7, when you create an object that exports a class, the object contains a child object of a base class. This sub-object is the same as the object you created directly from the base class.

8, a constructor with parameters.

If there is no default base class constructor, or if you want to invoke a base class constructor with parameters, you must explicitly write the statement that calls the base class constructor with the keyword super, with the appropriate argument list. The call to the base class constructor must be the first thing you want to do when you export the class constructor (except that you call this call to other own constructs).

9, Agent. An agent is the middle of inheritance and composition. Because we put a member object in the class we want to construct (like a combination), at the same time we expose all the methods of that class member object (like inheritance) in the new class.

10, name masking. Note the different handling of overloads and overrides between C + + and Java. Java is generally overloaded, and if it has to be overridden, note that the function of the base class is overridden by adding @override to the function. Otherwise, they are overloaded.

11, choose between combination and inheritance. Both composition and inheritance place sub-objects in the new class, and the combination is explicitly done, and inheritance is implicit. How to choose? Combinatorial techniques are often used in new classes to have the functionality of an existing class rather than its interface. That is, embedding an object in a new class allows it to implement the required functionality, but the new class's users see only the interface of the new class definition, not the interface of the embedded object. At this time, the private object of an existing class is generally embedded in the new class. But sometimes it can be public (rarely), and public means that the outside can access the member, perhaps for ease of understanding.

At the time of inheritance, use an existing class and develop a special version of it.

12,protected keywords

"As far as the user is concerned, this is private, but it is accessible to export classes that inherit from this class, or to any other class in the same package."

In design, the best way is to keep the domain private, you should always keep the "change the underlying implementation" right, and then through the protected method to control the class's inheritors access rights.

13, Upward transformation

"Providing methods for new classes" is not the most important aspect of inheritance technology, the most important aspect of which is to represent the relationship between the new class and the base class. This relationship can be summed up with a "new class is a type of existing class". Why is it called upward transformation, because the previous class diagram structure always puts the base class on top, so it gets used to it. The upward transformation is always safe by converting a more specialized type to a more general-purpose type. The export class is a superclass of the base class, because it always contains all of the base classes and may have more methods and domains.

If a method is private, it is not part of the interface of the class. That is, the upward transformation of the time cannot be adjusted to it.

14,final keywords

Final can modify data, modify methods, classes.

1) Final data. The final modifier variable is so that it does not change, similar to C + + constants, and static final is often used to indicate that there is only one copy of memory. The final decorated field does not have to be initialized at the definition, and can be initialized when it is used, which is a bit like lazy initialization, which is called "blank final". After initialization, it cannot be changed. A final-decorated reference means that you cannot point the reference to a new object, but the data of the object itself can still be changed. The final parameter means that you cannot change this parameter (or refer to a reference) in this method, which is used primarily when passing data to an anonymous inner class.

2) Final method. Lock the method to prevent any inherited class from modifying its meaning, i.e. it will not be overwritten.

Final and Private keywords. All private methods in a class are implicitly specified as final. Because the export class cannot take the private method, it cannot be overwritten.

Overwrite occurs only if a method is part of the interface of the base class. If a method is private, it is not part of the base class interface. It is only the program code that is hidden from the class, except that it has the same name. This only.

3) Final class. A class is decorated to final, indicating that you do not want any class to inherit it. Of course the final class inside the final domain and the like, and ordinary class inside no difference. does not affect

15, initialization and loading of classes

The static code is loaded when the class is used for the first time. Suppose two classes a,b,b extens a. When the B object is generated, the static code of base class A is run first, then the static code of B, and then when the class code is executed, the object can be created. That is, executing object-related code.

Well, come here today.


Java4android of the re-use class

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.