A preliminary understanding of java--the eighth chapter-inheritance-the characteristics of member variables in the child-parent class-Applications covered

Source: Internet
Author: User

One.

Covering several sections, it seems to be a very important point of knowledge indeed.

In fact, we are not concerned about the characteristics of coverage and coverage of the attention, these small things as long as a little attention, not to write wrong on it. We have to pay attention to what, when to use the overwrite operation, which is the most frequently used when we develop.

First, no matter what is covered, first use a case to demonstrate it again.

First describe a small thing, mobile phone. Mobile phone has a function is the caller ID, early mobile phone, display only Num. After compiling, one runs and discovers that there are no problems.

Next, you encounter some problems. Now the phone comes out many new versions, the caller ID feature has undergone some changes, there are display names and photos. We're going to add these things in,

Directly on the basis of the original, plus some new content. The compilation runs without problems.

But there are a few small problems, whether it's name, or picture is later, write them together, right? is not appropriate because the added name and pic are equivalent to the upgrade, which is also part of later maintenance. If each upgrade, is to modify the source code, if found that the upgrade is not correct, you have to go back, which will cause the cost of modification. This is not conducive to the extension of the program. And, an upgrade to modify the code, often modify is not a place, is more than one place, will give you an upgrade to bring disaster, or even devastating, and so you change, not necessarily can change back. Therefore, in general, we do not recommend modification of the source code, so that the most dangerous. Therefore, in the design time becomes very important, if the design is not good, the back all appears not to be possible.

So, we can do this, can make a new class, Newphone, it is also a phone, let it inherit the previous phone can be, create a new display method, to show the post-add things.

( Why is there a repetition of number in this case, is it a bit superfluous ?) )

The same is the change, but we are not going to modify the original class, directly through a class to inherit it. be extended in an inherited manner. It would be cumbersome to design a class individually, with none of the previous relationships, because there are other features in the phone class that don't need to be modified, such as calls, which have always been the same from the previous to the present. That is, the call () method in the phone class, do you think the Newphone class needs to be redefined? There is no need, and we know new things, is one of the old class, why not directly inherit call (), to use it? Just the show () method has changed. Therefore, the extension of the program can be improved by using inheritance.

DOS results show that there are no problems. It seems to solve the problem, but there is still some discomfort. The Newphone inherits the phone, which is equivalent to inheriting two of the methods in the parent class, meaning it can also call show (), and the result is yes,

But is it cool? Uncomfortable. After the subclass appears, it is nothing more than to improve the caller ID feature in the parent class, but does it need to change the caller ID statement (this is the name of the show)? The parent class itself has the function of caller ID, as long as the subclass inherits it, but what? Subclasses want to say that this feature, I can keep, but I want to change the content of the function (keep the Show method declaration, but the contents of the execution will change). How does this work? There is no need to define a new method, after the new method is defined, the old method can be used, and what I want to do is to improve the existing call display function.

The simplest action that you want to improve is to overwrite, preserve the original functionality of the parent class, and create a subclass-specific content. There have been caller ID, I do not like, but the function has to be, because I also need to call display, but my caller ID is not the same as the content of the display.

As long as you create a subclass object, you can use Show (), or call (), p is the sub-class caller ID, and the phone is not the same, but also can go to the phone calls ().

Here's another thing to note, The method of the child parent class Num wrote two times,

Why not omit the NUM statement directly? Because it is overwrite, if the NUM statement is omitted, then when the show () method of the subclass is called, there is no num output.

A preliminary understanding of java--the eighth chapter-inheritance-the characteristics of member variables in the child-parent class-Applications covered

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.