/**
* Copyright 2013, www.brook.net. All rights reserved.
*/
package com.brook.thread.service;
/**
* Inner type study.
* @author Brook Lieu
* @version v1.0
*/
public class InnerClass {
private Long myMoney = 20000L;
public class Inner {
//private static int age = 33; 非静态内部类中不能有任何静态的方法和变量,此外,也不能用静态的内部类。
public class InnerInnerClass {
}
//public static class InnerInnerStaticClass {}
public long getMoney() {
InnerClass myInner = new InnerClass();
Long myMoney = 2L;
sayHello();
//return myMoney;
System.out.println(InnerClass.this);
return InnerClass.this.myMoney;
//return myInner.myMoney;
}
}
public int age =322;
public static class StaticClass {
public void getAge() {
//System.out.println("age is " + getAge());
//System.out.println("age is " + InnerClass.this.age);
System.out.println("age is " + new InnerClass().age);
}
}
public void sayHello() {
System.out.println("Hello world. I love the world");
}
public static void main(String[] args) {
InnerClass innerClass = new InnerClass();
/*InnerClass.Inner inner = innerClass.new Inner();
Long money = inner.getMoney();
System.out.println("money is " + money);*/
//new InnerClass.StaticClass().getAge();
// 实现多重继承
C c = new C() {
public void getA() {
System.out.println("this is Class C");
};
public void getAa() {
System.out.println("this is getAa");
}
};
D d = new D() {
public void getB() {
System.out.println("this is class D");
}
};
Goods goods = new Goods() {
int goodsValue =33;
public void sayValue() {
System.out.println("the goods value is " + goodsValue);
}
};
goods.sayValue();
c.getA();
//c.getAa(); //该方法无法访问
d.getB();
}
}
/**
* Inner type study
* @author Brook Lieu
* @version v1.0
*/
interface Goods {
public void sayValue();
}
class C {
public void getA() {}
}
class D {
public void getB() {}
}
publicinterface Contents {
int value();
}
publicinterface Destination {
String readLabel();
}
publicclass Goods {
privateclass Content implements Contents {
privateint i = 11;
publicint value() {
return i;
}
}
protectedclass GDestination implements Destination {
private String label;
private GDestination(String whereTo) {
label = whereTo;
}
public String readLabel() {
return label;
}
}
public Destination dest(String s) {
returnnew GDestination(s);
}
public Contents cont() {
returnnew Content();
}
}
class TestGoods {
publicstaticvoid main(String[] args) {
Goods p = new Goods();
Contents c = p.cont();
Destination d = p.dest("Beijing");
}
}
publicclass Goods {
privateint valueRate = 2;
privateclass Content implements Contents {
privateint i = 11 * valueRate;
publicint value() {
return i;
}
}
protectedclass GDestination implements Destination {
private String label;
private GDestination(String whereTo) {
label = whereTo;
}
public String readLabel() {
return label;
}
}
public Destination dest(String s) {
returnnew GDestination(s);
}
public Contents cont() {
returnnew Content();
}
}
publicclass Goods1 {
public Destination dest(String s) {
class GDestination implements Destination {
private String label;
private GDestination(String whereTo) {
label = whereTo;
}
public String readLabel() {
return label;
}
}
returnnew GDestination(s);
}
publicstaticvoid main(String[] args) {
Goods1 g = new Goods1();
Destination d = g.dest("Beijing");
}
}
publicclass Goods2 {
privatevoid 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();
}
}
publicvoid track() {
internalTracking(true);
}
publicstaticvoid main(String[] args) {
Goods2 g = new Goods2();
g.track();
}
}
publicclass Goods3 {
public Contents cont() {
returnnew Contents() {
privateint i = 11;
publicint value() {
return i;
}
};
}
}
frame.addWindowListener(new WindowAdapter(){
publicvoid windowClosing(WindowEvent e){
System.exit(0);
}
});