Object oriented -- inner class

Posted by morris on Fri, 19 Nov 2021 06:51:48 +0100

1. Formal parameters and return values

1. Class name as formal parameter and return value

  • The formal parameter of the method is the class name. In fact, what is needed is the object of the class
  • The return value of the method is the class name. In fact, it returns the object of the class
//Cat class
package internal.study01;

public class Cat {
    public void eat(){
        System.out.println("Cats eat fish");
    }
}

//CatOperator class
package internal.study01;

public class CatOperator {
    public void useCat(Cat c){
        c.eat();
    }

    public Cat cat(){
        Cat cat = new Cat();
        return cat;
    }
}

//Test class
package internal.study01;

public class CatDemo {
    public static void main(String[] args) {
        CatOperator catOperator = new CatOperator();
        Cat cat = new Cat();
        catOperator.useCat(cat);

        Cat c2 = catOperator.cat();
        c2.eat();
    }
}

2. Abstract class name as formal parameter and return value

  • The formal parameter of the method is the name of the abstract class. In fact, what is needed is the subclass object of the abstract class
  • The return value of the method is the abstract class name. In fact, it returns the subclass object of the abstract type
  • Note: abstract classes need to use polymorphic forms when creating objects.
//
package internal.study02;

public abstract class Animal {
    public abstract void eat();
}

//
package internal.study02;

public class Cat extends Animal{
    @Override
    public void eat() {
        System.out.println("Cats eat fish");
    }
}

//
package internal.study02;

public class AnimalOperator {
    public void useAnimal(Animal animal){
        animal.eat();
    }
    public Animal getAnimal(){
        Animal a = new Cat();
        return a;
    }
}

//
package internal.study02;

public class AnimalDemo {
    public static void main(String[] args) {
        AnimalOperator animalOperator = new AnimalOperator();
        Animal a = new Cat();
        animalOperator.useAnimal(a);

        Animal a2 = animalOperator.getAnimal();
        a2.eat();
    }
}

3. Interface name as formal parameter and return value

  • The formal parameter of the method is the interface name. In fact, what is needed is the implementation class object of the interface
  • The return value of the method is the interface name. In fact, it returns the implementation class object of the interface
//Jumpping interface
package internal.study03;

public interface Jumpping {
    void jump();
}

//Cat class
package internal.study03;

public class Cat implements Jumpping{
    @Override
    public void jump() {
        System.out.println("Cat jump");
    }
}

//JumpOperator operation class
package internal.study03;

public class JumpOperator {
    public void useJumpping(Jumpping j){
        j.jump();
    }
    public Jumpping getJumpping(){
        Jumpping j = new Cat();
        return j;
    }
}

//JumpDemo test class
package internal.study03;

public class JumpDemo {
    public static void main(String[] args) {
        JumpOperator jo = new JumpOperator();
        Jumpping c = new Cat();
        jo.useJumpping(c);

        Jumpping j2 = jo.getJumpping();
        j2.jump();
    }
}


2. Internal class

1. Internal class overview

  1. Inner class: in fact, it is to define a class in a class. For example, if a class B is defined inside a Class A, class B is called an inner class.

  2. Definition format of internal class

public cass Class name{
  Modifier  class Class name{
    
  }
}
  1. example:

    public class Outer{
      public class Tnner{
        
      }
    }
    
  2. Access characteristics of internal classes

  • Internal classes can directly access members of external classes, including private classes.

  • An object must be created for an external class to access members of an internal class.

    package internal.study04;
    
    public class Outer {
        private int sum;
        //Inner class
        public class inner{
            public void show(){
                sum =9;//Internal classes can directly access members of external classes, including private classes.
                System.out.println("sum:"+sum);
            }
        }
    
    //    An object must be created for an external class to access members of an internal class.
        public void methon(){
            inner i = new inner();
            i.show();
        }
    }
    
    

2. Member internal class

  1. Classification of internal classes

According to the different positions of internal classes defined in the class, they can be divided into the following two forms

  • In the member position of the class: the inner class of the member
  • Local position in class: local inner class

Internal class of member, how to create objects outside:

  • Format: external class name. Internal class name object name = external class object. Internal class object
//Inner class
package internal.study05;

public class Outer {
    private int sum = 10;
    /*public class inner{
        public void show(){
            System.out.println(sum);
        }
    }
      Not commonly used
     */

    private class inner{
        public void show(){
            System.out.println(sum);
        }
    }
    public void methon(){
        inner i = new inner();
        i.show();
    }
}

//Test class
package internal.study05;
/*Test class*/
public class Demo {
    public static void main(String[] args) {
        //Create an internal class and call a method
      /*  Outer.inner oi = new Outer().new inner();
        oi.show();
        Internal classes exposed for
        */
        Outer o = new Outer();
        o.methon();
    }
}

3. Local internal class

The local internal class is a class defined in the method, so the external class cannot be used directly. You need to create an object inside the method and use it. This class can directly access the external class or the internal variables in the method.

//Inner class
package internal.study06;

public class Outer {
    private int sum = 10;
    public void methon(){
        int sum1 = 102;
        class inner{
            public void show(){
                System.out.println("sum:"+sum);
                System.out.println("sum:"+sum1);
            }
        }
        inner i = new inner();
        i.show();
    }
}

//Test class
package internal.study06;

public class Demo {
    public static void main(String[] args) {
        Outer o = new Outer();
        o.methon();
    }
}

4. Anonymous inner class

  1. Premise: there is a class or interface. The class here can be either a concrete class or an abstract class.

  2. format

new Class name or interface name(){
  override method ;
};
  • example
new Inter(){
  public void show(){
    
  }
}
  1. Essence: it is an anonymous object that inherits the class or implements the subclass of the interface.

  2. Details of anonymous inner classes

  • Anonymous inner classes can be accepted in the form of polymorphism
Inter i = new Inter(){
  @Override
    public void method(){
        
    }
}
  • Anonymous inner classes call methods directly
interface Inter{
    void method();
}

class Test{
    public static void main(String[] args){
        new Inter(){
            @Override
            public void method(){
                System.out.println("I am an anonymous inner class");
            }
        }.method();	// Direct call method
    }
}

5. Use of anonymous inner classes in development

  1. Use of anonymous inner classes in development
  • When a method needs a subclass object of an interface or abstract class, we can pass an anonymous inner class to simplify the traditional code
  • Example code:
interface Jumpping {
    void jump();
}
class Cat implements Jumpping {
    @Override
    public void jump() {
        System.out.println("The cat can jump high");
    }
}
class Dog implements Jumpping {
    @Override
    public void jump() {
        System.out.println("The dog can jump high");
    }
}
class JumppingOperator {
    public void method(Jumpping j) { //new Cat();   new Dog();
        j.jump();
    }
}
class JumppingDemo {
    public static void main(String[] args) {
        //Requirements: create the object of the interface operation class and call the method method method
        JumppingOperator jo = new JumppingOperator();
        Jumpping j = new Cat();
        jo.method(j);

        Jumpping j2 = new Dog();
        jo.method(j2);
        System.out.println("--------");

        // Simplification of anonymous inner classes
        jo.method(new Jumpping() {
            @Override
            public void jump() {
                System.out.println("The cat can jump high");
            }
        });
		// Simplification of anonymous inner classes
        jo.method(new Jumpping() {
            @Override
            public void jump() {
                System.out.println("The dog can jump high");
            }
        });
    }
}

Topics: Java