内部类

本文转自:https://www.cnblogs.com/shen-hua/p/5440285.html

成员内部类

特征:

1. 作为外部类的一个成员存在,与外部类的属性、方法并列

2. 成员内部类持有外部类的引用(能直接使用外部类的方法、属性

3. 成员内部类中不能定义static变量和方法

使用格式:

Outer outer = new Outer();
Outer.Inner inner = outer.new Inner();//写法有点儿奇怪...

实例:

/*
 * 成员内部类
 */
public class Body {
    String arm;
    String leg;
    String blood;
    public Body(String arm, String leg, String blood) {
        super();
        this.arm = arm;
       this.leg = leg;
        this.blood = blood;
    }
    //成员内部类Heart
    class Heart{
        String name;
         void work() {
            System.out.println("心脏正在给"+arm+leg+"输"+blood);
        }
     };
}
public class Test {
    public static void main(String[] args) {       
        Body body=new Body("两个胳膊","两条腿","血");
        Body.Heart  heart=body.new Heart();
        heart.work();
    }
}

---------------------------------------------------------------------------------------------------------

静态内部类

特征:

1. 静态内部类不会持有外部类的引用,如果访问外部类的成员变量必须通过外部类的实例访问

2. 静态内部类可以访问外部的静态变量,(就好比静态方法只能调用静态方法,访问静态变量)

3. Java中只有内部类才可以是静态类

使用格式:

Outer.Inner inner = new Outer.Inner();

实例:

/*
 * 静态内部
 */
public class Company {
    String companyNam;
    static String country;
    static class Clear{
        String name;
        public Clear() {
            // TODO Auto-generated constructor stub
        }
        
        public Clear(String name) {
            super();
            this.name = name;
        }

        public void work(String name){
            String na=new Company().companyNam="联想";
            country="中国";
            System.out.println(name+"为"+na+"打扫卫生,该公司属于"+country);
        }
    }
}
public class Test {
    public static void main(String[] args) {
        Company.Clear zcl=new Company.Clear();
        zcl.work("shen_hua");
    }

}

---------------------------------------------------------------------------------------------------------

局部内部类

特征:

1. 用在方法内部,作用范围仅限于该方法中

2. 根据情况决定持有外部类对象引用

3. 不能使用private,protected,public修饰符

4. 不能包含静态成员

5. 只有在方法的局部变量被标记为final或局部变量是effctively final的,内部类才能使用它们

实例:

/*
 * 局部内部类
 */
public class School {

    String schoolName;
    String buss="培养人才";
    int studentNum;
    public School() {
        // TODO Auto-generated constructor stub
    }
    
    public School(String schoolName, String buss, int studentNum) {
        super();
        this.schoolName = schoolName;
        this.buss = buss;
        this.studentNum = studentNum;
    }

    //宣传
    public void show(){
        final double tvMoney=10000;
        final double netMoney=20000;
        class AdverTeam{
            String teamName="shen_hua";
            //电视宣传
            public void tvShow(){
                System.out.println("宣传队是:"+teamName);
                System.out.println("这是电视宣传,学校名称"+schoolName+",业务内容:"+buss+",学校人数:"+studentNum+",宣传所需费用"+tvMoney);
            }
            //网络宣传
            public void netShow(){
                System.out.println("宣传队是:"+teamName);
                System.out.println("这是网络宣传,学校名称"+schoolName+",业务内容:"+buss+",学校人数:"+studentNum+",宣传所需费用"+netMoney);
            }
        }
        new AdverTeam().tvShow();
        new AdverTeam().netShow();
    }
}
public class Test {
    public static void main(String[] args) {
        School qinghua=new School("清华","互联网培训",1000);
        
        qinghua.show();
    }
}

---------------------------------------------------------------------------------------------------------

匿名内部类

特征:

1. 使用new创建没有具体位置

2. 创建的匿名类,默认继承或实现new后面的类型

3. 根据使用情况决定是否持有外部类对象引用

4. 内嵌匿名类编译后生成的.class文件的命名方式是”外部类名称$编号.class”,编号为1,2,3…n,编号为x的文件对应的就是第x个匿名类

/*
 * 匿名内部类
 */
public interface Person {
    public void run();
}
/*
 * 测试类
 */
public class Test {
    public static void main(String[] args) {
        Person p=new Person() {  //匿名内部类,实现Person接口      
            public void run() {
                System.out.println("匿名内部类实现的");
            }
        };
        p.run();
    }

}
原文地址:https://www.cnblogs.com/yanze/p/9928359.html