内部类的几个调用例子

第一种:

package com.itheima.manage.info.test06;
//!!!第一类内部类调用
public class demo03 {
    static class Outside{
        private int a = 100;
        class Inside{         //内部类
            private int a = 200;
            public void show(){  //内部类的方法
                int a = 300;
                System.out.println(Outside.this.a);//输出100
                System.out.println(this.a);//输出200
                System.out.println(a);//输出300
            }
        }
    }

    public static void main(String[] args) {
        Outside.Inside o =new Outside().new Inside();  //直接新建内部类
        o.show(); //调用内部类方法
    }

}

第二种

package com.itheima.manage.info.test06;
//!!!第二种内部类调用(类种类)
public class demo01 {
    static class Outside{
        public void show(){
              int a=10;
            class Inside{
                public void show(){
                    System.out.println( "a=" + a);//10
                }
            }
            Inside i=new Inside();  //在大类中调用类种类,类外不能调用
            i.show();

        }
    }

    public static void main(String[] args) {
           Outside outside=new Outside();  //类外新建大类
           outside.show();  //调用类中方法(已经调用的方法)

    }
}

第三种

package com.itheima.manage.info.test06;

import java.sql.SQLOutput;
//第三种内部类调用
public class demo02 {
    interface Animal{ //接口
        public void show();//接口方法
    }
   static class  Test{
        public static void main(String[] args){
            fun(new cat());//请用子类的形式调用
            fun(new Animal() {//请用匿名内部类的形式调用
                @Override
                public void show() {
                    System.out.println("城会玩");
                }
            });
        }
        public static void fun(Animal a ){ //使用接口

            a.show();                       //调用接口方法
        }
    }
       static class cat implements Animal{  //子类继承父类

           @Override                        //重写父类
           public void show() {
               System.out.println("吃猫粮");
           }
       }

}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值