内部类详情

一.什么是内部类

1.内部类是外部类中的元素

二.内部类的定义

1.外部类中使用内部类,按正常的方式使用

2.外部类以外使用非静态内部类

Peop peop = new Peop();
    Peop.Mess mess = peop.new Mess();
    mess.getChinese();
        
    public class Peop {

        private String name;
        private Mess mess;

        public void initMess(){
            mess = new Mess();
        }


        public class Mess {
            public Mess() {

            }

            private String math;
            private String english;
            private String chinese;

            public String getMath() {
                return math;
            }

            public void setMath(String math) {
                this.math = math;
            }

            public String getEnglish() {
                return english;
            }

            public void setEnglish(String english) {
                this.english = english;
            }

            public String getChinese() {
                return chinese;
            }

            public void setChinese(String chinese) {
                this.chinese = chinese;
            }
        }

        ;
    }

3.外部类以外使用静态内部类

 Robot.ContentBuilder builder = new Robot.ContentBuilder();
        builder.getHigh();

class Robot {

    static class ContentBuilder {
        private String high;
        private String width;


        public String getHigh() {
            return high;
        }

        public void setHigh(String high) {
            this.high = high;
        }

        public String getWidth() {
            return width;
        }

        public void setWidth(String width) {
            this.width = width;
        }
    }
}

4.局部内部类

class Rectangle {
        private int width;
        private int height;

        public int getArea() {
            class Rule {
                int measureWidth = 3;
                int measureHeight = 4;

                public int getRule() {
                    return measureHeight * measureWidth;
                }
            }
            Rule rule = new Rule();
            return rule.getRule();

        }
    }

5.匿名内部类

new 接口()|父类构造器(实参列表){

//匿名内部类类体部分

}

  new BaseView(){

            @Override
            public void showView() {

            }
        };
    

    interface BaseView {
        void showView();
    }

 

 

 

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值