Day09 learning java (object-oriented, relationship between classes, design patterns)

Posted by The Stranger on Thu, 13 Jan 2022 14:18:15 +0100

object-oriented

1. Relationship between classes

1.1 succession

Using extensions, there is single inheritance between classes, multiple inheritance between interfaces, and multiple inheritance are separated by commas

1.2 realization

class A{
}
//Single inheritance between classes
class B extends A{
}
interface C{
}
interface D{
}
//Multiple inheritance between interfaces
interface E extends C,D{
}
//There are multiple implementations between classes and interfaces. After a class implements an interface, it must implement all abstract methods
//Otherwise, the class needs to add an abstract modifier
class F implements C,D{
}

1.3 dependency

Local variables, save another class object reference, and after the method is executed, the relationship does not exist

	public static void main(String[] args) {
		A a = new A();
	}

1.4 Association

The member variable holds another class object reference, and the relationship is long-term

1.5 polymerization

The whole and part have independent declaration cycles

public class Test_02 {
	Person person;

	public Test_02(Person person) {
		// It is not created manually and needs to be called to pass
		this.person = person;
	}

	public static void main(String[] args) {
		Person person = new Person();
		Test_02 t1 = new Test_02(person);
		System.out.println(t1);
		System.out.println(t1.person);
		Test_02 t2 = new Test_02(person);
		System.out.println(t2);
		System.out.println(t2.person);
		// Although the Test object is destroyed at this time, the reference of the person object is still saved at the call
		// Then, the person will not be garbage data and can be reused without being recycled
		t1 = null;
		t2 = null;
		System.out.println(person);

	}

}

class Person {
}

1.6 combination

Part and whole cannot be separated

public class Test {
	// The life cycle of the person object is bound to the Test object
	// When multiple Test objects are created, multiple Person objects are usually created
	// Since the person object is created manually, it means that there is no reference to person saved elsewhere
	// When the test object is destroyed, the person object is also treated as garbage and waiting for recycling
	Person person = new Person();

	public static void main(String[] args) {
		Test t = new Test();
		System.out.println(t);
		System.out.println(t.person);
		t = null;
		System.out.println(t.person);
	}

}

class Person {
}

2. Internal class

2.1 general

When there is an internal part of a thing that needs a complete structure to describe, and the internal complete structure only provides services for external things, it is best to use the internal class for the whole internal complete structure
In Java, the definition of one class is allowed to be located inside another class. The former is called internal class and the latter is called external class.
Inner class is generally used within the class or statement block that defines it. When it is referenced externally, it must be given a complete name.

2.2 member internal class

It can be regarded as a member variable
Advantage: you can access private properties of external classes

package Outclass;

public class OutClass_01 {
	private static String s1 = "Privatized static variables";
	private String s2 = "Privatized member variable";

	// Member inner class
	// You can use the access control modifier
	// Internal class name after compilation: external class name $internal class name
	public class InnerClass {
		// There can be no static declaration in an inner class
		// static int i =1;
		public void m1() {
			// All properties of the external class can be accessed directly in the internal class of the member
			System.out.println(s1);
			System.out.println(s2);
		}
	}

	public static void main(String[] args) {
		// 1. Create an external class object
		OutClass_01 o = new OutClass_01();
		// 2. Create an internal class object
		InnerClass iclass = o.new InnerClass();
		InnerClass iclass1 = o.new InnerClass();
		// 3. Access properties
		iclass.m1();
		System.out.println(iclass);
		System.out.println(iclass1);
	}

}

2.3 static internal class

It can be regarded as a static variable. In a static inner class, you can declare member properties or static properties
You cannot directly access the member properties of an external class in a static internal class. You need to create an external class object to access it

package Outclass;

public class OutClass_02 {
	private static String s1 = "Privatized static variables";
	private String s2 = "Privatized member variable";

	// Static inner class
	static class InnerClass {
		public static void m1() {
			System.out.println(s1);
			// System.out.println(s2); ← error will be reported
		}

		public void m2() {
			System.out.println(s1);
			// System.out.println(s2); ← error will be reported
		}
	}

	public static void main(String[] args) {
		// Call m1
		OutClass_02.InnerClass.m1();
		// The class name can be omitted from the current class
		InnerClass.m1();
		// Create a static inner class object
		InnerClass iclass = new OutClass_02.InnerClass();
		// The class name in the current class can be omitted
		InnerClass iclass2 = new InnerClass();
		// Call m2
		iclass.m2();
	}

}

2.4 local internal class

It can be regarded as a local variable
When accessing a local variable in an external method through a local inner class, the variable needs to be modified with final, but since 1.8, final can be omitted

Local inner classes cannot use permission modifiers or static
The class name of the local internal class is the external class name $1, and the internal class name is $2 if the class names are the same, and so on

package Outclass;

public class OutClass_03 {
	public void m1() {
		int age = 19;
		class InnerClass {
			public void m2() {
				System.out.println(age);
				System.out.println(s1);
				System.out.println(s2);
			}
		}
		// Local inner class call
		InnerClass iclass = new InnerClass();
		System.out.println(iclass);
		iclass.m2();
	}

	public static void m3() {
		final int age = 20;
		class InnerClass {
			public void m1() {
				System.out.println(age);
				System.out.println(s1);
				// A local inner class in a static method cannot directly use the member properties in an outer class
				// System.out.println(s2);
			}

		}
		// Local inner class call
		InnerClass a = new InnerClass();
		System.out.println(a);
		a.m1();
	}

	private static String s1 = "Privatized static variables";
	private String s2 = "Privatized member variable";

	public static void main(String[] args) {
		new OutClass_03().m1();
		OutClass_03.m3();
	}

}

2.5 anonymous inner class

When you need to pass in an interface implementation class object during method call, you can write an anonymous class directly without writing the implementation class
The class name is external class class name $1, external class class name $2, and so on

package Outclass;

public class OutClass_04 {

	public static void test(IUserService userService) {
		userService.login();
	}

	public static void main(String[] args) {
		test(new IUserService() {

			@Override
			public void login() {
				System.out.println(this);
				System.out.println("Login succeeded");
			}

		});
	}
}

interface IUserService {
	public void login();
}

class UserService implements IUserService {
	public void login() {
		System.out.println("Login succeeded");
	}
}

3. Design mode

3.1 general

Design pattern is that in coding practice, people find that many codes are often used, so they summarize and form a fixed structure, which generally represents the best implementation method
Only one instantiated object can be created for the current class
1. To control the number of objects created, it means that the client cannot directly operate the construction method, so the construction method needs to be privatized
2. The class provides a method for obtaining objects
(there is no input parameter, and the output parameter is a static method of the current class type)
3. Create a static variable to save the instantiated object

3.2 hungry Han mode

Objects are created during the load phase

package SingleTon;

public class SingleTon_01 {
	private static SingleTon_01 s =new SingleTon_01();
	public static SingleTon_01 getInstance(){
		return s;
	}
}

3.3 lazy mode

Create objects when needed

package SingleTon;

//Create objects when they are used, which is called lazy mode
public class SingleTon_02 {
	private static SingleTon_02 s = null;

	public static SingleTon_02 getInstance() {
		if (s == null)
			s = new SingleTon_02();
		return s;
	}

}

Topics: Java Back-end