[Java開発の道](24)内部類

84392 ワード

Javaでは、1つのクラスを別のクラスの中または1つの方法の中に定義することができ、このようなクラスを内部クラスと呼ぶ。広範な意味での内部クラスは、一般的に、メンバーの内部クラス、局所内部クラス、匿名内部クラス、および静的内部クラスの4つを含む。まず、これらの4つの部類の使い方を調べてみます。
1.メンバー内部クラス
メンバー内部クラスは他のクラスの内部に定義されているクラスです。

  
  
  
  
  1. package com.qunar.fresh;
  2. /**
  3. * Created by xiaosi on 16-3-29.
  4. */
  5. public class Circle {
  6.    private double radius;
  7.    public Circle(double radius) {
  8.        this.radius = radius;
  9.    }
  10.    //
  11.    class Draw{
  12.        public void drawCircle(){
  13.            System.out.println("Circle---->" + radius);
  14.        }
  15.    }
  16.    public Draw getDrawInstance(){
  17.        return new Draw();
  18.    }
  19.    public static void main(String[] args) {
  20.        Circle circle = new Circle(12.5);
  21.        circle.getDrawInstance().drawCircle(); // 12.5
  22.    }
  23. }

Circle 。 ( private )。 drawCircle , radius。


(1) , , 。


  
  
  
  
  1. public class Circle {
  2.    private double radius;
  3.    public Circle(double radius) {
  4.        this.radius = radius;
  5.    }
  6.    //
  7.    class Draw {
  8.        private double radius;
  9.        public Draw(double radius) {
  10.            this.radius = radius;
  11.        }
  12.        public void drawCircle() {
  13.            // ,
  14.            System.out.println("Circle---->" + radius);
  15.        }
  16.    }
  17.    public Draw getDrawInstance(double radius) {
  18.        return new Draw(radius);
  19.    }
  20.    public static void main(String[] args) {
  21.        Circle circle = new Circle(12.5);
  22.        circle.getDrawInstance(11.6).drawCircle(); // 11.6
  23.    }
  24. }

, :

  • .this.

  • .this.


  
  
  
  
  1. public void drawCircle() {
  2.    System.out.println("Circle---->" + Circle.this.radius); // 12.5
  3. }

(2) , 。


  
  
  
  
  1. public class Circle {
  2.    private double radius;
  3.    public Circle(double radius) {
  4.        this.radius = radius;
  5.        //
  6.        new Draw().drawCircle();
  7.    }
  8.    //
  9.    class Draw {
  10.        public void drawCircle() {
  11.            System.out.println("Circle---->" + radius); // 12.5
  12.        }
  13.    }
  14.    public static void main(String[] args) {
  15.        Circle circle = new Circle(12.5); // Circle---->12.5
  16.    }
  17. }

(3) , , , 。 :


  
  
  
  
  1. package com.qunar.fresh;
  2. /**
  3. * Created by xiaosi on 16-3-29.
  4. */
  5. public class Outer {
  6.    private String name;
  7.    public Outer(String name) {
  8.        this.name = name;
  9.    }
  10.    //
  11.    class Inner {
  12.        public void print() {
  13.            System.out.println("Outer---->" + name); // Outer---->OuterClass
  14.        }
  15.    }
  16.    public static void main(String[] args) {
  17.        Outer outer = new Outer("OuterClass");
  18.        Outer.Inner inner = outer.new Inner();
  19.        inner.print();
  20.    }
  21. }

2 :


  
  
  
  
  1. package com.qunar.fresh;
  2. /**
  3. * Created by xiaosi on 16-3-29.
  4. */
  5. public class Outer {
  6.    private String name;
  7.    private Inner inner = null;
  8.    public Outer(String name) {
  9.        this.name = name;
  10.    }
  11.    public Inner getInstance() {
  12.        if (inner == null) {
  13.            return new Inner();
  14.        }
  15.        return inner;
  16.    }
  17.    //
  18.    class Inner {
  19.        public void print() {
  20.            System.out.println("Outer---->" + name); // Outer---->OuterClass
  21.        }
  22.    }
  23.    public static void main(String[] args) {
  24.        Outer outer = new Outer("OuterClass");
  25.        Outer.Inner inner = outer.getInstance();
  26.        inner.print();
  27.    }
  28. }

(4) private、protected、public 。 , Inner private , , public , ; protected , ; , 。 , public 。

2.


, public、protected、private static 。

3.

, 。 , new 。 new , : new < > < >  new , 。 , 。

, Map,key ,value , 。 , Map, value , , 。 Function 。


  
  
  
  
  1. @Test
  2.    public void test1(){
  3.        Map<String,Double> map = Maps.newHashMap();
  4.        map.put("apple",4.5);
  5.        map.put("bear",6.3);
  6.        // {bear=6.3, apple=4.5}
  7.        System.out.println(map.toString());
  8.        Map<String,Double> newMap = Maps.transformValues(map, new Function<Double, Double>() {
  9.            double rate = 0.1544;
  10.            @Override
  11.            //
  12.            public Double apply(Double input) {
  13.                return rate * input;
  14.            }
  15.        });
  16.        // {bear=0.97272, apple=0.6948000000000001}
  17.        System.out.println(newMap.toString());
  18.    }

new Function<Double,Double>(){...} 。

( )。 , , 。 Outter$1.class。 , , , 。

, , 。 。

4.

static , , static , , , static , static 。


   
   
   
   
  1. public class Outer2 {
  2.    private static String sname = "Outer2";
  3.    private String name;
  4.    public Outer2(String name) {
  5.        this.name = name;
  6.    }
  7.    //
  8.    public static class Inner {
  9.        public void print() {
  10.            // static
  11.            // System.out.println("Outer---->" + name);
  12.            //
  13.            System.out.println("Outer---->" + sname);
  14.        }
  15.    }
  16. }


   
   
   
   
  1. Outer2.Inner inner2 = new Outer2.Inner();
  2. inner2.print();