day12 encapsulation and inheritance

Posted by judgenules on Wed, 23 Feb 2022 11:41:21 +0100

1. Implementation principle of static keyword

Method area: the method area is a specification proposed by SUN company. JDK8 was called permanent generation before, and JDK8 began to call it metadata.

Static area: refers to the area where static related information is stored.

Process of creating objects:

1. When we create a new object, the JVM will first check in the method area whether the class file of this class has been loaded.

If it is not loaded, first load the class file into the method area, and then the static related information will be initialized

If it is already loaded, proceed to step 2 directly

2. Open up space in the heap. At this time, the instance attribute will have a default value

3. Point the reference in the stack to the space in the heap

The metadata will not be recycled in the program's execution area for a long time. In addition, because the data in the method area is initialized early, it is later called metadata.

1.1 code block

When the JVM loads a class, it loads static code blocks

If there are multiple static blocks, load them sequentially

Each static code block is executed only once

1.2 static variables

The member variables of the class include

Class variable (static variable) the variable modified by static has only one copy in memory. The static variable can be accessed directly in any method. In other classes, it can be accessed directly through the class name

For variables whose instance variables are not modified by static, memory will be allocated for instance variables every time an instance is created. Instance variables have multiple copies in memory and do not affect each other

1.3 static method

Static method: you can directly access the static method through the class name. You can't use this and super in the static method. You can't directly access the instance variables and instance methods of the class. You can directly access the static variables and static methods of the class

Instance method: through instance access, you can directly access the static variables, static methods, instance variables and instance methods of the class. Static methods must be implemented

1.4 about access

Direct access between peers

Non static can directly access static

Static objects cannot be accessed directly. Non static objects must be new first

2. Exercises

Simulate the voter voting process: a group of voters vote, each voter is only allowed to vote once, and stop voting when the total number of votes reaches 100

package com.qfedu.test2;
/**
 * 	Voter class
 * @author WHD
 *
 */
public class Vote {
	static int ticketCount;
	String name;
	
	public boolean voteFor() {
		if(ticketCount < 100) {
			ticketCount++;
			System.out.println(name + "Cast a vote and there's still a lot left" + (100 - ticketCount) + "ticket");
			return true;
		}else {
			System.out.println("Voting deadline");
			return false;
		}
	}
	
	public static void main(String[] args) {
		Vote zhaosi = new Vote();
		zhaosi.name = "Zhao Si";
		zhaosi.voteFor();
		
		Vote guangkun = new Vote();
		guangkun.name = "Guangkun";
		guangkun.voteFor();
		
		Vote liuneng = new Vote();
		liuneng.name = "Lennon ";
		liuneng.voteFor();
		
		for (int i = 1; i < 200; i++) {
			Vote v1 = new Vote();
			v1.name = i + "Voter No";
			if(!v1.voteFor()) {
				break;
			}
		}		
		
	}
}

3. Succession

Inheritance is one of the important means to realize code reuse in Java. Only single root inheritance is supported in Java, that is, a class can only have one direct parent class

Subclasses and superclasses must conform to the relationship of is-a

Use the extends keyword to implement inheritance

package com.qfedu.test3;
/**
 * 	Dog class
 * 	name
 * 	Health value
 * 	Love value
 * 	varieties
 * 
 * 	Nonparametric structure
 * 	Printing information method
 * @author WHD
 *
 */
public class Dog extends Pet{
	private String strain;
	public String getStrain() {
		return strain;
	}
	public void setStrain(String strain) {
		this.strain = strain;
	}
	
	public Dog() {
	}	
}
package com.qfedu.test3;
/**
 * 	Penguins
 * 	name
 * 	Health value
 * 	Love value
 * 	Gender
 * 
 * 	Nonparametric structure
 * 	Printing information method
 * @author WHD
 *
 */
public class Penguin extends Pet{
	
	private String sex;
	
	public String getSex() {
		return sex;
	}
	public void setSex(String sex) {
		this.sex = sex;
	}
	public Penguin() {
	}	
}
package com.qfedu.test3;
/**
 * 	Pet class parent class 
 * 	The parent class retains the properties and methods shared by the child classes
 * @author WHD
 *
 */
