1. Packaging
So that the attribute will not be accessed directly but needs to be accessed or modified, the + get and set methods are modified with private
-
One of the three characteristics of Java object-oriented: encapsulation (important)
-
Three characteristics of object-oriented: encapsulation, inheritance, polymorphism and abstraction
-
Packaging from life:
-
Computer: memory module, hard disk, CPU, radiator, etc
-
House decoration: water pipe
-
On the main road: route
-
Female: age, weight
-
Encapsulation in Java:
-
We need to consider the security of the data. The data cannot be easily accessed by the outside world directly
-
But let the outside world (other classes) access (provide an open access to the outside world) in the safe way we specify
-
Ensure the rationality and standardization of data
-
Ensure code independence. * private is private and can only be accessed in this class. Other classes cannot be accessed directly
-
Public is public and can be accessed anywhere
-
Steps for encapsulation:
-
1. Add the access modifiers private, public, default and protected in front of the attribute
-
2. Provide public access methods to the outside world (getter get value / setter set value)
-
3. When setting external values, some logic verification codes will be added to ensure the rationality of the data **/
2. Extensions
-
Concept of inheritance:
-
Steps to inherit:
-
Characteristics of inheritance:
-
Advantages and disadvantages of inheritance:
-
Inherited Keywords:
-
Inheritance concept:
-
Inheritance from life
-
Father's property (money, house, car)
-
A son can inherit his father's property
-
Inheritance from Java:
-
Parent class -- property, method
-
Subclass -- property, method
-
Subclasses can inherit the properties and methods of the parent class*
-
Characteristics of inheritance:
-
1. Inherited keyword extends
-
2. Which attributes and methods in the parent class cannot be inherited '
-
(1) private attributes and methods of the parent class
-
2. Construction method of parent class
-
3. The default modified attributes and methods under different packages cannot be inherited
-
)
-
3. Inheritance can only be single-layer inheritance (single root), not multi-layer inheritance
-
For example, subclass A extends parent class B
-
Other subclasses D extensions subclass A
-
A subclass can only have one direct parent, such as the subclass extends parent
-
4. Using multi-layer inheritance, the smallest subclass can have the properties and methods of the parent class, or the properties of the parent class of the parent class
-
Inherited properties and methods*
-
Use steps of inheritance:
-
0. Object oriented needs to meet the relationship of is a, the parent class Animal, and the child class Dog
-
1. Find common attributes and methods, extract them and put them in a class, which is called parent class / superclass / base class
-
The inherited class is called subclass / derived class
-
2. Use the keyword extends to inherit the parent class. Syntax: subclass extends parent class
-
3. Create a subclass and call the properties and methods of the parent class*
-
Advantages and disadvantages of inheritance:
-
Benefit: provides reusability
-
The code is saved and the code is optimized
-
Disadvantages: encapsulation benefits (providing independence), inheritance is to make the relationship between each class closer
3. Authority control
The property modified by private cannot be overridden by subclasses, and so can methods. Because the property decorated with private cannot be directly accessed by subclasses (. field)---- This model is analogous to direct addressing and indirect addressing
Override, and override. The modification before the property or method name indicates that the permission range is greater than or equal to that of the parent class.
Class does not inherit the constructor of the parent class, but will automatically call the constructor of the parent class by default.
For example, in subclass construction, super() can only be written in the first line or not, because java must ensure that the construction of the parent class is completed before building the subclass.
Only public or no writing can modify a class
Code is logical. We should pay attention to logical order.
package w2day8.demo1; /** * @Title * @Author eastlin * @Description: */ public class Pets { private String name; //private String sex;// Cannot be overridden String sex;//1.Q 2 Sister Q private String type;//int type;//1. Dog 2 penguin private double health; private double adhere; public Pets(String sex) { this.sex = sex; } public Pets() { } public String getName() { return name; }//get,return public void setName(String name) { this.name = name; } public String getSex() { return sex; } public void setSex(int num) {//Assign void if(num==1){ this.sex= "Q Son"; } else if(num==2){ this.sex="Q Sister"; }else { System.out.println("Your choice is wrong!"); this.sex=null; } } public String getType() { return type; } public void setType(int num) { if(num==1){ this.type="Dog"; } else if(num==2){ this.type= "penguin"; }else { System.out.println("Your choice is wrong!"); this.type=null; } } public double getHealth() { return health; } public void setHealth(double health) { if(health<0||health>100){ System.out.println("The health value should be between 0 and 100, and the default value is 60"); health=60; } this.health = health; } public double getAdhere() { return adhere; } public void setAdhere(double adhere) { if(adhere<0||adhere>100){ System.out.println("Intimacy should be between 0 and 100, and the default value is 60"); adhere=60; } this.adhere = adhere; } public void selfSay(){ System.out.println("Pet confessions:"); System.out.println("My name is"+this.name+",Health value is"+this.health+",What is the value of intimacy with the host"+this.adhere+",My gender is"+this.sex); } } package w2day8.demo1; /** * @Title * @Author eastlin * @Description: */ public class Penguins extends Pets { String sex ; //Using super in a subclass to call a parent class constructor public Penguins() { super( "Q Son");//Call the parameterized constructor of the parent class } } package w2day8.demo1; /** * @Title * @Author eastlin * @Description: */ public class Dogs extends Pets{ private String strain="Shepherd Dog"; public String getStrain() { return strain; } public Dogs() { } } package w2day8.demo1; import java.util.Scanner; /** * @Title * @Author eastlin * @Description: */ public class Penguin { // Penguins using encapsulation to realize electronic pet system correctly input health value and intimacy // Ensure the validity of health value (0-100), otherwise the default value is 60 // Ensure the effectiveness of intimacy (0-100), otherwise the default value is 60 public static void main(String[] args) { System.out.println("Welcome to the pet shop!"); // Pets a1=new Pets(); // Scanner input = new Scanner(System.in); // String str=""; // int n=0; // System.out.print("please enter the name of the pet to be adopted:"); // str=input.next(); // a1.setName(str); // System.out.print("please select the type of pet to adopt: (1. Dog, 2. Penguin)"); // n=input.nextInt(); // a1.setType(n); // System.out.print("please select" + a1.getType() + "gender: (1.Q child, 2.Q sister)"); // n=input.nextInt(); // a1.setSex(n); // System.out.print("please enter the health value of" + a1.getType() + "(between 0-100):"); // n=input.nextInt(); // a1.setHealth(n); // a1.selfSay(); Penguins a2=new Penguins(); a2.selfSay(); a2.setAdhere(2); Pets a3=new Penguins(); a3.selfSay(); a3.setAdhere(2); } } package w2day8.demo1; /** * @Title * @Author eastlin * @Description: */ public class Dog { public static void main(String[] args) { Dogs d1=new Dogs(); d1.getStrain(); d1.selfSay(); } }
Establish habits
Class is a complex number of templates
Objects (test classes) are concrete, using singular numbers
4. Package dependency
import jar package 𞓜 classes of some packages
-
package
-
A package is actually a folder
-
Help us manage Java files by category
-
You can also solve Java classes with the same name*
-
Package features:
-
1. Syntax of declaring package: package package name (just like declaring an address) Package name 2 Package name; Must be in the first line of the java class.
-
2. Import package syntax: import package name 1 Package name 2 Class name;
-
3. If you need to use a class under a package, you need to import the package name 1 Package name 2 Class name;
-
4. If you import different packages of the same class, you need to display the guided package
-
import package name 1 Package name 2 Class name;
-
import java.util.Date;
-
Date date = new Date();
-
java.sql.Date d = new java.sql.Date(1000L);
-
5. The package naming standard is lowercase and separated by small dots
-
Reverse writing of the company's domain name, such as www.baidu.com com.baidu.www. module name Group name
-
6. Classes under the system package provided by java do not need to be imported Lang does not need to import packages, Math classes, strings, etc
-
7. When you need to use classes under different packages, you must guide the package,
-
Using classes under the same package does not need to import packages
-
8. The order of a java class, package > Import > class
-
9. Common packages
-
java.lang system package Math
-
java.util toolkit Arrays
-
java.awt form package
-
java. Package for IO file read / write operations
-
java. Reflit reflection package
-
10. Classes without packages cannot be used (entries cannot be found without addresses)
-
11. * represents all classes. If you want to import all classes under a package
-
Just use Java util.*; *
5.super
-
super keyword: (similar to this learning)
-
Access the keyword of the parent class. super can only be used in the child class
-
Access the member properties of the parent class: super Parent property name
-
Access the member method of the parent class: super Method name
-
Parameterless constructor for accessing parent class: super();
-
Parameter construction method for accessing parent class: Super (value 1, value 2,...)*
-
Features of super keyword:
-
1. super can only be used in subclasses
-
2. When accessing a constructor, it can only appear in the first statement of the constructor
-
3. First access the construction method of the parent class, and then access the construction method of the child class
-
4. Before accessing the parameterless / parameterless construction methods of the subclass, the parameterless construction methods of the parent class will be accessed first, and then the parameterless construction methods of the subclass will be accessed
-
5. If you want to call the parent method with its own parameters, you need to call it first
-
Parent class before child class
6.this
-
Features of this keyword:
-
1.this is to solve the problem of representing me in the case of the same name
-
2. Only used in this category
-
3.this can be omitted
-
4.this can only be placed on the first line of the constructor*
-
Three uses of this:
-
-
Access the attribute of this class: this Attribute name;
-
-
-
Access the method of this class: this Method name ();
-
-
-
Access parameterless construction method: this();
-
-
Access parameterized construction method: this (value 1, value 2, value...);