Java Learning Notes 6 - Reusing Classes

xiaoxiao2021-03-06  40

Reusing classes

There are two ways: composition and inheritance.

Composition vs. inheritance

1. Difference: Composition: *. Have-a, *. Due to the function of the Old Class: *. Don't want upcasting. *. Summary: In general, synthesis is used for new classes to use the functions of the old class, not The occasion of its interface. That is, the object is embedded, and it is used to implement the new class's function, but the user sees the interface of the new class, not the interface of the object embedded. Therefore, you have to embed the old class object in the new class. Inheritance: *. IS-a, *. Due to the OLD Class interface, *. Want upcasting *. Summary: Inheritance is to make a transformation of existing classes to get a special version. In short, you have to transform a more abstract class into a class that can apply to certain specific needs. I will know if I want to use a Vehicle (vehicle) object to synthesize a CAR (car) is meaningless -car does not contain Vehicle, which is a Vehicle. Inheriting is a "YES (IS-A)" relationship, while synthetic expression is to express "there is (HAS-A) relationship.

2. Synthesis or inheritance, then explore: In the programming of the iconic object, the most common programming and use of the code also simply encapsulates the data and methods to the class, and then use the object of that class. You can also create new classes on the basis of existing classes by synthesis. Inheritance is less common. So, although in the study of OOP, inherits a very important status, this is not to say that you can abuse everywhere. On the contrary, when you use inheritance, you should be as good as possible, you can use it when it can bring a significant benefit. When judging that the use of synthesis or inherits, there is a simpler method, just ask if you will transfer the new class to the base class. If you have to upload, then the success is that if you don't need to upload, then you should see if you should inherit it. The next chapter (polymorphism) will tell why use upload, but if you still remember to ask yourself "I need to upload?" , then you have a one can help you judgment This synthesis is still a good tool inherited.

3. Summary: Inheritance and synthesis can create new classes on the basis of existing classes. However, in general, the synthesis is to reuse the existing classes as part of the new class underlayer, and inheritance is multiplexed. Since the derived class has a base class interface, it can be uploaded (Upcast) to the base class,

Although the object-oriented programming will repeatedly emphasize inheritance, when you start designing, usually, you should first consider synthesis, only inheritance is used when necessary. Synthesis will be more flexible. In addition, members can make members use inherited objects so that you can replace these members' specific types, and their behavior. Thus, the behavior of the synthetic object can also be changed.

All non-Primitive objects have a toString () method, when the compiler needs a string and it is an object, the compiler will automatically call this method.

"" x can automatically convert X to string (if X is a non-Primitive object, then call the TSTRING method)

unit test

You can create MAIN (), and this is also a programming method worth advocating, because in this way, the test code can be placed in the class.

Even if the program includes many classes, it will only call the main () method of the class you given in the command line.

(As long as main () is PUBLIC, as for the class is not public, it is not important.)

This kind of main () approach to each class can make the class test make it easier.

Once you have finished the test, you don't have to remove main (); leave it to be used later.

Inheritance design has a general guideline, which is set to private, and the method is set to public.

Inheritance should be used in PRIVATE or protected? The best approach is to set the data member (fields) to private , then use protected permissions for Method to control the inheritance class access:

The new class of the object (this) contains the subobiect (Super) of Base Class:

From the perspective of outsiders, the new category has the same interface with the old class, and there may be

Some of its own methods and data. But inherit is not just a copy of the base class.

When you create a derived class object, there is a sub-object of a base class in this object.

This sub-objects are nothingordual with the objects created by the base class.

Just from the outside, this sub-object is wrapped in the object of the derived class.

The compiler will force you to call the code for the base class - Super (...), placed in the derived structure

The most in front of the number. In other words, there is no thing before it.

Try {

...

} finally {

...

}

TRY means that the following program (defined by the curly brackets) is a protected region that needs to give special attention.

The so-called special attention is that no matter what way to exit the TRY block, the FINALLY clause following this protected area must be performed.

(For abnormal processing, there will be many cases of non-normal to exit the TRY block.) Here

In many cases, the cleaning is not a problem; it will leave it to the garbage collector. but

If you want to do it yourself, you can only have worry, but also be careful, because

Nothing, no one can help you. Garbage collectors may never start. Even if it

Start, you can't control its recycling order. It is best not to rely on garbage collectors to do anything.

Things that are not related to memory recovery. If you want to clean up, you must write your own method, don't

Use Finalize ().

Progressive development

One of the advantages of inheritance is that it supports progressive development (INCREMENTAL Develop).

When adding new code, it will not bring bugs to the old code; in fact, new bugs are all in the new code.

By inheriting existing classes, you can work properly, then add some Fields and Method (and redefined the existing Method).

You can't modify the old code that may still have people, and thus will not cause Bug. Once Bug, you

I know it is definitely in the new code. Compared to the modification of the old code, the new code will be short, and it is easier to read. Although in the experimental stage, inheritance is a very useful technology, after the project enters the stable phase, you have to use a new look to examine the inheritance system, you have to compress it into a logical structure. . The program should not be turned around the bits, which should start from the problem space, express a method of solving the problem by creating and manipulating a variety of objects.

Upload (Upcasting)

Inherits the relationship between the new class:

"The Derived Class Is A Type of Base Class".

Detective class is supercharge (SuperSet), and the method of the base class is derived, so the message given to the base class can also be delivered to the derived class.

Final

Fields:

1.primitive: value cannot be modified

2. Non-Primitive (Object Reference): Can't point to other objects

Method:

1. Prohibit Overriding, while this method will not be used by late binding

2. Efficiency (not available

Class:

Prevent it from being inherited. Final Class's fields can be Final or not,

But Method is implied in Final (because Class cannot be inherited, Method cannot be replied)

ArrayList replaces vectorhashmap replaces HashTable

Override: Derived Class redefines a base class with the same name and the same nonvarius.

Overload: Whether in Derived Class or on Base Class, the same name Method is different from the list of parameter, will not be overridden (Override)

Note: Only things in the base interface (Private Method are not interface) can be overriddled (Overriding) If the method is private, it does not belong to the interface of the base class. It can only be hidden by class, just with the same name. If you created the same name of public or protected, or package permissions in the derived class, then there is no contact with the same name in the base class. You didn't override that method, you just created a new way.

Do not fall into the trap of premature optimization code. If you have a normal job but run speed

A very slow system, it is difficult to solve the problem with Final.

But we can pass Profiling this is a tool that can help you improve the speed of the program.

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

New Post(0)