-------Android Training, Java training, look forward to communicating with you! ----------
I. Object-oriented inheritance
is to extract the generality of multiple classes and describe them separately, and to establish the relationship between classes by means of inheritance.
Subclasses inherit all members from the parent class. Keyword extends. The Java language only supports single inheritance, because it can pose a security risk and cause code conflicts. However, multi-layer inheritance and multi-implementations are supported.
Inherited:
1, improved Code reusability
2, let the class and the class have a relationship. With this relationship, there is a polymorphic nature.
Note: Do not inherit from simplifying code in order to get the functionality of other classes.
You must have an owning relationship between the class and the class to inherit.
The use of the inheritance system, lookup of the parent class function, the creation of subclass object use function.
Override overrides for a function in a parent class in a subclass
When the subclass is identical to a function in the parent class, when the subclass object calls the function, the contents of the subclass function are run as if the parent class's function is overwritten.
When the subclass inherits the parent class, inheriting the function of the parent class into the subclass, but the subclass has the function, but the function content is inconsistent with the parent class, it is not necessary to define the new function, but to use the override special, preserve the function definition of the parent class, and rewrite the function content.
Characteristics
When the program is updated to extend the functionality, it is possible to modify the original function by inheriting the overridden method, generally do not modify the original program, because there are too many places involved. Too much work.
The Super keyword, which can be overridden after the method in the parent class is called by this method, is generally written on the first line.
Attention
Subclasses override the parent class, you must guarantee that the subclass permission is greater than or equal to the parent class permission, and static can only overwrite static.
About access rights
Public>default>private
When instantiating an object, the subclass first calls the parent class constructor and then calls the class constructor, where the default first row in all subclass constructors is implicitly super (), and when there is no null parameter constructor in the parent class, you must manually change the super statement to the corresponding parameter constructor. Of course, the other constructors in this class can be called by this () in the subclass, if the function calls the constructor of the parent class, it does not need to write the super () statement.
Why is it necessary to access the constructors in the parent class, because the data subclasses in the parent class can be obtained directly, so when the subclass object is established, it needs to first access the parent class to see how the parent class initializes the data.
In addition, when the Parent property member is decorated by private, the child class cannot be accessed by borrowing the parent class's constructor method.
Final keyword
1,final can modify classes, variables, functions
2. A class that is final modified cannot be inherited.
3, the final modified method can not be overridden to ensure that subclasses do not require the encapsulation of functionality.
4, the last modified variable can only be assigned one time, both to modify the member variables and to modify the local variables.
Block in describing things when some data is fixed, then in order to enhance the reading, the specific naming, when no longer need to change this variable with the final decoration as a constant, the constant writing specification all letters are uppercase, there are multiple words in the composition of each word with an underscore connection.
Two. Abstract class, keyword, abstract
The origin of abstract class is extracted from the generality of the function of the thing, the function name is the same but the function body is different, then it is defined as abstract method.
Characteristics of abstract Classes
Abstract methods must be defined in abstract classes, abstract classes can have non-abstract methods, you can also do not define abstract methods, do not allow the creation of objects.
An abstract class cannot create an object with new, because invoking an abstract method makes no sense
When a method in an abstract class is to be called, the class object invocation must be established after the subclass has replicated all the abstract methods.
A subclass is still an abstract class if it covers only a subset of the abstract methods.
Abstract classes are used for subclasses, although they cannot create objects but have constructors.
Because of the inheritance of the abstract class, the subclass must rewrite the abstract method of the parent class to be able to create the object, which makes the function module mandatory.
The difference between abstract class and general class
Is the definition of a more abstract function, and cannot be instantiated.
Template method mode;
When defining a feature, part of the feature is deterministic, but part of it is indeterminate, and the identified part is in the indeterminate part, then the indeterminate part is exposed. It is done by subclasses of the class.
Abstract class gettime{
Public final void GetTime () {//is final decorated here because it does not require subclasses to override it.
Long Start=system.currenttimemillis ();
RunCode ();
Long End=system.currenttimemillis ();
System.out.println ("MS:" + (End-start));
}
public abstract void RunCode ();//This is where the test code is extracted as a function abstraction, and then it is replicated by subclasses.
}
Class Subtime extends gettime{
public void RunCode () {
Test code;
}
}
Three. interface; keyword interface,implements
The member modifiers in the interface are fixed
Member constants: public static final
member function; public abstract
The appearance of the interface will be manifested by another form of multiple inheritance. That is, multiple implementations.
Interfaces: Similar to special abstract classes, it is not possible to create objects, it is necessary to implement abstract methods of the quilt class.
Interfaces can be implemented in many classes, and a class can implement multiple interfaces. The limitations of inheritance are addressed because there are no method principals that need to be rewritten only once for conflicting functions.
interfaces, interfaces can inherit and support multiple inheritance, and also because their methods do not have method principals.
Features of the interface
1, interface is the rule of external exposure
2, interface is the function extension of the program
3, the interface can be used to implement multiple
4, the class and interface is the implementation of the relationship, and the class can inherit a ray colleague implementation of multiple interfaces.
5, there can be an inheritance relationship between the interface and the interface.
Three. polymorphic
There are many forms of a certain kind of things, and the relationship between the entity object and the class and the parent class is related to the entity objects.
A reference to a parent class to a subclass object is a polymorphic code representation.
Polymorphism through inheritance greatly improves the extensibility of the code.
The precondition of polymorphism is that there must be inheritance or realization between classes and classes.
The disadvantage of polymorphism is that you can only access common members that exist in the parent class by using a reference to the parent class
Abstract class animal{
public abstract void Eat ();
}
Class Cat extends animal{
public void Eat () {
System.out.println ("Eat fish");
}
Three. Internal class access rules
A class is defined in another class, and the opposite class becomes an inner class. Can be modified by the member modifiers, such as private and static.
Access features
Inner classes can access members in external classes directly, including private members. The external class must establish an inner class object to access the members of the inner class. When you ask an inner class from an external class externally, you need to first establish its object in the form Outer.Inner in=new Outer (). New Inner ();
A member of an external class can be accessed directly inside a class because the inner class can directly hold a reference to an external class, in the form of an external class name. This member
In external other classes, accessing non-static members in static inner classes
New Outer.Inner (). function ();
Accessing static members in static inner classes in other classes outside
Outer.Inner.function ();
A static member is defined in the party inner class, which must be static
When a static method in an external class accesses an inner class, the inner class must also be static.
Definition of inner class
When describing things, there is something inside the thing, which is described by an inner class.
Because internal transactions are using external things.
The inner classes above are in the member position, and if written in a local location such as a method body, it cannot be decorated with a member modifier.
You can access members in an external class directly, but you cannot access the local variables, only the final decorated local variables.
An anonymous inner class is a shorthand format for an inner class.
Prerequisites for defining anonymous inner classes
The inner class must be an integrated class or implement an interface.
The format of the anonymous inner class is as follows; The new parent class or interface () {Defines the contents of a subclass}
In fact, an anonymous inner class is an anonymous subclass object. is a form of expression.
Class outer{
int x=3;
public void function () {
New Absdemo () {//anonymous inner class. Absdemo is the parent class.
void Show () {
System.out.println ();
}
}.show ();
}
}
The method defined in an anonymous inner class is best within two
An anonymous inner class is actually a method body that uses an anonymous class to replace an external class member or member method. Think of it as a whole to help you understand better.
public void Catchmouse () {
System.out.println ("Catch Mouse");
}
}
Class Dog extends Animal () {
public void Eat () {
System.out.println ("Eat Bones");
}
public void Kanjia () {
System.out.println ("housekeeping");
}
}
Class duotai{
public static void Main (String []args) {
Animal a =new Cat ();//type promotion, upward transformation.
Animal b=new Dog ();
Cat c= (CAT) a;//type strong conversion, down transformation. The premise is that you have experienced upward transformation, that is, this type. This sentence can also be written in function functions but to add a judgment statement to determine the specific type, the INSTANCEOF keyword is used to determine the owning type.
function (a);
function (b);
}
public void function (Animal a) {//The commonality behavior is extracted by polymorphism to improve the reusability of the code. You can also define a tool class to encapsulate it individually.
A.eat ();
}
}
If a new subclass of animal or multiple objects of the same class is present, the Eat method can be executed simply by calling the function method. The inheritance system of the parent-child class, the call of the tool class to the object class, and the final consumer of the main function. All of these are based on object-oriented encapsulation and build a program structure network through inheritance and polymorphism.
Characteristics of polymorphic Members
Notice the object reference of the calling method, if it is a reference to the parent class, see if the Frezent has the method, if not, it cannot compile, and it needs to judge the type to be a reference to the subclass. If so, refer to the following rules. Compilation is a reference, the runtime only non-static member function is to see the class that belongs to, member variable static members are looking at the reference.
In polymorphic non-static member functions in the compile time, see if the referenced variable belongs to a class that has a calling method, if it is compiled by, there is no compilation failure, see if the object belongs to the class in the runtime has a calling method.
Compile by parent class, run by child class.
The characteristics of member variables in polymorphism, both compiled and run, are referenced on the left reference class.
The characteristics of static member functions in Polymorphic, both compiled and run, are based on the reference type on the left.
Dark Horse Programmer _java Object-oriented (ii)