Read tij-7 Polymorphism

Source: Internet
Author: User

Think in Java chapter 7th

[Three basic features of object-oriented programming languages: data abstraction, inheritance, and polymorphism]

There is no "idea" at this level! When you think about the "construction and organization" program in a way that people are familiar with and used to think, you may feel natural-you have an object-oriented idea; or, some people x give you a familiar, habitual (human rather than machine) way of thinking.Introduction and ExplanationIn data abstraction, inheritance, and polymorphism, you feel that you are close to the thinking habits of life and people, you think that the concepts they are talking about are natural (of course they are easy to understand ), this is X's explanation of object-oriented under the guidance of "thoughts. The object-oriented thinking and object-oriented paradigm must begin with Thomas? It is usually obtained from the paradigm theory, various object-oriented design principles, and software engineering principles.

In this chapter, what I want to talk about is [p148Abstract classes and Java interfaces play an important role in programming. Use (dependency) abstract types as much as possible in code, instead of specific classes..], However,Interface Programming"Expand, and the term "polymorphism" is used as the basis for guerrilla warfare, which makes me feel a bit itchy.

Therefore, the first section polymorphism separates the interface from the specific implementation details from another perspective, that is to say, the separation between "what" and "how to do" is realized. With the concept of polymorphism, code organization and readability can be improved. In addition, you can create programs that are easy to expand. You can easily "grow" either during project creation or when new features need to be added ".]

You 'd better putReplace it with an abstract type (an abstract class in Java and a Java interface). If it is not theoretically demanding, the parent type (a larger applicable surface type) can also be used., That is:

Abstract type (parent type) separates interfaces from implementation details from another perspective, that is, the separation of the two modules "what" and "how to do" is realized. Abstract types can be used to improve code organization and readability. In addition, you can create programs that are easy to expand. Both during project creation and when new features need to be added, they can easily "grow".

Polymorphism, handle (a XX). For a client program, A is a placeholder for its various subclasses. Parent class-it is usually an abstract type, and its sub-classes are defined as the same concept. In the program, all its sub-classes are viewed with a noun, thus shielding the specific type of sub-classes. The role of parent and abstract types is embodied by polymorphism.

7.1 secondary shape

Assign a subclass reference to the referenced variable of the parent class, which is calledUp shape (upcasting). For example

Sup S = new sub (); // sup is the parent class

Dosomething (SUP s) Call Statement dosomething (new sub ())

7.1.1 why do we need to perform a secondary styling?I want to say: "During programming, dosomething (SUP S) and so on are all written for parent SUP (assuming sup is an animal, then your sub is handled by cats, dogs, and horses in a unified manner. To unify the processing, we need to shape up ." I don't like the writing of the author. At least when I read this section, I followed his text [this program may seem strange. Why Should everyone intentionally forget the type of an object ?......] For a long time, Sherlock Holmes was used to describe an extremely natural thing. In addition, he gave an example that seemed very powerful and boring-or a negative example. Finally, he said, [but ifWrite only one method and use the base class as the independent variable or parameter.Isn't it much easier to use, instead of using specific response classes? That is to say, if we are able to deal with the basic class with our code, the workload will be hard to estimate .].

7.2 in-depth understanding 7.2.1 dynamic binding 
7.2.2 generate correct behaviorsReference: What is polymorphism (polymorphism)
7.2.3 scalabilityIn the above case, during programming, dosomething (sup s) and so on are all written for the parent SUP (assuming sup is an animal), so your sub has a new animal, such as goat, chicken ...... [Add any number of new types to the system as needed ...... Such a program has "scalability]

[Suppose we add more methods to the base class and a series of new classes, what will happen ?] Class Diagrams, codes, and [we can see that the tune () method is not affected when all other code around the tune () method changes, I am still working normally. This is exactly what we hope to achieve by taking advantage of polymorphism. After we modify the code, it will not affect the part of the program that should not be affected. In addition, we think that polymorphism is a crucial technology that allows programmers to "distinguish what is changed from what is not changed ".] Me Completely incomprehensibleWhat does this paragraph mean.
public static void doSth(Sup s) {// ...s.m();}
Under the conditions of the above Code, dosomething (SUP s) processes various sup subclasses in a unified manner, while dosomething (SUP s) only involves the sup interface M (). Suppose sup has no subclassIt has interfaces M1 () and M2 (). Have you deleted M1 () and M2 () that have an impact on dosomething (SUP s? Does the interfaces M8 () and M9 () that you added sup affect dosomething (SUP s? There is no influence on polymorphism. This "unaffected" has a dime relationship with polymorphism.? Is it necessary to tell us the truth that it will not NoThe affected part is affected.
7.3 coverage and overload

This example...

7.4 abstract classes and Methods 7.5 Interfaces

In 3rd, an interface can also contain fields, but these are implicitly static and final.Basic Data TypeBut both are static and final by default.

I don't like "think of it as a" pure "abstract class". The constructor is a special method.

7.5.1 "Multi-inheritance" of Java" 

7.5.2 inherit the Extended InterfaceSub-interface

7.5.3 constant group"An interface is a good tool for grouping constant values", which is not a good idea.

7.5.4 fields in the initialization InterfaceUse a random number for initialization.

7.6 The internal class I introduced [9.3 event-driven programming], especially the callback class, before introducing the 9.4 nested type. Anonymous classes are the basic method for implementing callback in Java, and are also the main reason for introducing nested types in Java. Now there is a Lambda expression. Skip this section and think about how to deal with this part.


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.