201711671224 "Java Programming" Chapter 6 learning summary

Learning content summary

Object-oriented, the subclass inherits the parent class, to avoid duplication of conduct defined, but not in order to avoid duplication of conduct on the use of the definition of inheritance.
Program code is repeated in the programming signal is bad, the program code appears repeated among a plurality of classes, the improved process design consideration is the same lift as a parent class program code.
In java, when using inheritance extends keyword, private members would be inherited, the subclass method must be accessed through the parent class.
java, the subclass can only inherit a father, has inherited an important relationship between child and parent class is-a relationship there is.
Check the multi-state logic syntax is correct, the method is read from left = the right: the right instead of the left type is the type of a subclass? If not on the compilation fails.
UML
Polymorphism • Inheritance can reuse the code, greater use is to achieve "multi-state"
package is the basis for inheritance, polymorphism is the basis of inheritance
, "a statement quoted by the parent class objects, generating object subclasses" there is more than state
if a block does not have any method of operating the program code, the method may be used to mark abstract abstract method, this method is not written {} block a direct ";" end like.
java contains abstract methods specified in the class must be marked abstract before the class, he said it was an incomplete abstract class definition.
Inheritance is also consistent with (Do not Repeat Yourself) principle DRY
the ISA and the principle of OCP, LSP principle
assignment: "=" left to the right ISA, otherwise they will be cast Role role1 = new new Swordsman • ()
Swordsman Swordsman = role1; // the X-
Swordsman swordsman = (SwordsMan) role1; // ok

Note ISA refers to the functionally

extends • Java only single inheritance, that is, only one parent class

Abstract methods, abstract classes abstract •
the Java class that has abstract methods must be abstract, but the method is not necessarily an abstract class abstract methods
abstract class can not be used to generate new objects

Inheritance syntax details
• public / Package Penalty for / protected / Private
ppp
• Super and the this
• • java.lang.Object toString
• the equals
• hashCode
• instanceof

Java has public, protected and private authority three keywords, but in fact there are four of competence.
Want to get the definition of the parent class method, you can call the method before, plus the super keyword.
If you want to execute a parent class constructor you can use super () specified. this () and super () can only select a call, and must be executed first in the constructor.
If you used the final keyword before the class definition, it indicates that the class is the last one.
If you do not use the extends keyword specified when defining classes inherit any class, it must be inherited java.lang.Object. In java, dating back to the top of any class of the parent class, necessarily java.lang.Object.

Other (perception, thinking, etc.)

no

Reference material

  • "Java Programming"

Guess you like

Origin blog.csdn.net/nemeziz/article/details/84544146