Inclusion and dependency of class relationships

Keywords: Java

  • The number of classes has increased - > need to manage classes - > package (can be understood as a folder). Package and import appear at the same time. Write package first and then import. There can only be one package and multiple imports.

is-a (generalized) inheritance implementation

has-a (inclusive) combination aggregation Association

use-a dependency

1, Inheritance relationship

1. If a subclass wants to inherit from the parent class, it can be implemented through the extend keyword.

2. The subclass inherits the parent class and can call the properties and methods in the parent class (public protected)

2.1 construction method strictly speaking, it does not count as a subclass inheritance, but simply calls the construction method of the parent class by default when the subclass calls the construction method

2.2 in a strict sense, a program block is not a subclass inheritance (the subclass cannot be called by itself and has no name)

The subclass of a program block cannot be called directly. Before the subclass executes the construction method
By default, the constructor of the parent class is called. The constructor of the parent class is automatically executed before the constructor of the parent class

3. In addition to inheriting the properties and methods of the parent class, a subclass can also add its own unique members (properties and methods)

4. When the method inherited from the parent class cannot meet the needs of the child class, the method can be overload ed.

5. Each class has an inherited class. If the extend keyword is defined, it directly inherits the class (indirectly inherits the Object class). If extensions is not defined, it inherits the Object class by default.

6. Inheritance in Java is single inheritance, which means that only one class is allowed to be written after an extends keyword, and multiple inheritance can be realized in the form of transmission (subsequent multiple implementations).

7. Deeply understand the storage structure inherited in memory.

8. The difference between this keyword and super keyword.

2, has-a containment relation (combination aggregation Association)

		It's different in terms of intimacy
		combination-->The relationship between human brain and human heart
				The relationship between the whole and the part is inseparable. Both appear and die
		polymerization-->Car and wheel computers and motherboards
				The relationship between whole and part may be separated when created
		relation-->People have cars, people have computers
				The relationship between whole and part can be divided and later formed together
		from Java The program describes such a relationship by storing the object of one class as an attribute of another class
package contains;

public class Wheel{
	public String brand;//brand
	public int size;//size
	public String color;//colour
	//Construction method
	public Wheel(){}
	public Wheel(String brand,int size,String color){
	public void turn(){
		System.out.println("The wheels of the car can rotate");
package contains;

public class Car{
	public String brand;//Automobile brand
	public String type;//model
	public String color;//colour
	public Wheel wheel;//There is a wheel in the car ------ >

	//Construction method
	public Car(){}
	public Car(String brand,String type,String color,Wheel wheel){
	public void showCar(){
		System.out.println("This is a car"+brand+"brand"+type+"model"+color+"My car");
		System.out.println("The car is carrying"+wheel.brand+"Card"+wheel.size+"size"+wheel.color+"Colored wheels");
		wheel.turn();//The method of the car wheel called by a certain object must be called by the car wheel object and can be placed anywhere

package contains;

public class Test{
	public static void main(String[] args){
		Car car=new Car();
		car.color="Sapphire blue";
		car.wheel=new Wheel();
		car.wheel.color="Cool black";
		car.showCar();//Show car
		//Or one line
		//Car car=new Car("BMW", "Z4", "Sapphire Blue", new Wheel("Michelin", 400, "cool black");
		//car.showCar();// Show car

3, use-a (need-a) dependency

	Butcher kills pig     Farmers raise pigs
	A butcher like
			There's one thing you can do to kill a pig
			Need a pig
	It's not the relationship between the whole and the part. Something has a relationship
	Once the temporary combination is completed, the relationship will be dissolved
	Java The form of procedure system is:
	An object of another class is used in the method of one class
			The first is to pass parameters in a method
			The second can be created in the method itself
package rely;

public class Pig{//Describe pig
	private String name;//name
	private int weight=20;//weight

	//Construction method
	public Pig(){}
	public Pig(Stirng name){;
	//Describe a method to indicate that the pig has been killed
	public void beKilled(){
		System.out.println("It's terrible to be killed!");

	//Describe a way to make pigs grow meat
	//  Every month is twice as high as the previous month
	//The return value represents the weight of the growing pig
	public void growUp(int month){

	//Describe a way for a pig to tell him his weight
	public int getWeight(){
		return this.weight;
	public String getName(){
package rely;

public class Butcher{//Describe the butcher
	//Attribute name has knife

	//Describing a butcher's method of killing pigs requires providing conditions for a pig
	public void  killPig(Pig pig){
		System.out.println("The butcher executed the method of killing pigs");
		String pigName=pig.getName();
		int pigWeight=pig.getWeight();
		System.out.println(pigName+"Your weight is:"+pigWeight);

package rely;

public class Farmer{//farmer
	//Farmers raise pigs ---- >
	//Parameter ---- > months return value ---- > is a pig
	public Pig feedPig(int month){
		Pig pig=new Pig("floret");//Dependency - > pig object is used in butcher's method
		return pig;
package  rely;

public static void main(String[] args){
	//Create farmer object
	Farmer farmer=new Farmer();
	//A farmer does one thing -- > raising pigs
	Pig pig=farmer.feedPig(5);
	//Create butcher object
	Butcher butcher=new Butcher();
	//Butchers do things -- > kill pigs

The design class relationship follows the principle of high cohesion and low coupling
Coupling: tight inheritance (Implementation) > inclusion > dependency

Small tasks:
0. Simulate farmers raising pigs, butchers killing pigs, and simulate the relationship between cars and wheels
1. Simulate a student using a computer in the computer room
There is a computer room
There is a computer with power on / off status. The computer is turned on and turned off while in use
There is a student
Let students enter the computer room to use computers
Expansion: five computers are placed in the computer room so that students can enter the computer room and choose a closed computer to use
Five students also entered the computer room one after another
2. Simulate the relationship between a police car and a car tachometer
The speedometer measures the speed of a car at 100 meters for 5 seconds
If the car is speeding, the police car will chase it
If the police car pursues successfully, the pursuit time is output
If the police car can't catch up, the output can't catch up

Posted by twooton on Sat, 06 Nov 2021 16:12:02 -0700