Inner class and Object root class
Article directory
Inner class
Inner class simply use the inner class is the inner class.
Location: define one class into another, then the internal class is the internal class.
Note: internal classes cannot be created directly
Syntax for creating inner classes:
External class. Internal class variable name = new external class object. New internal class object
Methods of external classes of internal classes if you want to access methods of internal classes, you must create objects of internal classes to access according to the objects of internal classes.
General internal class
class Outter{ int num; public void outMethod(){ System.out.println("I'm an external class method"); } class Inner{ int innerNum; public void inMethod(){ System.out.println("I'm an internal class method"); } } } public class TestInner{ public static void main(String[] args){ //Create object external class of internal class. Internal class variable name = new external class object. New internal class object Outter.Inner inner = new Outter().new Inner(); inner.innerNum = 13; inner.inMethod();//I'm an internal class method } }
Compiled class of inner class
Characteristics of inner class
-
This class can directly create objects of internal classes
-
Methods of inner classes can access properties and methods of outer classes
Anonymous Inner Class
What is an anonymous class: a class without a name, which needs to be implemented on the interface.
Both anonymous class and anonymous inner class need the support of interface or abstract class.
Create an anonymous class object. The object of this class implements the interface of outerinterand implements the methods in the interface in curly braces. After the method is called, it will be garbage collected
- Anonymous inner class in interface
interface Outter{ public void outMethod(); } class OutterImpl implements Outter { public void outMethod(){ } } public class TestInner{ public static void main(String[] args){ Outter outter = new OutterImpl(); outter.outMethod(); //It is not the interface itself that is created. The curly bracket after new outer () is an anonymous inner class, // Implement the outer interface Outter o = new Outter() { @Override public void outMethod() { System.out.println("I'm an anonymous inner class method"); } }; o.outMethod(); } //I'm an anonymous inner class method }
-
Anonymous inner class in abstract class
abstract class Outter{ public abstract void outMethod(); } public class TestInner{ public static void main(String[] args){ //It is not the interface itself that is created. The curly bracket after new outer () is an anonymous inner class, // Implement the outer interface Outter o = new Outter() { @Override public void outMethod() { System.out.println("I'm an anonymous inner class method"); } }; o.outMethod(); } //I'm an anonymous inner class method }
Object
Object overview
-
Object is the root class of all classes. All classes directly or indirectly inherit object classes.
-
Class Object is the root class of class hierarchy. Each class uses Object as its superclass. All objects (including arrays) implement methods of this class.
-
When defining a class, you do not use the extends keyword, that is, you do not explicitly inherit a class, so this class directly inherits the Object class. All objects inherit methods of this class.
Basic method
- toString() method
Returns information about the current object itself, and returns a string object. Typically, the toString method returns a string that "represents" this object as text. The result should be a concise but easy to read information expression. It is recommended that all subclasses override this method.
Implementation of toString in Object
The toString method of the Object class returns a string consisting of the class name (the Object is an instance of the class), the at flag "@" and an unsigned hexadecimal representation of the hash code of the Object. In other words, the method returns a string whose value is equal to: get the name of the class "@" getClass().getName() + '@' + Integer.toHexString(hashCode())
public class Test{ public static void main(String[] args) { Object o = new Object(); System.out.println(o.toString()); } }//java.lang.Object@1540e19d
public class Test{ private String name = "Zhang San"; private int age = 10; @Override public String toString() { return "name='" + name + '\'' + ", age=" + age ; } public static void main(String[] args) { Test t=new Test(); System.out.println(t.toString()); } }//name = Zhang San, age=10
- getClass()
The return value of getClass() is Class (the Class object of the Class), which is related to reflection. Get the Class name of the object through getClass().getName().
- hashCode()
The hashCode method defined by the Object class does return different integers for different objects.
- finalize()
For garbage collection, we don't need to call it manually. It's called by the jvm,
This method is called by the object's garbage collector when the garbage collector determines that there are no more references to the object.
-
equals(Object obj), return value is boolean type
equals is used to compare whether two objects are equal
public class Test { public static void main(String[] args) { //1. Create an Object of type Object Object obj1 = new Object(); Object obj2 = new Object(); //2. Test the member methods in the Object class //2.1 hashCode: hash code values of different objects are generally different int code1 = obj1.hashCode(); int code2 = obj2.hashCode(); System.out.println(code1);//356573597 System.out.println(code2);//1735600054 //2.2 class <? > getclass(): a class has only one bytecode object. Class cls1 = obj1.getClass(); Class cls2 = obj2.getClass(); System.out.println(cls1);//class java.lang.Object System.out.println(cls2);//class java.lang.Object //2.3 String toString(): returns the string form of the object. By default, it returns the address value. Different objects have different return values //Composition of address value: full class name @ the unsigned hexadecimal form of hash code of the object String s1 = obj1.toString(); String s2 = obj2.toString(); System.out.println(s1);//java.lang.Object@1540e19d System.out.println(s2);//java.lang.Object@677327b6 //2.4 boolean equals(): compare whether two objects are equal. By default, compare address values (meaningless). Subclasses will generally override. boolean b = obj1.equals(obj2); System.out.println(b);//false } }