The relationship between Object class, inner class, class and class in java

The Object class is the root class (ancestor class) in Java;

  • All classes will inherit the Object class, and empty classes will also inherit;
  • Therefore, the method in Object is a function that all classes can have;
  • Object xxx = new xxx(); Object can be polymorphic;
  • Since polymorphism can occur, if a method needs to pass data, we can write Object when we are not sure of the data type;
  • How to learn a method?
  •  1.该方法是谁的?
    
  •  2.是什么方法?(成员还是静态)
    
  •  3.方法的功能是什么?入参出参是什么?能实现什么,我们需要给它什么,它能给我们提供什么;
    
  •  4.方法的覆写,什么情况下覆写?
    
  • The design purpose of equals is to compare whether two objects are equal;
  • But it does not implement this function, we need to overwrite it according to our own needs;
  • The two objects can be anything, and only when specific to the instance, we can achieve this method by overwriting;
  • @author MARECLE_YING
  • @Date January 18, 2021
    */
    public class _02Equals { //main method public static void main (String[]args){ // create objects s1, s2; // assign values ​​to them through parameterized construction and assignment Student s1 = new Student(18,"李阳"); Student s2 = new Student(19,"李阳"); // Output comparison == comparison is the memory address false System.out.println(s1 == s2); / / equals Compare two objects System.out.println(s1.equals(s2)); } } // Create a student class class Student{ // Because you want to use getter/setter methods, you must privatize the member variable private int age; private String name; // Use getter/setter methods public int getAge() { return age; } public void setAge(int age) {






















    this.age = age;
    }
    public String getName() { return name; } public void setName(String name) { this.name = name; } // Create a parametric structure public Student(int age, String name) { super( ); this.age = age; this.name = name; } // Create a non-argument construction public Student() { super(); // TODO Auto-generated constructor stub } // Overwrite the equals method according to the requirements // Requirements The same name is a student @Override public boolean equals(Object arg0) { // 1. Compare addresses; equal addresses indicate that they are the same object and the values ​​must be equal if(this == arg0){ return true; }
























    //2. Determine whether the type of the object is the current class type, otherwise return false, different classes are not comparable;
    if(arg0 instanceof Student){ // 3. Force type conversion, compare the desired data Student s2 = (Student )arg0; //Determine whether the values ​​are equal // if (name.equals(s2.name) && age == s2.age){ if (name.equals(s2.name)){ return true; } } return false; }









}

equals

  • Design purpose: compare whether two objects are equal;
  • == and !=: compare basic data types and compare the size of the value;
  •  					比较引用数据类型,比较内存地址;
    
  • equals compares memory addresses by default;
  •  		   实际中我们常常比较的都是对象的有意义的属性;
    
  • To compare strings for equality, equals should be used, because == compares memory addresses;
  • Override the equals method in the String class, and compare the value, not the memory address;
  • So we can also use equals to compare the properties of two objects of the same kind by overwriting;
  • Different types of objects are not comparable;
  • Implementation method: Any reference type comparison must be converted to a basic type for comparison; unless you want to compare whether two addresses are the same;
  • Object-oriented is a form of basic data encapsulation, so we must convert to basic type comparison when we compare;
  • @author MARECLE_YING
  • @Date January 18, 2021
    /
    public class _01Equals { // main method public static void main(String[]args){ // Create two String type objects s1 s2 s3 String s1 = new String("998") ; String s2 = new String("1088"); String s3 = new String("1088"); //Output the comparison result, the comparison is the memory address, the address must be different; false System.out.println(s1 = = s2); // equals compares the size of the value // Syntax structure: object 1.equals(object 2) // values ​​are different, false System.out.println(s1.equals(s2)); // values ​​are the same , True System.out.println(s2.equals(s3)); } } package _16_Object_Finalize; /

















    *

finalize method function

  • Garbage calls this method in the last step of the collection, what you want to achieve overwrite Object;
  • ①finalize is a method for every object, because they will eventually become garbage and be recycled;
  • ②Does not need to be called by the program, the system automatically calls
  • ③ What is garbage? In Java, if there are no more references to this object, then this object has no value, and the system will treat it as garbage data;
  • Waiting to be recycled, before recycling, automatically call the finalize method
  • ④It is worth noting that finalize has nothing to do with garbage collection, but its execution is actually set in the last step of garbage collection, which itself is equivalent to a member method that is automatically executed at this point in time;
  • ⑤ Generally used for data destruction operations before some objects are recycled (application scenarios)
  • @author MARECLE_YING
  • @Date January 18, 2021
    /
    public class _01Finalize { //main method public static void main(String[]args){ // instantiate the object Animal animal = new Animal(); // set the address of animal to empty Value animal = null; // The programmer recommends the garbage collector to recycle the jvm to collect garbage. // It is recommended to recycle, and there may be no recycling. System.gc(); // When there is too much garbage, no advice is needed, and the system automatically recycles // Use a for loop to create 999999 objects for(int i = 0; i<999999; i++){ new Animal(); } } } // Create an Animal class Animal{ // Create a finalize method // This place is just It is through the finalize method that shows us whether the garbage is collected.// In fact, the garbage collection has nothing to do with this method. The trigger condition of the collection is the object. If there are more objects, the system automatically recycles public void finalize(){






















    System.out.println (this + "I want to be recovered, save me _ ");
    }
    }
    Package Penalty for _16_Object_HashCode;
    /
    *