public class Pet {
	private String name;
	private int health;
	private int love;
	
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public int getHealth() {
		return health;
	}
	public void setHealth(int health) {
		this.health = health;
	}
	public int getLove() {
		return love;
	}
	public void setLove(int love) {
		this.love = love;
	}
	
	public void print() {
		System.out.println("The pet's name is:" + name + ",The health value is:" + health + ",The value of love is:" + love);
	}
}
package com.qfedu.test3;

public class TestPet {
	public static void main(String[] args) {
		Dog dog1 = new Dog();
		dog1.setName("chinese rhubarb");
		dog1.setHealth(100);
		dog1.setLove(100);
		dog1.setStrain("Erha");
		
		dog1.print(); // At present, the printing information is incomplete and there is a lack of varieties
		System.out.println("---------------------------------");
		
		Penguin p1 = new Penguin();
		p1.setName("Big white");
		p1.setHealth(100);
		p1.setLove(100);
		p1.setSex("female");
		
		p1.print();// At present, the printed information is incomplete and lacks gender
		
	}
}

4.super keyword

super keyword: indicates the parent class object

You can access the allowed by the parent class access permission:

Properties:

method:

Construction method:

4.1 access properties

We can access the attributes allowed by the parent class access permission through the super keyword

package com.qfedu.test4;
/**
 * 	Dog class
 * 	varieties
 * 
 * 	Nonparametric structure
 * 	Printing information method
 * @author WHD
 *
 */
public class Dog extends Pet{
	private String strain;
	public String getStrain() {
		return strain;
	}
	public void setStrain(String strain) {
		this.strain = strain;
	}
	
	public Dog() {
	}

	
	public Dog(String name,int health,int love,String strain) {
		this.strain = strain;
		super.name = name;
		super.health = health;
		super.love = love;
	}

}

package com.qfedu.test5;

import com.qfedu.test4.Pet;

public class Cat extends Pet{
	private String furColor;

	public String getFurColor() {
		return furColor;
	}

	public void setFurColor(String furColor) {
		this.furColor = furColor;
	}
	
	public Cat() {}
	
	public Cat(String name,int health,int love,String furColor) {
		this.furColor = furColor;
		this.name = name;
		super.health = health;
		super.love = love;
    }
}
package com.qfedu.test4;
/**
 * 	Pet class parent class 
 * 	The parent class retains the properties and methods shared by the child classes
 * @author WHD
 *
 */
public class Pet {
	protected String name;
	protected int health;
	protected int love;
	
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public int getHealth() {
		return health;
	}
	public void setHealth(int health) {
		this.health = health;
	}
	public int getLove() {
		return love;
	}
	public void setLove(int love) {
		this.love = love;
	}
}

4.2 access method

We can use the super keyword to access the methods of parent class access permission

package com.qfedu.test4;
/**
 * 	Pet class parent class 
 * 	The parent class retains the properties and methods shared by the child classes
 * @author WHD
 *
 */
public class Pet {
	protected String name;
	protected int health;
	protected int love;
	
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public int getHealth() {
		return health;
	}
	public void setHealth(int health) {
		this.health = health;
	}
	public int getLove() {
		return love;
	}
	public void setLove(int love) {
		this.love = love;
	}
	
	public void print() {
		System.out.println("The pet's name is:" + name + ",The health value is:" + health + ",The value of love is:" + love);
	}

}
package com.qfedu.test4;
/**
 * 	Dog class
 * 	varieties
 * 
 * 	Nonparametric structure
 * 	Printing information method
 * @author WHD
 *
 */
public class Dog extends Pet{
	private String strain;
	public String getStrain() {
		return strain;
	}
	public void setStrain(String strain) {
		this.strain = strain;
	}
	
	public Dog() {
	}
	
	public void printDog() {
		super.print();
		System.out.println("The breed of dog is:" + strain);
	}
	
}
package com.qfedu.test4;
/**
 * 	Penguins
 * 	Gender
 * 
 * 	Nonparametric structure
 * 	Printing information method
 * @author WHD
 *
 */
public class Penguin extends Pet{
	private String sex;
	
	public String getSex() {
		return sex;
	}
	public void setSex(String sex) {
		this.sex = sex;
	}
	public Penguin() {
	}
	
