java内部类小结

内部类

作用特点

  • 隐藏代码
  • 可以访问外围类 访问外围类
  • 和组合是完全不同的概念
  • 内部类和属性一样有访问权限的修饰
  • 内部类访问外围类不受访问权限影响
  • 实例化类时,内部类不会被实例化;c++ sizeof()不会计算内部类大小
  • 匿名内部类 c和c++是实现是有所区别的
  • 匿名内部类只能实现或者继承 二选一
  • 内部类隐含一个外部类的引用 但是加上static就会发生改变 它会消失
    但是也不是毫无益处,它就可以单独new了
  • 普通内部类(没有static修饰)的类持有一个外部类的引用,内部类中不允许出现static字段或方法
  • 在方法中定义类结构,返回内部类 java可以正常实现 c++却不行,能返回但是没有句柄
  • 嵌套类(普通内部类持有一个外部类引用):一个内部类通过static关键之修饰就是嵌套类
  • 嵌套类注意点:失去普通内部类对外部类的引用,失去了访问外部类的权限
  • 接口中可以实现内部类,它是一个嵌套类.
  • 使用这个内部类,可以实现多重继承
    private public只是在编译器起作用

匿名内部类其实有一个继承或者实现的过程

package Innerclasses;
//普通的匿名内部类 和Parcel7还是有一定差别的
public class Parcel8 {
    public Wrapping wrapping(int x){          //这是一个匿名内部类  而且还是隐式的继承
        return new Wrapping(x){     //这里其实实现了一种继承的机制很厉害
            public int value(){
                return super.value() * 47;
            }
            public void print(){      //这里可以实现但是和c++一样无法正常引用  哈哈
                System.out.println(value());
            }
        };     //这里是表明表达式结束  这玩意算是一个表达式
    }
    public static void Parcel8main(String[] argv){
        Parcel8 parcel8 = new Parcel8();
        Wrapping wrapping = parcel8.wrapping(18);
        System.out.println(wrapping.value());
    }
}


///分隔符
package Innerclasses;
//匿名内部类
public class Parcel7 {
 public Contents contents(){
     return new Contents() {    //匿名内部类
         private int i = 11;
        @Override
        public int value() {
            // TODO Auto-generated method stub
            return i;
        }
    };
 }

 public static void main(String[] argv){
     Parcel7 parcel7 = new Parcel7();
     Contents contents = parcel7.contents();
     System.out.println(contents.value());         //成功运行
 }
}

/*-----------------------------------------------------*/
package Innerclasses;
//匿名内部类  定义字段并初始化 以及无参构造方法
public class Parcel9 {
    public Destination destination(final String dest){  
        //下面使用 这里必须这样定义
        return new Destination() {
            { //匿名代码块的就是创建时开始执行 == static   我们就可以把他当成一种无参的构造方法啦
                System.out.println("匿名内部类创建");
                System.out.println(this.readLabel());
            }
            private String lable = dest;
            @Override
            public String readLabel() {
                // TODO Auto-generated method stub
                return this.lable;
            }
        };
    }
    public static void main(String[] args) {
        Parcel9 parcel9 = new Parcel9();
        Destination destination = parcel9.destination("xiaochen");
        System.out.println(destination.readLabel());      
    }
}
/****************************************************/
package Innerclasses;

public class Parcel11 {
    private static class ParcelContents implements Contents{ 
        //嵌套内部类  
        private int i = 11;
        @Override
        public int value() {
            // TODO Auto-generated method stub
            return i;
        }
    }

    protected static class ParcelDestination implements Destination{
        private String label;
        public ParcelDestination(String whereTo) {
            // TODO Auto-generated constructor stub
            this.label = whereTo;
        }
        @Override
        public String readLabel() {
            // TODO Auto-generated method stub
            return label;
        }
        public static void fun(){}
        static int x  =  100;
        static class AnotherLevel{
            public static void f(){}
            static int x = 10;
        }
    }
    public static Destination  destination(String s){
        return new ParcelDestination(s);
    }
    public static Contents contents(){
        return new ParcelContents();
    }
    public static void main(String[] argv){
        Contents contents = contents();
        Destination destination = destination("xiaochen");
        System.out.println(contents.value());
        System.out.println(destination.readLabel());
    }
}

/*********************************/
package Innerclasses;
//类中实现嵌套类main正常执行
public class TestBed {
    public void f(){System.out.println("f()");}
    public static class Tester{
        public static void TestBedmain(String[] args){
            TestBed testBed = new TestBed();
            testBed.f();
        }
    }
}

/**************************************/
package Innerclasses;
//正常执行
public abstract class TestSt {
    abstract void fun();
    public static class Tester extends TestSt{
        @Override
        void fun() {
            // TODO Auto-generated method stub
            System.out.println("fun Function!");
        }
        public static void main(String[] argv){
            TestSt testSt = new Tester();
            testSt.fun();
        }
    }

}

/******************************************/
package 内部类调用;

public abstract class Plane {
    abstract void flying();
    public static class fly extends Plane{
        @Override
        void flying() {
            // TODO Auto-generated method stub
            System.out.println("起飞");
        }
        void play(){
            System.out.println("开始起飞");
        }

    }
}
package 内部类调用;

public interface Inter {
    void move();
    class car implements Inter{
        @Override
        public void move() {
            // TODO Auto-generated method stub
            System.out.println("move function!");
        }
    }
}
package 内部类调用;

public class Main {
    public static void main(String[] args){
        Inter inter  =  new Inter.car();
        inter.move();
        Plane plane = new Plane.fly();
        Plane.fly fly = (Plane.fly) plane;
        fly.play();
        fly.flying();
    }
}

/*******************************/
package Innerclasses;
//内部类继承
class WithInner{
    class Inner{}
}
public class InheritInner extends WithInner.Inner {
    public InheritInner(WithInner w) { //必须这样写
        // TODO Auto-generated constructor stub
        w.super();
    }
    public static void InheritInnermain(String[] args) {
        WithInner withInner = new WithInner();
        InheritInner inheritInner = new InheritInner(withInner); //这样才可以正常初始化
        System.out.println("main");
    }
}

/***************************************/
//局部类是否可以被覆盖  不会
//package Innerclasses;

class Egg{
    private Yolk y;
    protected class Yolk {
        // TODO Auto-generated constructor stub
        public Yolk() {
            // TODO Auto-generated constructor stub
            System.out.println("Egg Yolk");
        }
    }
    public Egg() {
        // TODO Auto-generated constructor stub
        System.out.println("Egg.yolk");
        y = new Yolk();  //这里不会被子类覆盖
    }
}
public class BigEgg extends Egg {
    public class Yolk{
        public Yolk() {
            // TODO Auto-generated constructor stub
            System.out.println("BigEgg.Yolk init");
        }
    }
    public static void main(String[] args) {
    new BigEgg(); //这样直接初始化父类的内部类和外部类  因为内部类会被直接初始化
    System.out.println("----------------------------------------");
    new BigEgg().new Yolk();  //这里也可以看出来  gigedd不会直接初始化
    }
}
/* Egg.yolk
Egg Yolk
----------------------------------------
Egg.yolk
Egg Yolk
BigEgg.Yolk init

 */

嵌套类小结

  • 接口中实现嵌套类 编译成功 但是实现main方法会报错,找不到main
  • 抽象类 普通类都能实现嵌套类 并且实现main正常运行
  • 嵌套类的调用还是蛮简单的就是外部类.嵌套类 包括接口抽象类

接口

接口:接口默认所有方法都是public static修饰的

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值