1. A class is an abstraction of an object, and the class also encapsulates the object. The so-called encapsulation is that the designer of the class simply provides the user with the parts that the class object can access, while the other hidden member variable methods in the class are inaccessible to the users.
Implementation: A: In the definition of the class, set the access to the member variables and methods in the object;
B: Provide a uniform method of reference for other classes;
C: Other objects cannot directly modify the properties and methods owned by the text object.
2. Access rights:
The member variables and methods of a:private can only be accessed by the methods of the class itself;
B: The default member variables and methods can only be accessed by all other classes within the same package, not outside the package;
The members of the c:protected can be accessed by the class itself, its subclasses (which are available in different packages);
D:public is completely open. Only one class can be modified to public in a program, and if there is no public class in the program, and the file name is a class name in the program, the class is considered public
Note: You cannot use protected and private ornaments!!!!
3. Inheritance of the class:
Implementation of Class 3.1 inheritance:
The object class in A:java is the ancestor of all classes in Java! (P209)
Son a extends dad------can only have one dad!
3.2 This and super:
This:this. data member this. Instance method this (parameter)--references other construction methods in this class.
Super: Represents a reference to the parent object of the current object
Super. Data member Super. Instance method Super (parameter)--refers to the construction method of the parent class. --the constructor method of the parent class is called first when the constructor of the subclass is implemented! and the super () that is used to call the parent constructor method must be placed in the first sentence of the subclass construction method
3.3 Initialization Order:
The initialization order of a object: first static member, non-static member, and then constructor method,
B the order in which the constructor is called in the inheritance: The Father's class, then the sub-class.
3.4 Type conversions between objects:
Object Conversion Bible: Can only say that the cat is a mammal, can not say that mammals are cats!!! When said cat is a mammal, the cat will lose the unique properties and behavior of cats!!! (New member variables and methods as a cat)
Mammal aa=new cat ()---cat is a mammal (to be tested by IS-A)
4. Awaited began to come out------polymorphic!
The so-called polymorphism is the same method to obtain different behavioral characteristics!
Divided into:
A compile-time polymorphism---overloads (different number of parameters or different types of parameters)
B Dynamic polymorphism----Overwrite (the rewritten method cannot have more restrictive access than the overridden method-only the more open & rewritten method cannot produce more exceptions than the overridden method)
******************************************************************
Put on some little music!
First song final:
1. Final type variable name = initial value---constant
2. The final method---that the method cannot be overwritten at any time, but can be overloaded.
3. The final class----The class cannot be inherited and overwrite its contents. (Like system class, String Class)
Second song native: local reference specifier ^@^
Java allows you to directly reference programs written in a non-Java language on the local machine in your program.
Native type programname ([parameter list]);
ProgramName is the name of the program to be referenced, appearing in the form of a method. Because it is a reference, the specific actions are included in the source program, all programname after no concrete method body!
******************************************************************
One. Deep Inheritance:
1 "Design Inheritance tree:
(1) Identify objects with common properties and behaviors (wolves and dogs and sheep ^ ^---Common properties of animals)
(2) The design represents the common state and behavior of the class (All belong to animals!) ---parent class)
(3) Deciding whether a subclass needs to make a particular behavior behave differently (all eating, but sheep only eat grass---method of coverage)
(4) Find more opportunities for abstraction by finding subcategories that use common behaviors (wolves and dogs belong to the canine family!!!) --Re-inheritance)
(5) Check whether the design is reasonable (IS-A)
(6) You are done!
2 The meaning of inheritance:
(1) Avoid duplicated code (subclasses will always respond to changes in the parent class)
(2) Define a common protocol (service to polymorphic ^ ^)
? When you set the parent of a set of classes, you can use any class of the subtype to fill any position you want or expect the parent to have?
Two. In-depth polymorphism:
(1) Class vet{
public void Giveshot (Animal a) {
A.makenoise ();
}
}
Class petowner{
public void Start () {
Vet v=new Vet ();
Dog D=new Dog ();
Hippo h=new Hippo ();
V.giveshot (d);
V.giveshot (h);
}
}
In this case, if the animal type is used to declare its arguments (the return type, the type of the array), the program code can handle all animal subclasses. This means that other people can use your vet! if they notice that they have to expand animal. (P168)
If this example is not polymorphic, then every animal has to write different ways-how sad it is!!
(2) Interface:
Polymorphism means "a lot of forms", you can treat your son as a father or a grandfather!!
You can define common protocols between related classes through inheritance (the method is the sign of the protocol!). ), which is equivalent to declaring in other program code: "All my subclasses can use these methods to perform these tasks!" ”
In order to avoid "deadly block" so Java does not allow multiple inheritance, interface is our Savior! The Java interface is a purely abstract class of 100%. and using interfaces can inherit more than one source, while other classes can implement the same interface. So you can combine different levels of inheritance for different requirements!
Three. How can you tell if you should design a class, subclass, abstract class, or interface?
(1) If the new class cannot pass IS-A tests on other classes, it is designed not to inherit classes from other classes.
(2) to overwrite or add a new method to inherit an existing class only if a specialized version of the class is required.
(3) When you need to define templates for a group of subclasses and do not want others to initialize the template, design abstract classes for them.
(4) If you want to define only the role that the class can play, use the interface.
How to understand encapsulation, inheritance, polymorphism! The difference between the three?