Common Object methods, internal classes

Posted by colforbin05 on Fri, 15 Oct 2021 19:55:04 +0200

one   Common Object methods

1.1 what is an object?

         Object is the root class provided by java. All classes inherit object directly or indirectly. Because inheritance in java can be passed, all methods in object are methods of all classes

1.2 equals method

1.2.1   Design purpose

Compares whether two objects are equal

1.2.2 difference between "= ="

     Using = = to compare basic data types can compare the size of values, but comparing reference types can only compare memory addresses. The default equals method in object compares memory addresses, because people don't know what attributes you want to compare? You need to overwrite it according to your own needs

eg:
             public boolean equals(Object obj){
                return this ==obj;
             }

example  

Requirement: the same id and the same name mean the same student

code:

public class Equals_01 {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		String a1="asd";
		String a2="asd";
		System.out.println(a1.equals(a2));//true
		Student s1=new Student(001, "Zhang San");
		Student s2=new Student(001, "Zhang San");
		System.out.println(s1==s2);//false because the addresses s1 and s2 are different
		System.out.println(s1.equals(s2));//true is returned only if the name and student number are the same;
	}
}
class Student {
	private int id;
	private String name;
	public Student(int id,String name){
		this.id=id;
		this.name=name;
		
	}

	public String getName(){
		return this.name;
	}
	@Override
	public boolean equals(Object obj){
		//Requirement: if the specified id is the same, it means the same student
		//1. Compare the addresses first. If the addresses are the same, it means that they are the same object, and return true directly;
		if(this==obj){
			return true;
		}
		//2. If it is not the same object, you need to judge whether it is of the same kind. Different classes are not comparable
		if(obj instanceof Student){
			//Transition down, or you will lose subclass specific attributes (the disadvantage of polymorphism)
				Student s=(Student)obj;
				if(this.id==s.id && this.name.equals(s.name)){
					return true;
				}
			}
		return false;

}
}

one point three   finalize method

1.3.1 design purpose

Four features of JVM: cross platform      Object oriented multithreading automatic garbage collection

         Garbage: if an object has no reference to it, it will be regarded as garbage data, and no one can find it. It is equivalent to useless resources and a waste of space

1.3.1   Characteristics of finalize method

1 is the method in Object, which means that all class objects have it
2 this method does not need to be called by the programmer, but is called automatically by the system
3 when an object is recycled, the finalize method of the object will be called automatically
4 if we call this method manually, it is just a method call and will not be recycled

example

public class Finalize_01 {
public static void main(String[] args) {
	Finalize_01 finalize_01=new Finalize_01();
	finalize_01=null;
	for(int i=1;i<10000000;i++){
		System.gc();//Programmers can "suggest" garbage collection
	}
	//Execution result: day_05._02_Finalize.Finalize_01@7ad74083 recovery
	//The execution results may not appear every time, so you can use multiple suggestions or increase the amount of garbage
}
@Override
	protected void finalize() 	{
	System.out.println(this+"recovery");
}
}

1.4 hashCode method

1.4.1 design purpose

Method is designed to return a string representation of the object

1.4.2 hash conflict:   

If an object takes this hash value, the value must be the same, but different objects may get the same hash value, which is also called hash conflict

The hashCode method is a hash value obtained according to the hash method
hash algorithm: it is a secure encryption algorithm. Changing data of variable length into data of fixed length does not guarantee its uniqueness
1.4.3 what should I do if there is a hash conflict and the uniqueness of the object cannot be guaranteed?
  Use equals
1 compare the hash values first. If they are different, they are not the same object
2. If different, compare with equals

1.5 toString method

1.5.1 design purpose

Method is designed to return a string representation of the object

1.5.2 realization principle

         When outputting data of a reference type, the toString method of the Object will be called automatically  , The default toString method in Object is to print the memory address of the Object. If you do not want to print the memory address when outputting an Object, but print the content in the specified format, you need to override the toString method

example

public class toString_01 {
	public static void main(String[] args) {
		Person person=new Person(001,"Zhang San",18);
		Person person2=new Person(002, "Li Si", 19);
		System.out.println(person);//Running result: Name: Zhang San id:1 Age: 18
		System.out.println(person2);//Running result: Name: Li Si id:2 age: 19
}
}
class Person{
	private int id ;
	private String name;
	private int age ;
	public Person(int id, String name, int age) {
		super();
		this.id = id;
		this.name = name;
		this.age = age;
	}
	@Override
	public String toString() {
		
		return "full name:   "+this.name+"    id:"+this.id+"   Age  :"+this.age;
	}
} 

II. Internal category    

2.1 internal concepts

