java's object-oriented, privite, encapsulation, construction, this, super, inheritance, rewriting...

object-oriented

1, Concept

1, Class - object

  • Details:
  • 1. Create a human and describe the attributes and methods of this class
  • 2. Create human objects
  • 3. Operation object

Class:

  1. Class has only properties and methods
  2. Attributes are also called global variables. Attributes are divided into member variables and static variables
  3. Methods are divided into member methods and static methods

Member properties / variables

Syntax structure: data type variable name;

Where to write: inside the class, outside the method

Member variable vs local variable

Member variable: the variable outside the method in the class, and the system will assign a default value to act on the whole class

The default method does not work in a local variable

Member method

Syntax structure: access modifier return value type method name ([parameter]) {}

Writing location: in class

Member method vs static method

Member method: a method belonging to an object, which must be called with an object

Static method: a method belonging to a class, which is called with the class name

Create a class: Human

package com.dream.test01;

//human beings
public class Person {
	
	//Properties - global variables
	//Attribute classification: member variable and static variable
	//Member variable
	String name;
	char sex;
	int age;
	
	
	//method
	//Classification of methods: member methods, static methods and abstract methods
	//Member method
	public void eat(){
		System.out.println("having dinner");
	}
	
	public void sleep(){
		System.out.println("sleep");
	}
}



object

Syntax for creating objects: class name object name = new class name ();

new construction method; If it belongs to an object, it opens up space in heap memory

Class name object name: the object name belongs to reference and stores the address of the object in heap memory

Operation object:

  1. Set member properties

  2. Get member properties

  3. Call member method

package com.dream.test01;

//Test class: test the code we write
//Features: there is a main method
public class Test01 {
	
	public static void main(String[] args) {

		//Create p object
		Person p = new Person();
		
		//Set member variables
		p.name = "acb";
		p.sex = 'male';
		p.age = 21;
		
		//Get member variable
		System.out.println(p.name);
		System.out.println(p.sex);
		System.out.println(p.age);
		
		//Call member method
		p.eat();
		p.sleep();
	}
}

2, Construction method

Meaning: together with new, it is the function of creating objects

characteristic:

  1. Method with the same class name

  2. No items returned

be careful:

  1. When no constructor is written in the class, the system will add parameterless constructor (parameterless constructor) by default

  2. Construction methods can be overloaded

Benefits of parametric Construction: when creating an object, assign data to the object

public class Test01 {
	
	public static void main(String[] args) {
		
		//Create a p object and pass the variable value into the person object
		Person p = new Person("aaa", 'male', 22);
		p.eat();
		
	}

}

public class Person {//Create a Person class
	
		String name;
		char sex;
		int age;
        //When there is a parametric construction, you need to create a nonparametric construction method
		//Nonparametric structure
		public Person(){}
        //Parametric structure
		public Person(String name,char sex,int age){
			this.name = name;
			this.sex = sex;
			this.age = age;
		}
		
		public void eat() {
			System.out.println(this.name + "eat");
		}
		
		public void sleep() {
			System.out.println(this.name + "sleep");
		}

	}

3, private - privatization

Meaning: Privatization

effect:

  1. Modified attribute: it is a privatized attribute and cannot be used by the outside world

  2. Modification method: privatization method, which cannot be used by the outside world

Application scenario:

  • Properties that do not allow external calls are decorated with private
  • Methods that do not allow external calls use the private modifier

public class Test01 {

	public static void main(String[] args) {
		
		//Create a new object
		A a = new A();
		//Call member method
		a.method02();
	}
	
}

public class A {
		//Privatized member variable
		private String str;
		//Privatized member method
		private void method01(){
			this.str = "aaa";
			System.out.println(this.str);
		}
		
		public void method02(){
			this.method01();
		}
	}

4, Encapsulation

  • Knowledge points: Encapsulation

    • Steps for encapsulation:

    • 1. Privatization attribute

    • 2. Add get/set method

    • Benefits:

    • 1. Protect attribute security so that attributes cannot be called directly from the outside

    • 2. After adding the get/set method, you can perform additional functions in the get method (get()) and the set method (set())

    • Summary: process of writing classes

    • 1. Properties

    • 2. Privatization attribute

    • 3. Non parametric and parametric structures

    • 4.get/set method


public class Test01 {
	
