- public interface Contents {
- int value();
- }
- public interface Destination {
- String readLabel();
- }
- public class Goods {
- private class Content implements Contents {
- private int i = 11;
- public int value() {
- return i;
- }
- }
- protected class GDestination implements Destination {
- private String label;
- private GDestination(String whereTo) {
- label = whereTo;
- }
- public String readLabel() {
- return label;
- }
- }
- public Destination dest(String s) {
- return new GDestination(s);
- }
- public Contents cont() {
- return new Content();
- }
- }
- class TestGoods {
- public static void main(String[] args) {
- Goods p = new Goods();
- Contents c = p.cont();
- Destination d = p.dest("Beijing");
- }
- }
public interface Contents { int value(); } public interface Destination { String readLabel(); } public class Goods { private class Content implements Contents { private int i = 11; public int value() { return i; } } protected class GDestination implements Destination { private String label; private GDestination(String whereTo) { label = whereTo; } public String readLabel() { return label; } } public Destination dest(String s) { return new GDestination(s); } public Contents cont() { return new Content(); } } class TestGoods { public static void main(String[] args) { Goods p = new Goods(); Contents c = p.cont(); Destination d = p.dest("Beijing"); } }
- public class Goods {
- private int valueRate = 2;
- private class Content implements Contents {
- private int i = 11 * valueRate;
- public int value() {
- return i;
- }
- }
- protected class GDestination implements Destination {
- private String label;
- private GDestination(String whereTo) {
- label = whereTo;
- }
- public String readLabel() {
- return label;
- }
- }
- public Destination dest(String s) {
- return new GDestination(s);
- }
- public Contents cont() {
- return new Content();
- }
- }
public class Goods { private int valueRate = 2; private class Content implements Contents { private int i = 11 * valueRate; public int value() { return i; } } protected class GDestination implements Destination { private String label; private GDestination(String whereTo) { label = whereTo; } public String readLabel() { return label; } } public Destination dest(String s) { return new GDestination(s); } public Contents cont() { return new Content(); } }
- public class Goods1 {
- public Destination dest(String s) {
- class GDestination implements Destination {
- private String label;
- private GDestination(String whereTo) {
- label = whereTo;
- }
- public String readLabel() {
- return label;
- }
- }
- return new GDestination(s);
- }
- public static void main(String[] args) {
- Goods1 g = new Goods1();
- Destination d = g.dest("Beijing");
- }
- }
public class Goods1 { public Destination dest(String s) { class GDestination implements Destination { private String label; private GDestination(String whereTo) { label = whereTo; } public String readLabel() { return label; } } return new GDestination(s); } public static void main(String[] args) { Goods1 g = new Goods1(); Destination d = g.dest("Beijing"); } }
- public class Goods2 {
- private void internalTracking(boolean b) {
- if (b) {
- class TrackingSlip {
- private String id;
- TrackingSlip(String s) {
- id = s;
- }
- String getSlip() {
- return id;
- }
- }
- TrackingSlip ts = new TrackingSlip("slip");
- String s = ts.getSlip();
- }
- }
- public void track() {
- internalTracking(true);
- }
- public static void main(String[] args) {
- Goods2 g = new Goods2();
- g.track();
- }
- }
public class Goods2 { private void internalTracking(boolean b) { if (b) { class TrackingSlip { private String id; TrackingSlip(String s) { id = s; } String getSlip() { return id; } } TrackingSlip ts = new TrackingSlip("slip"); String s = ts.getSlip(); } } public void track() { internalTracking(true); } public static void main(String[] args) { Goods2 g = new Goods2(); g.track(); } }
- public class Goods3 {
- public Contents cont() {
- return new Contents() {
- private int i = 11;
- public int value() {
- return i;
- }
- };
- }
- }
- frame.addWindowListener(new WindowAdapter(){
- public void windowClosing(WindowEvent e){
- System.exit(0);
- }
- });
在这里内部类Inner中关键字this指向内部类Inner的对象,如果要想指向外部类的对象,必须在this指针前加上外部类名称,表示this是指向外部类构造的碎屑,如Outer.this 。
在利用new构造方法构造一个外部类对象时,并没有连带着构造一个内部类对象,故需要访问内部类方法时,必须使用new操作符为这个外部类对象再构造一个内部类对象。如 Outer outer = new Outer();
Outer.Inner inner = outer.new Inner(); // 注意此处变化
规则篇:
1、一个内部类的对象能够访问创建它的外部类对象的所有属性及方法(包括私有部分)。
//可以闭上眼镜,把这个内部类等同于一个类的一个方法,当然就可以访问这个外部类的
//所有方法和属性,私有方法和属性是属于外部类的,当然也就等同于内部类的.
//Inner Class 可以访问容器类的任何成员,不管是public、private或protected的成员全是透明的。反之则不然,容器//类只能访问Inner Class的public成员
2、对于同一个包中的其它类来说,内部类能够隐藏起来。(将内部类用private修饰即可)
3、内部类可定义在方法中,称为局部内部类,但它只能使用方法中的final常量。
// 定义在一个方法内的类,又被成为局部内部类,这个类只能使用在方法中的final常量,注意,这个常量是在一个方法中的,那么能否使用一个类中的常量呢?
当然是可以的,因为类中的常量在在一个方法中是可见的.
4、内部类可以被定义为抽象类
// abstract 类同样可以在内部类中
5、非静态内部类不能声明本类的static成员
//只有一个静态的内部类,才可以声明一个static成员,
匿名内部类
在某些情况下,我们只需要内部类的一个对象,那么我们就没有必要给内部类命名,没有名字的内部类我们称为匿名内部类
以下情况使用匿名内部类可以达到很好的效果:
·只用到类的一个实例。
·类在定义后马上用到。
·类非常小(SUN 推荐是在 4 行代码以下)
·给类命名并不会导致你的代码更容易被理解。
在使用匿名内部类时,要记住以下 4 个原则:
·匿名内部类不能有构造方法。
·匿名内部类不能定义任何静态成员、方法和类。
·匿名内部类不能是 public,protected,private,static。
·只能创建匿名内部类的一个实例。