There are other classes in the class body. This class is called the inner class

         When a class needs a complete internal structure to describe, and the complete internal structure only provides services for external classes, the class can be defined as an internal class

2.2 internal advantages

You can access all properties inside the external, including private properties

2.3 use of internal classes

1. Member inner classes can be regarded as member variables

Characteristics of member internal classes:
    1 member inner class, no static modifier
    two   You can use the permission control modifier
    3. All properties of the external class can be accessed in the internal class of the member
    four   Cannot have a static declaration in a class inside a member
    5. The compiled class name of the internal class is  : External class name $internal class name

example:

public class OuterClass_01 {
	private static String s1="Privatization static";
	private String s2="Privatization into member state";
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		OuterClass_01 outerClass_01=new OuterClass_01();
		InnerClass innerClass=outerClass_01.new InnerClass();
		innerClass.m2();//Output result: privatization static privatization into member state
	}
class InnerClass{
//	static int i=1;
//	public static void m1(){}
	public void m2(){
		System.out.println(s1);
		System.out.println(s2);
	}
}
}


2. Static inner classes can be regarded as static variables

Static internal class features:
      one   Static inner classes are decorated with static
      two   You can use the permission control modifier
      three   The compiled class name of the internal class is: external class name $internal class name
      four   In static inner classes, members can also be declared, and static can also be declared
      five   In a static internal class, you cannot directly access the member properties of an external class. You need an object

example

public class OuterClass_02 {
	private static String s1="Privatization static";
	private String s2="Privatized member";
	static class InnerClass{
		public void m1() {
			System.out.println(s1);
			// System.out.println(s2);
			// The member property of an external class can only be called when an external class object is created
			OuterClass_02 o = new OuterClass_02();
			System.out.println(o.s2);
		}
//		static int i=1;
//		public static void m1(){}
		public static void m2(){
			System.out.println(s1);
//			System.out.println(s2);
			OuterClass_02 o=new OuterClass_02();
			System.out.println(o.s2);	
		}
	}
	public static void main(String[] args) {
		OuterClass_02.InnerClass.m2();
		InnerClass.m2();//The class name can be omitted in the current class
		//create object
		InnerClass innerClass=new OuterClass_02.InnerClass();
			//External class names can be completely omitted
		InnerClass innerClass2=new InnerClass();
		innerClass2.m1();
	}
}


3. Local internal classes can be regarded as local variables (classes in methods cannot write any modifiers (static,public, etc.)

Local internal class features:

1. Local inner class: the class in the method is the local inner class
two   Local inner classes cannot have static declarations, and static modifiers are not allowed for this class
three   If the external method is a member method, the internal class can directly access all properties of the external class
4 if the external method is a static method, the internal class cannot directly access the member properties of the external class, and an object is required
5 local internal classes can also directly access local variables of external methods, but they need to be modified with fina. final can be omitted from 1.8
    in other words  , If the local internal class uses the local variable of the external method, the local variable is decorated with final,       The value cannot be changed
  6. Internal class name:
         If the internal class names of multiple methods are inconsistent, this is the case: external class name $internal class name,  
         If the internal class names of multiple methods are consistent, this is the case: external class name $1, internal class name, $2, internal class name       Class name, external class name $3, internal class name

example

public class OuterClass_03 {
	private static String s1 = "Privatization static";
	private String s2 = "Privatized member";
	
	public void m1(){
		final int i=100;
		int i2=200;
		class InnerClass {
		//static int x=2;
		public void m3(){
			System.out.println(s1);
			System.out.println(s2);
			System.out.println(i);
			System.out.println(i2);
			// Cannot change in
			// i=2;
			// Local inner classes can only be used inside methods
			// Create an internal class object
			InnerClass innerClass = new InnerClass();
			innerClass.m3();
		}
		
	}
	
}
public static void m2(){
		final int a=100;
		int a2=200;
		class InnerClass{
			public void m3(){
				System.out.println(s1);
				//An internal class in a static method cannot directly access the member properties of an external class. You need to create an external class object
//				System.out.println(s2);
				OuterClass_03 outerClass_03=new OuterClass_03();
				System.out.println(outerClass_03.s2);
			}
		}
		// Cannot change in
				// i=2;
				// Local inner classes can only be used inside methods
				// Create an internal class object
				InnerClass innerClass = new InnerClass();
				innerClass.m3();
				System.out.println("111111");
	}
public static void main(String[] args) {
	OuterClass_03 outerClass_03 = new OuterClass_03();
	outerClass_03.m1();
	m2();
}
}


4. Anonymous inner classes can be regarded as arguments     

        See for the next article

    

Topics: Java