	public static void main(String[] args) {
		
		
		User user = new User("lk123", "123456", "lk", 3500);
		
		//Do not directly manipulate attributes, because this behavior can only manipulate attribute values and cannot perform other functions
//		user.money = user.money-200;
//		System.out.println(user.money);
		
		//Withdrawal: 1 Deposit voucher 2 Modified amount
		user.setMoney(user.getMoney()-200);
		System.out.println(user.getMoney());
	}

}

public class User {
	
	private String username;
	private String password;
	private String name;
	private double money;
	
	public User() {
		}

		public User(String username, String password, String name, double money) {
			this.username = username;
			this.password = password;
			this.name = name;
			this.money = money;
		}
		
		//Get the money property of this object
		public double getMoney(){
			//Additional features
			System.out.println(LocalDateTime.now() + "Got it money Properties:" + money);
			return this.money;
		}

		//Set the money property of this object
		public void setMoney(double money){
			//Additional features
			System.out.println(LocalDateTime.now() + "Set money Properties:" + money);
			this.money = money;
		}
		
	}

5, This - this object

  • Knowledge point: this - this object

    Meaning: this in the method represents the object calling the method

    • effect:
    • 1.this. Member variable: call the member variable of this object
    • 2.this. Member method: use this object to call member methods
    • 3.this(): call the constructor of this class
    •  Note: the first sentence in one constructor calls another constructor
      

public class Test01 {
	
	public static void main(String[] args) {
		
		
		Person p = new Person();
		//Call the getName method to get the name
		System.out.println(p.getName());//lk
		
		p.eat();
	}
}

public class Person {

		private String name;
		private char sex;
		private int age;
		
		public Person() {
			//Call the constructor of this class
			this("lk", 'male', 22);
		}
		
		public Person(String name, char sex) {
			this.name = name;
			this.sex = sex;
		}

		public Person(String name, char sex, int age) {
			this.name = name;
			this.sex = sex;
			this.age = age;
		}
		
		public void eat(){
			//this.name: call the member variable of this object
			System.out.println(this.name + "eat");
			//Use this object to call member methods
			this.sleep();
		}
		
		public void sleep(){
			System.out.println(this.name + "sleep");
		}
		
		
	}

6, Static - static

effect:

  1. Modifier attribute

When a class is loaded into the method area, the JVM scans all the properties of the class
And load the static attributes into the static area. The static attributes belong to class attributes,
All objects of this class share this property
Static properties are not recycled until the end of the project

Note: static properties are called using the class name

  1. Modification method

It belongs to a class method and is called directly with the class name

Application scenario: tool class

package com.dream.test05;

public class Test01 {

	public static void main(String[] args) {
		
		
		A a1 = new A();
		A a2 = new A();
	
		a1.str1 = "123";
		a2.str1 = "456";
		System.out.println(a1.str1);//123
		System.out.println(a2.str1);//456
		
		A.str2 = "321";
		A.str2 = "654";
		System.out.println(A.str2);//654
		System.out.println(A.str2);//654
	}
	static class A {
	
		//Member variable
		String str1;
		
		//Static variable
		static String str2 ;
	}
}

3. Static code block

Static code block is called only when the class is loaded into the method area. This code block can only initialize static variables

The code block takes precedence over the constructor call when creating an object. The code block can initialize member variables and static variables

The construction method is called when the object is created. This method can initialize member variables and static variables

Member variable vs static variable

Member variable: a variable belonging to an object. Each object has an exclusive copy

Static variable: a variable belonging to a class. Each object shares one

package com.dream.test02;

public class A {
	
	String str1;
	static String str2;
	
	//Static code block: called when the class is loaded into the method area
	//Static variables can be initialized
	static{
		str2 = "yyy";//A.str2
		System.out.println("A Static code block for");
	}
	
	//Code block: object creation takes precedence over construction method calls
	//You can initialize static variables and member variables
	{
		str1 = "xxx";//this.str1
		str2 = "yyy";//A.str2
		System.out.println("A Code block for");
	}
	
	//Construction method: called when creating an object
	//You can initialize static variables and member variables
	public A() {
		
		str1 = "xxx";//this.str1
		str2 = "yyy";//A.str2
		System.out.println("A Construction method of");
	}
}

package com.dream.test02;

public class Test01 {
	
	public static void main(String[] args) {

		A a1 = new A();
		A a2 = new A();
	}
}

7, Inherit

