Disclaimer: This article is a blogger original article, follow the CC 4.0 BY-SA copyright agreement, reproduced, please attach the original source link and this statement.
this and super and final
The difference between super and this:
- this means that the current generation of objects created
- Other constructors call this class: The first line of this constructor (parameter list)
- Variables distinction of the same name (and local members) this.
- refers to super parent class object
- Call the parent class constructor
- Default empty constructor calls the parent class
- Other constructor calls the parent class to be displayed using the super (parameter list)
- To use the first line in the constructor
- this does not exist and super constructor call use
- Variables with the same name to distinguish (sub-classes and superclasses)
- If present, local, sub-class members, the parent class members with the same name issue, calling time
- Local calls, direct write
- Call the sub-class member, use this.
- Call the parent class members, using super.
- If the problem with the same name does not exist, corresponding to omitted this. Or super.
- Before you create a subclass object, first create the parent object -> Subclass after my late father (parent object subclass object memory space, can not directly operate external, internal use only by super in this subclass the parent class object)
- Initial order: first the static member
- final final
- 1. The final variable is a constant modified
- If the data type of the basic variable data types: stored data value can not be changed
- If the data type of the variable is a reference data type: address storage object can not be changed, but the property value of the target memory can be varied
- 2. the final modified method can not be overridden
- 3. the final modified class can not be inherited (eunuch class)
- Q: private property, and -> to provide getter, setter can use it ??? can access by setting and operation of the parent class of private property
Three characteristics of object-oriented
Object-oriented three characteristics:
- Package: hide implementation details inside and outside to provide a common access method
- Inheritance: Once the subclass inherits the parent class, you have the right to use the content of the parent class
- Polymorphism: a variety of forms a thing of | a variety of forms
- Polymorphic premise: class inheritance | implements the interface
- Polymorphic ultimate expression of: subclass object reference points to the parent class
- If the child has override methods in parent classes, polymorphic method call is overridden in a subclass
- Note: New in reference to the parent class is not visible to subclasses
- Use of polymorphic members call:
- 1) member variables
- Compile and run to see the parent class | Look left | see type
- 2) member methods
- Compile see the parent class, subclass run to see
- Compile look left, look to the right to run
- Compile look at the type of operation to find the object
Rewrite and overloading
- The difference between the rewrite and reload: rewriting and methods are overloaded
- Overload:
- The same class
- Multiple methods, the same method name
- Different parameter list | different method signature
- Rewrite:
- 1. Different classes
- 2. inheritance | achieved
- 3. The same method signature
- Why use rewrite:
- Demand: There is a subclass inherits from a parent class in function, but the subclass implementation of a function, the parent class is not the same, this method can be function of rewrite
- How to use the override method:
- In this subclass object using the function call, and if there is a method to rewrite, rewrite method is called, if the rewrite, it will call inherited from the parent class functions
- Check whether the override methods:
- 1) the presence of a method override left upward triangle
- 2) @Override add annotations on the method, mandatory testing, if not rewrite method on the error
- note:
- Proprietary content can not be inherited, rewrite
- The modified final methods can not be overridden
- static content can not be rewritten
-
如果子父类中存在方法签名相同的方法,父类中这个方法为static修饰的,子类中的同名方法必须也要被static修饰
tostring and equals method
1.toString () to show the contents of an object as a string
- Source:
public String toString() {
return getClass().getName() + "@" + Integer.toHexString(hashCode());
}
- In print an object when the reference, print the address of the object, the object is the default print calls toString method's return value
- Requirements: When you want to print an object reference data type a custom quote, print the value of all the member properties of the object, rather than address
2.equals method
- Source: *
public boolean equals(Object obj) { //Object obj=new Person();
return (this == obj);
}
The difference between equals and ==:
- == address value comparison reference data type
- equals () address () method or the comparison target equals Object class if used in
- If you have content to rewrite equals () method, you can achieve more objects
instanceof
Transformation:
a strongly typed language
data type transformation
java.lang.ClassCastException abnormal type conversion
- To avoid abnormal type conversion introduces an operator instanceof
- Instanceof type reference
- Determine whether the reference is to an object in front of the back type of | his sub-class object, if the return is true, not false returns
Abstract methods abstract class
Abstract classes: modified class is abstract
- Abstract methods: abstract being modified method
- There is no way the body can
- In the abstract class must
- note:
- 1. abstract class can not be instantiated | can not create object
- 2. To abstract the presence of an abstract class, an abstract method to be rewritten
- 3. The use of abstract classes:
-
具体子类: 重写所有的抽象方法+ 按需新增
-
抽象子类: 按需重写抽象方法 + 按需新增
- 4. A method abstract been rewritten once, it does not need to be rewritten again
- The abstract class there may be an ordinary method, there may be an abstract method
- 6.abstract not be used with private, final, static, native
- father:
- Specific parent: the parent object can be created, how the contents of the parent class definition
- Abstract parent class:
- You do not want to create the parent class object directly
- The parent class method body does not know how to achieve