Java classes and anonymous inner classes

Hello everyone, in this article, we mainly introduce to you the inheritance knowledge of Object class, anonymous inner class, and construction method in Java foundation, let's learn together!

One, Object class

The Object class is the parent class of all classes, and all classes inherit the Object class.

Object class method:

toString() method: This method is used more, it returns the string of the object, generally subclasses will cover;
getClass() method: returns the type of Object running;
equals() method: judges whether the content of the object is equal;
finalize() method: This method is mainly used to release resources. This method is rarely used, and it is impossible to determine when to call it.
hashCode() method: return the hash value of this object;
notify() method: this method is to wake up a certain thread waiting on this object;
notifyAll() method: this method is to wake up all threads waiting on this object;
wait() method: The current thread is waiting for the lock of this object, which is the lock of this object. The wait() method means to wait until it gets the lock or is interrupted. wait(long timeout) sets a timeout interval and returns if the lock is not acquired within the specified time.
clone() method: protection method to realize shallow copy of objects. This method can be called only when the Cloneable interface is implemented, otherwise an exception will be thrown.

Object class example

E.g:

//Define the fruit category 
class Fruit{ 
double weight=10;//Define the weight attribute as weight 
//Define info() method 
void info(){ 
        System.out.println("I am a fruit, the weight is: "+weight+"g!"); 
  } 
} 
public class p32 { 
public static void main(String[] args) { 
        // TODO Auto-generated method stub 
        Fruit f=new Fruit();//Create a Fruit class object 
        System.out.println(f.toString());//Call the toString method 
  } 
} 

The result of the operation is:

Java class and anonymous inner classJava class and anonymous inner class

From the above code, the toString() method of the Fruit object is called in the output statement. This Fruit class inherits the Object class and defines the toString() method in Object to output the information of this object.

Example of overriding the toString() method of the Object class

//Define the fruit category 
class Fruit{ 
    double weight=10;//Define the weight attribute as weight 
    //Rewrite the toString() method of the Object class 
    public String toString(){ 
        return "I am a fruit, the weight is: "+weight+"g!"; 
  } 
} 
public class p32 { 
    public static void main(String[] args) { 
        // TODO Auto-generated method stub 
        Fruit f=new Fruit();//Create a Fruit class object 
        System.out.println(f.toString());//Call the toString method 
  } 
} 

The result of the operation is:

Java class and anonymous inner classJava class and anonymous inner class

From the above code, overwrite the toString() method of the Object class in the defined fruit class. The Fruit class inherits the Object class and outputs the information of the object.

Two, anonymous inner class

What is an anonymous inner class

An anonymous inner class simply means that you don't know the name of this class. To create an anonymous inner class, you only need to call it once.

Syntax of anonymous inner classes

new class name (parameter) | parent interface () {  
// The class body part of the anonymous inner class  
} 

How to implement an anonymous inner class example

//Define fruit interface 
interface Fruit{ 
    double weight=30;//Define the weight attribute as weight 
    void info();//define abstract method 
} 
public class p33 { 
    public static void main(String[] args) { 
        // TODO Auto-generated method stub 
        //Define an anonymous inner class as a parameter and pass it to the fruitInfo() method 
        fruitInfo(new Fruit(){ 
            //Implement the info() method 
            public void info(){ 
                System.out.println("I am a fruit, the weight is "+weight+"g!"); 
            } 
        }); 
} 
    //Define the static method fruitInfo() 
    public static void fruitInfo(Fruit f){ 
        f.info();//Call the info() method of object f 
  } 
} 

The result of the operation is:

Java class and anonymous inner classJava class and anonymous inner class

From the above code, the first step is to define the fruit class interface, define the weight attribute and the info() abstract method. When calling the fruitInfo method, writing the parameters of the method in new Fruit(){} is equivalent to instantiating the object and passing the object to the fruitInfo method. Inside the braces are the subclass anonymous.

Three, the inheritance of the construction method

Characteristics of the inheritance of construction methods

① The subclass is a parameterless construction method that can unconditionally inherit the parent class;

② If the subclass itself does not have a construction method, it will inherit the parameterless construction method of the parent class as its own construction method; if the subclass defines its own construction method, when creating a new object, it will first execute the parameterless construction method inherited from the parent class Construction method, and then execute your own construction method.

③ For the parameter-containing construction method of the parent class, the subclass uses the "super" keyword in its own construction method to call it. The call statement needs to be the first executable statement of the subclass construction method.

Inheritance example of construction method

//Define the parent class 
class Father{ 
    public Father(){ 
        System.out.println("父类..."); 
} 
    public Father(String name){ 
        System.out.println("The parent class name is:" + name); 
} 
} 
//Define the subclass to inherit the parent class 
class Son extends Father{ 
    public Sound () { 
        super("Tom"); 
        System.out.println("子类..."); 
  } 
} 
 
public class p39 { 
    public static void main(String[] args) { 
        // TODO Auto-generated method stub 
        Son son = new Son();//Create an instance of Son 
  } 
}

The result of the operation is:

Java class and anonymous inner classJava class and anonymous inner class

Four, summary

This article mainly introduces the inheritance of Object class, anonymous inner class and construction method.

The Object class is the parent class of all classes, and all classes inherit the Object class. Introduced some methods of the Object class, through the Object class example and overriding the Object class toString() method example to help understand the usage.

An anonymous inner class simply means that you don't know the name of this class. To create an anonymous inner class, you only need to call it once.

The inheritance of the construction method introduces its characteristics, through his example to understand the inheritance of the construction method.

Guess you like

Origin blog.csdn.net/yaxuan88521/article/details/113914618