1, Encapsulation
1.1 what is encapsulation
Encapsulation, i.e. hiding**object**of**Properties and implementation details**,Public only**Interface**(method/Function), which is controlled in the program**Read and modify properties**of**Access level**;Combine the abstract data and behavior (or function) to form an organic whole, that is, combine the data with the operation data[source code](https://baike.baidu.com/item / source code / 3814213) to form a "class", in which attribute data and functions are members of the class. The purpose of encapsulation is to enhance security and simplify programming,**User**You don't have to know the specific implementation details, but just through the external[Interface](https://baike.baidu.com/item / interface) to use members of the class with specific access rights.
**Broadly speaking: * * all public code extraction can be called encapsulation.
It is generally embodied in the encapsulation of public methods. Tool class.
In a narrow sense: hide the properties and implementation details of the object.
-
Privatize the attribute and decorate it with the private keyword.
-
Add get and set methods for properties decorated with the private keyword. The properties are manipulated through the get and set methods.
setAge() set / getAge() get
public class KongTiao { //Private. If an attribute is defined as private, it cannot be used externally, but only by classes //Member variables are privatized private int wendu; //get method value public int getWendu() { return wendu; } //set method assignment public void setWendu(int wendu) { if(wendu>31)wendu=31; if (wendu<60)wendu=60; this.wendu = wendu; } }
public static void main(String[] args) { //Encapsulation inheritance KongTiao kongTiao=new KongTiao();//Call a private method kongTiao.setWendu(-100);//Assignment directly through set System.out.println(kongTiao.getWendu()); }
Privatize the temperature through the private keyword, set the temperature through the set method, and set the range in the set method to 31 ~ 60 It can not only hide the properties of the object, but also hide the implementation details.
1.2 function of packaging
Improve the reusability and maintainability of urban and rural areas and protect private data
2, Inherit
2.1. What is inheritance
Inheritance is object-oriented A concept in software technology, and polymorphic,encapsulation Altogether object-oriented Three basic characteristics of. Inheritance can make a subclass have a parent class attribute and method Or redefine and append properties and methods.
inheritance. This technology makes it easy to reuse the previous code, which can greatly shorten the development cycle and reduce the development cost.
2.2 role of inheritance
Improve code reusability and expand functions
Reduce code redundancy
Write a parent class, and the child class can extend the parent class
Subclasses can redefine the parent class
2.3. Inherited method format
The java language is single inheritance
[Permission modifier] class Subclass name extends Parent class name{}
Inherit keyword extends
public protected the default properties and methods of the same package private cannot inherit
The property of private can be valued and assigned through set and get
2.4 rewriting of methods
stay Java And some other advanced object-oriented In the programming language, subclasses can inherit Methods in the parent class without rewriting the same methods. But sometimes subclasses don't want to be intact inherit The method of the parent class, but wants to make some modifications, which is need Rewriting of the method used. Method rewriting is also called Method coverage.
Overload: occurs in the same class, with the same method name and different parameter lists, regardless of the return value.
Override: occurs in parent and child classes. The child classes inherit the methods of the parent class. The method names and parameters are the same, but the implementation method bodies are different.
be careful
- You must ensure that the methods overridden in parent and child classes have the same method name and parameter list.
- The return value of a subclass method must be equal to the return value range of a parent method&
- The permission of the subclass method must be greater than or equal to the permission of the parent method (excluding private).
- Private cannot be inherited
- Constructor cannot inherit directly, but the constructor of the parent class must be called in the constructor of the child class. If it is not written, the parameterless constructor will be called
- When calling, it must be the code block of the first sentence of the code body
toString() method
The Object class is the base class for all classes
Any class is a direct or indirect subclass of the Object class. There is a toString() method in the Object class. When outputting an Object,
First call the toString() method of the current class. If the current class does not have toString(), find the parent class of the current class to see if there is a toString() method. If there is no parent class, find the toString() method in the Object class and run it.
public class Person { //Parent class private String name; private int age; protected String sex;//The protected variable can be called in a package private String address; public Person(){ System.out.println("non-parameter constructor "); } public Person (String name,int age){ System.out.println("judging from this"); this.name=name; this.age=age; } public String getName() { return name; } }
public class Kong extends Person{ //Subclass //this -- call the current object's, if not, the parent class will be called //The super parent class directly calls the parent class String sushe; public void sd(){//Method coverage System.out.println("sadfsadf"); } void ssd(){ super.sd(); } public Kong(){ System.out.println("kong"); //super();// Call the parent class parameterless constructor } public Kong (String name,int age){ super(name,age); this.sushe=sushe; } }
public static void main(String[] args) { Kong stu =new Kong("zs",45);//Call with parameters System.out.println(stu.getName()); //Person stu1=new Person(); Kong stu2=new Kong();//Call parameterless constructor }
2.5. super keyword and this keyword
Function: if a subclass wants to use both its own methods and the member variables and member methods of the parent class, it needs to use the super keyword.
super:
The member methods in the subclass directly access the member variables and member methods of the parent class
Directly access the constructor of the parent class in the constructor of the child class
this: this represents the current object; The current one
2.6 construction method in inheritance
characteristic:
Constructor cannot be inherited (unique to a class)
The created subclass object must call the parent class construction first, and then call the subclass construction
The subclass calls the parameterized construction of the parent class through the super keyword, with no parameter super() and parameter super (parameter list).
The super parent class construction call must be the first line statement in the child class construction. (and a subclass is constructed, and super () can only be called once). There must be a father before a son.
public Kong(){ //System.out.println("kong"); super();//The first line is required to call the parent class parameterless constructor }
Characteristics of inheritance: classes in java inherit from one class to another. But you can inherit at multiple levels. A parent class can have multiple subclasses.
A subclass can only have one parent, but a parent can have multiple subclasses
2.7 Object class
Object is the parent class of all classes. Any class inherits object by default
In theory, the object class is the parent class of all classes, that is, it inherits Java directly or indirectly Lang.Object class. Since all classes inherit from the object class, the extends Object keyword is omitted.
The equals() method compares the values of two strings when comparing strings. But in fact, the equals method compares address values.
Source code of equals() method in Object class:
Why can String compare values with equals()?
Because the equals() method is overridden in the String class, values can be compared.
3, final keyword
3.1 final function
4, Basic type packing class
4.1 concept
4.2 automatic packing and unpacking
4.3 conversion of basic data types and strings
summary
Inheritance:
Keyword extends
A single inheritance class can only have one parent class
Subclasses can inherit the parent class: public protected is the same as the default properties and methods in the package, and cannot inherit private methods
Subclasses can write their own properties and methods
Subclasses can override the methods of the parent class
In a subclass, you can call the methods and properties of the parent class through super. super. Method name / property name
A parent class construct cannot inherit, but must be called
new subclass, the first line of subclass construction must call the construction method of the parent class
If the subclass does not write super(), the system calls the parameterless construction of the parent class by default
. The parameter can be called through super (parameter), and must be the first line, and there can only be one super().
If you call a method through a subclass, call your own method first. The subclass rewrite method calls its own method, and if the subclass does not rewrite the method, the parent class method is used.
All classes directly or indirectly inherit the object class. Object is the base class and superclass of all classes.