	public void printPenguin() {
		super.print();
		System.out.println("The sex of penguins is:" + sex);
	}
}

4.3 access construction method

1.super keyword the construction method of accessing the parent class must be in the first sentence of the subclass construction

2. The subclass constructs the parameterless construction method of the parent class by default, unless the parameterless construction method of the parent class is explicitly called

That is, a subclass must access one of the parameterized or nonparametric constructs of the parent class

package com.qfedu.test6;
/**
 * 	Pet class parent class 
 * 	The parent class retains the properties and methods shared by the child classes
 * @author WHD
 *
 */
public class Pet {
	protected String name;
	protected int health;
	protected int love;
	
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public int getHealth() {
		return health;
	}
	public void setHealth(int health) {
		this.health = health;
	}
	public int getLove() {
		return love;
	}
	public void setLove(int love) {
		this.love = love;
	}

	
	public Pet() {
		System.out.println("Pet The parameterless construction of the parent class was executed");
	}
	
	public Pet(String name,int health,int love) {
		System.out.println("Pet The parameterized constructor of the parent class is executed");
		this.health = health;
		this.name = name;
		this.love = love;
	}

}
package com.qfedu.test6;
/**
 * 	Dog class
 * 	varieties
 * 
 * 	Nonparametric structure
 * 	Printing information method
 * @author WHD
 *
 */
public class Dog extends Pet{
	private String strain;
	public String getStrain() {
		return strain;
	}
	public void setStrain(String strain) {
		this.strain = strain;
	}
	
	public Dog() {
		System.out.println("Dog The parameterless construction method of the subclass is implemented");
	}
	
	public Dog(String name,int health,int love,String  strain) {
		super(name, health, love);
		this.strain =strain;
	}

}
package com.qfedu.test6;
/**
 * 	Penguins
 * 	Gender
 * 
 * 	Nonparametric structure
 * 	Printing information method
 * @author WHD
 *
 */
public class Penguin extends Pet{
	private String sex;
	
	public String getSex() {
		return sex;
	}
	public void setSex(String sex) {
		this.sex = sex;
	}
	public Penguin() {
		System.out.println("Penguin The parameterless construction method of the subclass is implemented");
	}
}
package com.qfedu.test6;
/**	
 * 	super Keyword: represents the parent class object
 * 	You can access the allowed by the parent class access permission:
 * 		Properties:
 * 		method:
 * 		Construction method:
 * 		1.super Keyword to access the construction method of the parent class must be in the first sentence of the subclass construction
 * 		2.A subclass construct accesses the parameterless constructor of the parent class by default, unless the parameterless constructor of the parent class is explicitly called
 * 		That is, a subclass must access one of the parameterized or nonparametric constructs of the parent class
 * @author WHD
 *
 */
public class TestPet {
	public static void main(String[] args) {
		Dog dog = new Dog("chinese rhubarb", 100, 100, "Golden hair");
		
		dog.print();
		
		System.out.println("---------------------------------");
		
		Penguin p1 = new Penguin();
		
		System.out.println("---------------------------------");
		
		Dog dog1 = new Dog();

	}
}

5. Succession summary

  • super keyword

Super keyword to access the members of the parent class. Super can only appear in the methods and construction methods of the child class. When super calls the construction method, it can only be the first sentence. Super cannot access the private members of the parent class

  • Construction method under inheritance condition

The call rule subclass construction method of the construction method under inheritance conditions does not explicitly call the parameterized construction method of the parent class through super, nor does it explicitly call its own other construction methods through this. The system calls the parameterized construction method of the parent class by default. The subclass construction method executes the corresponding construction method of the parent class through super by explicitly calling the parameterized construction method of the parent class, Instead of executing the parent class parameterless construction method, the subclass construction method explicitly calls its own other construction methods through this, and applies the above two rules in the corresponding construction methods

  • What does the subclass inherit from the parent class?

What does the subclass inherit from the parent class? Inherit the public and protected modified attributes and methods, regardless of whether the subclass and parent class inherit the attributes and methods modified by the default permission modifier in the same package, but the subclass and parent class must be in the same package

Please have a look at Yaqing!

Topics: Java Back-end