Usage scenario: if multiple similar classes have the same attributes and methods, you can extract the same attributes and methods from the parent class

Benefits: reduced code redundancy

Deep inheritance:

When creating a subclass object, will the parent class constructor be called?
meeting
If you create a subclass object, will you create a parent object?
can't
When you create a subclass object, why do you call the parent class constructor?
The purpose is to store the properties of the parent class in the child class object

To create a subclass object, first call the parent class constructor or subclass constructor?
Call the subclass constructor first

To create a subclass object, first complete the parent class construction method or subclass construction method?
Complete the parent class construction method first

Can a subclass inherit the properties and methods privatized by the parent class?

You can inherit, but you can't call directly, but you can set public methods in the parent class, calling private properties and methods in public methods. This is called indirect invocation.

package com.dream.test04;

public class A {
	
	String aAtrr;

	public A() {
		System.out.println("Construction method of parent class");
	}
	
	//Parent class privatization attribute
	private String a = "xxx";
	
	//Parent class privatization method
	private void aMethod01(){
		System.out.println("Parent class privatization method");
	}
	
	public void aMethod02(){
		System.out.println(a);
		aMethod01();
	}
}

package com.dream.test04;

public class B extends A{
	
	String bAtrr;

	public B() {
		//super();// Call the parameterless construction of the parent class, which is implemented by default
		System.out.println("Construction method of subclass");
	}

}


package com.dream.test04;

public class Test01 {

	public static void main(String[] args) {
		
		B b = new B();
		
		b.aMethod02();
	}
}

8, super - parent class

Knowledge point: super - parent class
effect:
1.super. Member variable: call the non privatized member variable of the parent class
2.super. Member method: call the non privatized member method of the parent class
3.super(): call the non privatized constructor of the parent class
Note: the constructor of the parent class is called in the first sentence of the constructor of the child class

package com.dream.test06;

public class Person {
	
	private String name;
	private char sex;
	private int age;
	
	public Person() {
	}
	
	public Person(String name, char sex, int age) {
		this.name = name;
		this.sex = sex;
		this.age = age;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public char getSex() {
		return sex;
	}

	public void setSex(char sex) {
		this.sex = sex;
	}

	public int getAge() {
		return age;
	}

	public void setAge(int age) {
		this.age = age;
	}

	public void eat(){
		System.out.println(this.name + "Have a meal");
	}
	
	public void sleep(){
		System.out.println(this.name + "Sleep sleep");
	}

}

package com.dream.test06;

//			  Subclass 		   inherit 		   Parent class
public class Chinese extends Person{

	private String chineseId;
	
	public Chinese() {
	}

	public Chinese(String name, char sex, int age, String chineseId) {
		super(name, sex, age);
		this.chineseId = chineseId;
	}

	public String getChineseId() {
		return chineseId;
	}

	public void setChineseId(String chineseId) {
		this.chineseId = chineseId;
	}

	public void playTaiJi(){
		System.out.println(super.getName() + "Tai Chi");
	}
}

package com.dream.test06;

public class Test01 {
	
	public static void main(String[] args) {
	
		Chinese c = new Chinese("lk", 'male', 11, "12525");
		System.out.println(c.getName());
		System.out.println(c.getSex());
		System.out.println(c.getAge());
		System.out.println(c.getChineseId());
		//Call parent method
		c.eat();
		c.sleep();
		//Call subclass method
		c.playTaiJi();
		
	}
}

9, Rewrite

Meaning: rewriting is also called replication, which rewrites the methods in the parent class in the child class

Application scenario: when the parent method does not meet the needs of the child, the child can repeat the parent method

Conditions:

​ 1. Override the method of the parent class in a subclass

		2. The return value, method name and parameter list must be consistent with the method overridden by the parent class
		3. The access modifier cannot be more restrictive than the method overridden by the parent class
//Add this paragraph to the Chinese subclass. The run is to call the eat method of the subclass

@Override
	public void eat(){
		System.out.println(super.getName() + "Eat delicacies");
	}

10, Access modifier

Meaning: modifies classes, methods and attributes, and defines the scope of use

Access modifier This categoryThis packageOther steamed stuffed bun subclassesOther packages
privateOK
defaultOKOK
protectedOKOKOK
publicOkOKOKOk

Keywords: Java

Added by Jago6060 on Mon, 03 Jan 2022 06:36:23 +0200