Java Programming Idea (vii, reuse Class)

Source: Internet
Author: User
Tags export class

Reusing code is one of the many compelling features of Java.

There are two methods for reusing a class. The first: Simply create an object of the existing class in the new class. Because the new class is made up of objects of existing classes, this method is called a combination. The second is to create a new class according to the type of the existing class. Instead of changing the form of an existing class, take the form of the current class and add new code to it. This approach is called inheritance.

1, the combination of grammar. Simply place the object reference in a new class. The compiler does not simply create a default object for each reference. If you want to initialize these references, you can do it in the following locations in your code: 1), where you define the object. This means that they can always be initialized before the constructor is called. 2), in the constructor of the class 3), just before using these objects, this is called lazy initialization. An empty one is initialized if it is empty. 4), use instance initialization.

2, inherit the syntax. When you create a class, it is always inherited. All classes in Java come from the root class object. In the inheritance process, you need to declare that the new class is similar to the old class. This declaration is implemented by writing the keyword extends followed by the base class name, before the left curly brace of the class body. When you do this, all the fields and methods in the base class are automatically obtained.

1), initialize the base class. Inheritance is not just an interface for copying a base class. 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. The difference is that the latter comes from outside, while the child objects of the base class are wrapped inside the exported class object.

2), a constructor with parameters. If there is no constructor, or if you want to invoke a base class constructor with parameters, you must explicitly write the statement of the base class constructor with the keyword super.

3, Agent. Java does not directly provide support for it. The introduction of code is easier to understand.

 Public class spaceshipdelegation{     privatenew  spaceshipcontrols ();       Public void back (int  velocity) {       controls.back (velocity);            }          }

Similar to the combination, but more than a combination of packaging a layer. This controls which methods are exposed by controls. If it is a combination, then all the methods in the controls will be exposed.

4. Ensure proper cleaning. In many cases, cleanup is not a problem, just the garbage collector's need to complete the action. But be careful when you have to handle the cleanup yourself. Because the garbage collector may never be called and is called immediately, it recycles objects in whatever order it wants. The best way is not to rely on the garbage collector to do anything except memory. If you need to clean up, write a cleanup method yourself, but do not use Finalize ().

 Public Static void Main (string[] args) {  cadsystem  new  Cadsystem ();      Try {   finally{     x.dispose ();   When the try block exits, it is bound to call the finally clause.  }          }    

5, the name of the screen. If the base class for Java already has a method name that has been overloaded multiple times (different argument lists). Redefining the method name in the export class does not mask any versions of it in the base class. As a result, the overloaded mechanism works fine, either at the layer or its base class Squadron method.

@override annotations are added to the Java SE5. It is not a keyword, but it can be used as a keyword. When you choose to overwrite a method, you can choose to add this annotation.

6. Choose between combination and inheritance. Both combination and inheritance allow child objects to be placed in a new class, and the combination is explicitly done, whereas inheritance is implicitly done. Generally, it's simple. If it's a "is-a" relationship, use inheritance. If it's a "has-a" relationship, use a combination.

7, protected keywords. In real projects, you often want to hide something as much as 8 of the world, but still allow members of the exported class to access them. The protected keyword can then be used.

8, upward transformation. A derived class reference is transformed up into a base class reference. Because the derived class must have a method of the base class. So this kind of transformation is certainly possible. The only thing that can happen with class interfaces is the loss of methods, not the acquisition of them.

9. Final key word. Here's a look at the final three usage scenarios: data, methods, and classes.

1), final data. One: A constant amount of compile time. Two: A value that is initialized at run time, but is no longer changed. This is for the base data type. When final modifies a reference, it means that the reference can never be changed again, and no longer points to other objects, but the contents of the object are subject to change.

2), blank final. The so-called blank final refers to a field declared final but not given an initial value. However, the compiler must ensure that the blank final must be initialized before it is used. You must assign a value to final in the definition of the domain or in each constructor with an expression.

3), final parameter. Java allows you to specify the parameters as final in the parameter list declaratively. This means that you cannot change the object that the parameter reference points to in the method.

4), final method. There are two reasons for using the final method. 1. Lock the method to prevent any inheriting class from modifying its meaning. You can ensure that the method behavior remains the same in inheritance and will not be overwritten. 2, when the final method is used. equals to the consent of the compiler to turn all calls to this method into inline calls. However, the method is now generally set to final only if you want to specify a static overlay.

5), final, and private keywords. All private methods in a class are implicitly specified as final. Because the private method cannot be taken, it cannot be overwritten. There is no point in adding a final modifier to the private method.

6), Final class. When the whole of a class is defined as final (before it is defined by the keyword final), it indicates that you are not going to inherit the class, and you are not allowed to do so.

10. Initialization and loading of classes. All things in Java are objects. So it takes a different load mode. The compiled code for each class is in its own standalone file, which is loaded only if the program code needs to be used. "The code for the class is loaded when it is first used." This usually refers to the load that occurs when the first object of the class is created, but also when the static domain or static method is accessed. The constructor is also a static method, although the static keyword is not explicitly written. So more accurately, a class is loaded when any of its static members are accessed.

The order of loading when running a class is this: Assume that the class name is Bettle. First Access Beetle.main () (a static method), the loader starts and finds out the compiled code for the Beetle class (in the file named Beetle.class). While loading it, the compiler notices that it has a base class (which is known by the keyword extends), so it continues to load. This happens regardless of whether you intend to produce an object of that base class. If the base class has its own base class, then the second base class is loaded, and so on. Next, static initialization in the base class is executed, followed by the next export class, and so on. This approach is important because the static initialization of the exported class may depend on whether the base class member can be initialized correctly. At this point, the necessary classes have already been loaded. You can then start creating objects. First, all the basic types in the object are set to the default values, and the object reference is set to null--, which is generated by setting the object's memory to a binary 0 value. The constructor for the base class is then called. In this case, it is automatically called. However, you can also use super to specify a call to the base class constructor. The base class constructor, like the constructor for the exported class, undergoes the same procedure in the same order. After the base class constructor is complete, the instance variables are initialized in their order. Finally, the rest of the constructor is executed.

Java Programming Idea (vii, reuse Class)

Related Article

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.