JAVA学习--内部类

一、创建内部类:把类的定义置于外围类的里面即可。

二、链接到外部类:生成一个内部类对象时,此对象能访问其外围对象的所有成员而不需要任何特殊条件。

三、使用.this和.new:

        使用.this:当需要生成对外部类对象的引用时,可以使用外部类的名字后面紧跟.this,这样产生的引用自动的具有正确的类型,这一点在编译期就可被知晓并收到检查。

public class DotThis {
    void f(){System.out.println("Dothis.f()");}
    public class Inner{
        public DotThis outer(){
            return DotThis.this;
        }
    }
    public Inner inner(){ return new Inner(); }
    public static void main(String[] args){
        DotThis dt = new DotThis();
        DotThis.Inner dti = dt.inner();
        dti.outer().f();
    }
}

        使用.new:创建某个内部类的对象时使用。注意在拥有外部类对象之前是不可能创建内部类对象的。因为内部类对象会暗中链接到创建它的外部类对象上。但如果创建的时嵌套类(静态内部类),那它就不需要对外部类对象的引用。

public class DotNew {
    public class Inner{
        Inner(){
            System.out.println("Construct Inner");
        }
    }
    public Inner inner(){
        return new Inner();
    }
    public static void main(String[] args){
        DotNew dn = new DotNew();
        DotNew.Inner dni1 = dn.inner();
        DotNew.Inner dni = dn.new Inner();
    }
}

四、在方法和作用域内的内部类

       这点具体用处暂时不是很懂,先放一放。

五、匿名内部类

public interface Contents{
    int value();
}
public class Parcel7 {
    public Contents contents(fianl f){
        return new Contents(){
            private int exam = f;
            private int i = 11;
            public int value(){return i;}
        };
    }
    public static void main(String[] args){
        Parcel7 p = new Parcel7();
        Contents c = p.contents();
    }
}

注意在匿名内部类中使用的参数一定要是final的。

六、工厂模式中的匿名内部类实例

public interface Service {
    void method1();
    void method2();
}
public interface ServiceFactory {
    Service getService();
}
public class Implementation1 implements Service {
    private Implementation1() {
    }

    @Override
    public void method1() {
        System.out.println("Implementation1 method1");
    }

    @Override
    public void method2() {
        System.out.println("Implementation1 method2");
    }
    public static ServiceFactory factory = new ServiceFactory() {
        @Override
        public Service getService() {
            return new Implementation1();
        }
    };
}
public class Implemention2 implements Service {
    private Implemention2() {
    }

    @Override
    public void method1() {
        System.out.println("Implementation2 method2");
    }

    @Override
    public void method2() {
        System.out.println("Implementation2 method2");
    }
    public static ServiceFactory factory = new ServiceFactory() {
        @Override
        public Service getService() {
            return new Implemention2();
        }
    };
}
public class Factories {
    public static void serviceConsumer(ServiceFactory factory){
        Service s = factory.getService();
        s.method1();
        s.method2();
    }
    public static void main(String[] args){
        serviceConsumer(Implementation1.factory);
        serviceConsumer(Implemention2.factory);
    }
}

七、接口内部的类

       正常情况下不能在接口内部放置任何代码,但嵌套类可以作为接口的一部分。

public interface ClassInInterface {    //接口中的任何类都是自动public和static的
    void howdy();
    class Test implements ClassInInterface{
        @Override
        public void howdy() {
            System.out.println("Howdy");
        }
    }
    public static void main(String[] args){
        new Test().howdy();
//        Test t = new Test();
//        t.howdy();
    }
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值