[Java開発の道](24)内部類
84392 ワード
Javaでは、1つのクラスを別のクラスの中または1つの方法の中に定義することができ、このようなクラスを内部クラスと呼ぶ。広範な意味での内部クラスは、一般的に、メンバーの内部クラス、局所内部クラス、匿名内部クラス、および静的内部クラスの4つを含む。まず、これらの4つの部類の使い方を調べてみます。
1.メンバー内部クラス
メンバー内部クラスは他のクラスの内部に定義されているクラスです。
1.メンバー内部クラス
メンバー内部クラスは他のクラスの内部に定義されているクラスです。
package com.qunar.fresh;
/**
* Created by xiaosi on 16-3-29.
*/
public class Circle {
private double radius;
public Circle(double radius) {
this.radius = radius;
}
//
class Draw{
public void drawCircle(){
System.out.println("Circle---->" + radius);
}
}
public Draw getDrawInstance(){
return new Draw();
}
public static void main(String[] args) {
Circle circle = new Circle(12.5);
circle.getDrawInstance().drawCircle(); // 12.5
}
}
Circle 。 ( private )。 drawCircle , radius。
:
(1) , , 。
public class Circle {
private double radius;
public Circle(double radius) {
this.radius = radius;
}
//
class Draw {
private double radius;
public Draw(double radius) {
this.radius = radius;
}
public void drawCircle() {
// ,
System.out.println("Circle---->" + radius);
}
}
public Draw getDrawInstance(double radius) {
return new Draw(radius);
}
public static void main(String[] args) {
Circle circle = new Circle(12.5);
circle.getDrawInstance(11.6).drawCircle(); // 11.6
}
}
, :
-
.this.
-
.this.
public void drawCircle() {
System.out.println("Circle---->" + Circle.this.radius); // 12.5
}
(2) , 。 , , :
public class Circle {
private double radius;
public Circle(double radius) {
this.radius = radius;
//
new Draw().drawCircle();
}
//
class Draw {
public void drawCircle() {
System.out.println("Circle---->" + radius); // 12.5
}
}
public static void main(String[] args) {
Circle circle = new Circle(12.5); // Circle---->12.5
}
}
(3) , , , 。 :
package com.qunar.fresh;
/**
* Created by xiaosi on 16-3-29.
*/
public class Outer {
private String name;
public Outer(String name) {
this.name = name;
}
//
class Inner {
public void print() {
System.out.println("Outer---->" + name); // Outer---->OuterClass
}
}
public static void main(String[] args) {
Outer outer = new Outer("OuterClass");
Outer.Inner inner = outer.new Inner();
inner.print();
}
}
2 :
package com.qunar.fresh;
/**
* Created by xiaosi on 16-3-29.
*/
public class Outer {
private String name;
private Inner inner = null;
public Outer(String name) {
this.name = name;
}
public Inner getInstance() {
if (inner == null) {
return new Inner();
}
return inner;
}
//
class Inner {
public void print() {
System.out.println("Outer---->" + name); // Outer---->OuterClass
}
}
public static void main(String[] args) {
Outer outer = new Outer("OuterClass");
Outer.Inner inner = outer.getInstance();
inner.print();
}
}
(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 。
@Test
public void test1(){
Map<String,Double> map = Maps.newHashMap();
map.put("apple",4.5);
map.put("bear",6.3);
// {bear=6.3, apple=4.5}
System.out.println(map.toString());
Map<String,Double> newMap = Maps.transformValues(map, new Function<Double, Double>() {
double rate = 0.1544;
@Override
//
public Double apply(Double input) {
return rate * input;
}
});
// {bear=0.97272, apple=0.6948000000000001}
System.out.println(newMap.toString());
}
new Function<Double,Double>(){...} 。
( )。 , , 。 Outter$1.class。 , , , 。
, , 。 。
4.
, static。 , , static , , , static , static 。
public class Outer2 {
private static String sname = "Outer2";
private String name;
public Outer2(String name) {
this.name = name;
}
//
public static class Inner {
public void print() {
// static
// System.out.println("Outer---->" + name);
//
System.out.println("Outer---->" + sname);
}
}
}
:
Outer2.Inner inner2 = new Outer2.Inner();
inner2.print();
・