hashCode

Design purpose: to generate a unique identifier for each object
*

  • Hash: The same object generates multiple hash values, and the value must be the same;
  •  				多个对象生成多个hash值时,值可能会相同,不能确保生成的hash值不同,这种情况被称作哈希冲突;
    
  • Since there is a hash conflict, the hash value cannot guarantee the uniqueness of the data;
  • In order to ensure the uniqueness of the data, we first compare the hashes;
  • Different hashes indicate different objects;
  • The hash is the same, indicating that the objects may be the same, so we need to compare whether the objects are equal (equals)
  • What else need to be considered when overwriting hashCode? equals
  • When overriding equals, also consider hashCode;
  • So in Java, the uniqueness of the object is represented by hashCode and equals
  • Hash algorithm: a secure encryption algorithm that converts a value of variable length to a value of fixed length; the disadvantage is that there is a hash conflict;
  • @author MARECLE_YING
  • @Date January 18, 2021
    */
    public class _01HashCode { //main method public static void main (String[]args){ // instantiate an object _01HashCode hc = new _01HashCode(); // define member variables and objects Assign the hash value of hc to it int hash = hc.hashCode(); // The system System is the JVM virtual machine of java // The value of the output hash should be the hash value of hc. The memory address // decimal Memory address System.out.println(hash); // Output the memory address of hc System.out.println(hc); // Output the value of member variable hash, convert it to hexadecimal and output System.out.println( Integer.toHexString(hash)); } // Overwrite hashCode // The return value here can change the value of hashCode @Override public int hashCode() { // TODO Auto-generated method stub




















    // return super.hashCode();
    return 1999;
    }

}

toString:

  • Design purpose: to return the string representation of the object, through this method, the data of each object can be displayed
  • When outputting a reference type, the toString method of the object is automatically called;
  • @author MARECLE_YING
  • @Date January 18, 2021
    */
    public class _01toString { public static void main(String[]args){ // instantiate object s1 and assign string String s1 = new String("String"); // why not Memory address (because toString is overwritten in the String class) System.out.println(s1); // instantiate the object p1 and assign it through the parameter construction Person p1 = new Person(18,"Li Yang"); // output Is the address of p1 System.out.println(p1); // Output name: Li Yang; age: 18 // You can optimize the output by overwriting toString System.out.println("Name:"+p1.getName() +";Age:"+p1.getAge()); } } // Create a class Person{ // Use getter/setter statements to manipulate the value private int age; private String name; public int getAge() { return age;





















    }
    public void setAge(int age) { this.age = age; } public String getName() { return name; } public void setName(String name) { this.name = name; } // The construction method has parameters to receive the above The passed value public Person(int age, String name) { super(); this.age = age; this.name = name; } //Override toString to optimize the output @Override public String toString() { // TODO Auto -generated method stub //return super.toString(); return "name:"+name+"; age: "+age; }




















}

Inner class

  • Simply put, it is to create a new class in a class body;
  • ①Member inner class (non-static)
  • ②Static member inner class
  • ③Local inner class
  • ④ Anonymous inner class (formal participation actual parameter)
  • Application scenario: When a complete structure is needed to describe the internal part of a thing, an internal class can be created;
  •  			  java中 允许一个类定义在一个类的内部,前者称为内部类,后者成为外部类;
    
  •  			  外部类调用内部类是,必须给出完整的名称让系统找到它;
    
  • The characteristics of the inner class: you can access the private data of the outer class;
  • variable:
  •  成员变量:成员内部类;
    
  •  静态变量:静态内部类;
    
  •  局部变量:局部内部类;
    
  •  形参与实参变量:匿名内部类;
    

