Java learning note 15 (object-oriented 8: anonymous object, inner class)

Anonymous object:

When an object is created, only the statement that creates the object does not assign the address value of the object to a variable

Create a normal object:

Person p = new Person();

Create an anonymous object:

new Person();

be careful:

1. Anonymous objects can only be used once

2. Anonymous objects can be passed as parameters

3. Anonymous objects can be used as return values of methods

Example:

public class Demo {
    public static Person getPerson(){
        //Common way
        //Person p = new Person();    
        //return p;
        
        //Anonymous object as method return value
        return new Person(); 
    }
    
    public static void method(Person p){}
}

public class Test {
    public static void main(String[] args) {
        //call getPerson Method, get a Person object
        Person person = Demo.getPerson();
        
        //call method Method
        Demo.method(person);
        //Anonymous object as parameter received by method
        Demo.method(new Person());
    }
}

 

 

Internal class:

Defining a class within a class

When do you need it? For example, a car, including an engine, can be described by an internal class

Internal class classification: member internal class, local internal class

Example of creating and calling a class within a member:

public class Outer {
    private int a = 1;
    //Internal classes can use external class members, including private
    //External classes cannot directly use internal class variables. Internal class objects must be created
    class Inner{
        public void inner(){
            System.out.println("Inner class method"+a);
        }
    }
}
public class Test {
    public static void main(String[] args) {
        //Internal class call format:
        Outer.Inner in = new Outer().new Inner();
        in.inner();
    }
}

 

Call problem of variable with the same name:

public class Outer {
    int i = 1;
    class Inner{
        int  i =2;
        public void inner(){
            int  i = 3;
            System.out.println(i);//3
            System.out.println(this.i);//2
            System.out.println(Outer.this.i);//1
        }
    }
}

 

Local inner class:

Write a class in a method

The calling method is relatively complex

Example:

public class Outer {
    public void out(){
        class Inner{
            public void inner(){
                System.out.println("Local inner class method");
            }
        }
        Inner inner = new Inner();
        inner.inner();
    }
}
public class Test {
    public static void main(String[] args) {
        new Outer().out();
    }
}

 

 

Actual use of internal classes:

Anonymous inner class:

Temporarily define a subclass of a specified type, and immediately create the subclass object just defined

Example:

public interface Smoking {
    public abstract void smoking();
}
public class Test {
    public static void main(String[] args) {
        //Fixed format
        //In fact, the implementation class of the interface is created and the method is rewritten
        //It can also be written here Smoking s = new Smoking() {};
        //Then? s.Method call
        new Smoking() {
            public void smoking() {
                System.out.println("People are smoking");
            }
        }.smoking();
        //Pay attention to the semicolon
    }
}

Keywords: Java

Added by intodesi on Tue, 05 May 2020 16:20:12 +0300