Class name as parameter and return value (a)

1. ## Parameter passing

### 1.1 class name as a parameter and return value (application)

* 1, the class name as a parameter of the method

  parameter is the class name of the method, object of that class is actually needed to

  actually pass the object address is a value []

* 2, the class name as the return value of

  the return value is the class name, the class is actually returned object

  actually transmitted, but also the address value [] of the object

* sample code:

java
  class Cat {
      public void eat() {
          System.out.println("猫吃鱼");
      }
  }
  class CatOperator {
      public void useCat(Cat c) { //Cat c = new Cat();
          c.eat();
      }
      public Cat getCat() {
          Cat c = new Cat();
          return c;
      }
  }
  public class CatDemo {
      public static void main(String[] args) {
          //创建操作类对象,并调用方法
          CatOperator co = new CatOperator();
          Cat c = new Cat();
          co.useCat(c);
  
          Cat c2 = co.getCat(); //new Cat()
          c2.eat();
      }
  }

1.2 abstract class ### as a parameter and return value (understood)

* abstract class as a parameter and return value

  * parameter method is an abstract class name actually needed is an object of the subclass of abstract class is
  the return value of the method * It is an abstract class name, in fact, the return of the object is a subclass of the abstract class

* sample code:

java
  abstract class Animal {
      public abstract void eat();
  }
  class Cat extends Animal {
      @Override
      public void eat() {
          System.out.println("猫吃鱼");
      }
  }
  class AnimalOperator {
      public void useAnimal(Animal a) { //Animal a = new Cat();
          a.eat();
      }
      public Animal getAnimal() {
          Animal a = new Cat();
          return a;
      }
  }
  public class AnimalDemo {
      public static void main(String[] args) {
          //创建操作类对象,并调用方法
          AnimalOperator ao = new AnimalOperator();
          Animal a = new Cat();
          ao.useAnimal(a);
  
          Animal a2 = ao.getAnimal(); //new Cat()
          a2.eat();
      }
  }

1.3 interface name as parameter and return value (understood)

* interface as the parameter and return value

  * parameter method is the interface name, in fact, required to achieve the class object for the interface
  returns a value * method is the interface name, in fact, returns class object is to achieve the interface

* sample code:

java
  interface Jumpping {
      void jump();
  }
  class JumppingOperator {
      public void useJumpping(Jumpping j) { //Jumpping j = new Cat();
          j.jump();
      }
      public Jumpping getJumpping() {
          Jumpping j = new Cat();
          return j;
      }
  }
  class Cat implements Jumpping {
      @Override
      public void jump() {
          System.out.println("猫可以跳高了");
      }
  }
  public class JumppingDemo {
      public static void main(String[] args) {
          //创建操作类对象,并调用方法
          JumppingOperator jo = new JumppingOperator();
          Jumpping j = new Cat();
          jo.useJumpping(j);
  
          Jumpping j2 = jo.getJumpping(); //new Cat()
          j2.jump();
      }
  }
  

2. Internal class ##

substantially ### 2.1 Internal class used (understood)

* internal generic concept

  * define a class in a class. For example: define a class B within a class A, class B will be referred to as inner classes

* Internal class definition format

  * & Format Example:

`java
    /*
            格式:
        class 外部类名{
                修饰符 class 内部类名{
                
                }
        }
    */
    
    class Outer {
        public class Inner {
            
        }
    }

* Access features within the class 

  * inner class can directly access the external members of the class, including private
  members of the outer class * To access the inner class, you must create an object

* Sample code:

java
  /*
      内部类访问特点:
          内部类可以直接访问外部类的成员,包括私有
          外部类要访问内部类的成员,必须创建对象
   */
  public class Outer {
      private int num = 10;
      public class Inner {
          public void show() {
              System.out.println(num);
          }
      }
      public void method() {
          Inner i = new Inner();
          i.show();
      }
  }

2.2 ### members of the inner class (understanding)

* members of the inner class defined position

  * in a class method, with the member variable is a location

* outside a founding member of the inner class format

  * Format: External class name inside the class name = external object name the class inside the object class object;
  * For example:. Outer.Inner OI = new new Outer () new new Inner ();

* within the class members recommended program

  * a class, designed for the purpose of internal class, mostly do not want to access to the outside world, so the definition of the inner class should be privatized,
  privatization, and then provide a method that allows external calls, internal methods to create inner class object and call.

* Sample code:

java
  class Outer {
      private int num = 10;
      private class Inner {
          public void show() {
              System.out.println(num);
          }
      }
      public void method() {
          Inner i = new Inner();
          i.show();
      }
  }
  public class InnerDemo {
      public static void main(String[] args) {
                  //Outer.Inner oi = new Outer().new Inner();
                  //oi.show();
          Outer o = new Outer();
          o.method();
      }
  }

### 2.3 partial inner class (understood)

* local position inside the class definition

  * partial inner class is defined in the class method

* partial inner class manner

  * partial inner class, the outside is not directly used to create objects within the required method and using
  * class can directly access the external members of the class may access local variables in a method

* sample Code

java
  class Outer {
      private int num = 10;
      public void method() {
          int num2 = 20;
          class Inner {
              public void show() {
                  System.out.println(num);
                  System.out.println(num2);
              }
          }
          Inner i = new Inner();
          i.show();
      }
  }
  public class OuterDemo {
      public static void main(String[] args) {
          Outer o = new Outer();
          o.method();
      }
  }

### 2.4 anonymous inner classes (applications)

* anonymous inner classes premise

  * presence of a class or interface, where the class may be a particular class may be abstract

format anonymous inner classes *

  * format: new class name () { The method of rewriting new new interface name} () {} override method

  * example: 

java
    new Inter(){
        @Override
        public void method(){}
    } 

 

Published 157 original articles · won praise 43 · views 90000 +

Guess you like

Origin blog.csdn.net/qq_39581763/article/details/103919071