Member inner class:

  •  1.可以等同看作成员变量
    
  •  2.成员内部类中不能有静态声明
    
  •  3.成员内部类中可以直接访问外部类所有的属性
    
  • @author MARECLE_YING

  • @Date January 18, 2021
    */
    public class _01_InnerClass { // privatized member variable // static private static String s1 = "A"; // non-static private String s2 = "B";




     // 创建内部类
     // private public protected 都可以使用
     // 编译后的类名为 外部类类名$内部类类名
     // _01_InnerClass$_01_InnerClass
     	public class InnerClass{
     		//内部类不能有静态声明(静态类和静态方法)
     		//static int x;
     		// public static void m1(){}
     		public void m1(){
     			// 内部类可以直接访问外部类的所有属性
     			System.out.println(s1);
     			System.out.println(s2);
     	}
     }
     // main方法
     public static void main (String[]args){
     	//创建外部类对象
     	_01_InnerClass wb = new _01_InnerClass();
     	//通过外部类去创建内部类对象
     	InnerClass nb = wb.new InnerClass();
     	nb.m1();
     }
    

}
package _18_InnerClass;
/**

  • Static inner class

  • 1. It can be seen as a static variable
  • 2. In the static inner class, the member data cannot be directly accessed, and the object is required.
  • 3. Any variable can be declared in a static inner class
  • @author MARECLE_YING
  • @Date January 18, 2021
    */
    public class _02_InnerClass { //Create private variable // Static private static String s1 = "I am a static member variable"; // Non-static private String s2 = "I am a member Variable"; // Create a static inner class static declare a static private static class InnerClass{ // Create a m1 static method public static void m1(){ // You can directly access the static member properties System.out.println(s1); // Cannot directly access member attributes // System.out.println(s2); // Need to use object call // Create an object class in the class relative to a type object s is equivalent to its value _02_InnerClass s = new _02_InnerClass(); // Use an object to call the output System.out.println(s.s2); } // Cannot directly access member attributes public void m2(){ // Cannot directly access member attributes, regardless of whether the method is static or not;






















    // System.out.println(s2);
    System.out.println("I am an m2 member method");
    }
    }
    // Write the main method
    public static void main(String[]args){ // Full path: external Class. Inner class. Static _02_InnerClass.InnerClass.m1(); // When accessing the static properties of the current class, the class name can be omitted (for external classes) // m1 is a static method InnerClass.m1(); // Yes InnerClass instantiation object InnerClass x = new _02_InnerClass.InnerClass(); // Similarly in the current class, the static external class can be omitted InnerClass x1 = new InnerClass(); // Use the internal class object to call its own member methods x1.m2(); } }












The relationship between class and class:

  • ①Inheritance relationship/generalization relationship;
  • ②Realization relationship; multiple realizations of classes and interfaces;
  • ③Dependency; call another class in one method;
  • ④ Association relationship; member variables of one class are references to objects of another class;
  • ⑤ Aggregation relationship; each member has the ability alone, together they become the organization to do great things, and they can do things on their own without the organization;
  • ⑥ Combination relationship; parts form a combination and become a whole; for the part, it is useless to separate from the whole. If the whole dies, the part will die;
  • @author MARECLE_YING
  • @Date January 18, 2021
    */
    public class _01ClassRelation { // The association relationship A member variable of one class is a reference to another class object; // The association relationship occurs in the class body // As long as the type of the member variable is not basic data Type, then it is the relationship. // Although A is a class, its value is relative to type A. Its value is a. This object A a = new A(); public static void main (String[]args){ // Dependency Invoking another class in one method is equivalent to a local variable being a reference to another class object // dependency occurs in the method body // Invoking another class in the main method A a2 = new A(); } } //Create class class A{














}
// Inheritance relationship Child class B inherits parent class A The inheritance of class and class is single inheritance
class B extends A{

}
// Create interface
interface C{

}
interface D{

}
// There is multiple inheritance between interfaces, separated by commas
interface E extends C,D{

}
// Implement multiple implementations of relationship classes and interfaces
class F implements C,D{

}

Guess you like

Origin blog.csdn.net/MIRACLE_Ying/article/details/112797886