Transformation of Java objects practical tutorial series

 

reflect:
Reference to the parent class can point to an object subclass
interface reference can point to an object implementation class of
transition:
Upcast
parent by the child class type class type transformation, or by a class that implements the interface type is the type of transformation
will be successful transition up, is an implicit conversion
target upward after the transition, you will only have access to the parent class or interface members
to transformation of the
transition from the parent class type is a subclass of the type or types of transition from the interface implementation class type
downcast may fail, an explicit conversion
target the downward transition, the access to a child class or specific implementation class members
instanceof keyword
specific to transition down.
If the downward transition is not successful, what will happen?
There will be an exception ClassCastException
how to avoid this?
Before downcast, we first determine what type of object is not to transition
How to judge?
Keyword instanceof
Animal animal = new Dog();
if (animal instanceof Dog) {
[Java]  plain text view  Copy the code
?
1
// 说明animal的确是一个Dog
}
If a class overrides the one parent class. at this time:
If the object of this class to invoke this method, the final execution is to achieve sub-class.
If the object after the transition up to call this method, the implementation is still to achieve sub-class.
Because the object upward after the transition, in the final analysis or subclass object.
Abstract classes and abstract methods
Abstract: abstract
Abstract class:
Modified by the keyword abstract class is an abstract class
Abstract methods:
Use keywords abstract modified method is an abstract method
Features:
Abstract methods:
An abstract method abstract modified, only the statement, did not materialize.
public abstract void bark ();
abstract method, can only be written in an abstract class.
Abstract class:
The abstract modified abstract class, an abstract class does not instantiate the object.
An abstract class is to write non-static member, this time these non-static members can be inherited to the subclass.
Abstract class that may contain constructors.
Binding abstract classes and abstract methods:
Non-abstract subclass inherits an abstract parent class, we must also implement all the abstract methods in the parent class.
Precautions:
final keyword
An abstract class can be modified with a final right?
Can not! Because the final indicates that the class can not be inherited. But for an abstract class, if not inherited, then the abstract class does not make any sense.
Abstract methods can be modified with a final right?
Can not! Because the final modification of the method can not be overridden. But you can only write an abstract method in an abstract class. If an abstract method used to modify the final, then this method will not be rewritten non-abstract subclass of this subclass that there will be problems.
Practical scene abstract classes and abstract methods:
You can use abstract classes and abstract methods to achieve the development of some simple rules.
For example: all courier companies need to be able xxxxx. KFC, McDonald's,
If only abstract classes and abstract approach to the rules specified behavioral constraints:
Drawbacks: Because Java is a single inheritance language, if a class in order to follow a certain kind of specification, try to inherit an abstract class, then this class will not be able to inherit from any other class.
To solve this problem, what can I use interface behavioral constraints, regulation code.
Interface
keyword: interface
grammar:
And it is more like a class, but he is not like
[Access modifier] interface Interface name {
[Java]  plain text view  Copy the code
?
1
// 接口中的成员
}
Access modifiers: and classes, only public and default default permissions.
The interface is not a class, can not instantiate the object.
Interfaces, and classes being written same level of relationship.
Interface name is an identifier that follows the big hump nomenclature
defined interface members:
Properties: interface attributes, the default modifier is public static final
constructor: the interface can not write the constructor
method:
interface methods are abstract
methods interface access modifiers are public
interface is a class that needs to be to achieve.
Implement the interface keyword: implements
Let the class implements the interface:
public class Shunfeng extends Company implements Express {
}
A non-abstract class implements the interface after all the abstract methods need to implement the interface.
After a class inherits from a parent class, you can also go to implement the interface.
If there are parent classes and interfaces simultaneously, then the parent class inherit the former, an interface implemented in the
A class can implement multiple interfaces
If there is the same plurality of interfaces of a class method implementation, this method needs to implement only the implementation class once.
There is an interface between inheritance and succession between the interface is multiple inheritance.
public interface GrilFriend extends HouseKeeping, Takecare {
}

Guess you like

Origin www.cnblogs.com/zhuxiaopijingjing/p/12302550.html