java基础回顾(二)——内部类

本文详细介绍了Java中内部类的概念,包括常规内部类和静态内部类的特点与使用方式,并提供了具体的实例化过程。

一、常规内部类

 1 public class Outer {
 2     int count;
 3     
 4     void say(){
 5         count++;
 6         System.out.println("我是外部类");
 7     }
 8     void walk(){
 9         count++;
10         System.out.println("I am walking.....");
11     }
12     class Inner{
13         int inner_count; //外部类无法访问到内部类的局部变量
14         void say(){
15             walk();//内部类可以直接调用外部类方法及变量
16             Outer.this.say();//方法名重复时可以使用此种方法调用
17             count++;
18             System.out.println("我是内部类"+count);
19         }
20         
21     }
22 }

   实例化内部类

1 public class Demo {
2     public static void main(String[] args) {
3         Inner i=new Outer().new Inner();
4         i.say();
5     }
6 }

 

二、静态内部类

 1 public class Outer {
 2     static int count;
 3     
 4     void say(){
 5         count++;
 6         System.out.println("我是外部类");
 7     }
 8     void walk(){
 9         
10         System.out.println("I am walking.....");
11     }
12     static    class Inner{ //静态内部类无法访问外部类的非静态变量
13         int inner_count; 
14         void say(){
15             System.out.println("我是内部类"+count);
16         }
17     }
18 }

    实例化内部类

public class Demo {
    public static void main(String[] args) {
        Inner i=new Outer.Inner();
        i.say();
    }
}

 

转载于:https://www.cnblogs.com/lyysz/p/5794686.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值