java 基础

/*
1.内部类可以访问外部类的成员,包括私有
 之所以可以直接访问外部类的成员,是因为内部类中包含了外部类的引用(外部类名.this)
2.外部类访问内部类时。需要建立内部类的对象。



访问格式:
1当内部类定义在外部类的成员位置上时,而且是非私有,,,可以在外部其他类中访问。
可以直接建立内部类对象:
 外部类名.内部类名 变量名=外部对象.内部对象;
  Outter.Inter out=new Outter().new Inter();
2.内部类在成员位置上时,,就可以被成员修饰符修饰。
   例如:private,将内部类在外部类中进行封装。
         static:只能直接访问外部类中的static修饰的成员,出现了访问局限。
                new Outter.Inter().function();

3.如何在外部其他类访问内部类的非静态成员呢
new Outter.Inter().function();
  如何在外部其他类访问内部类的静态成员呢
  Outter.Inter.function();

  注意:当内部类的成员是静态的,,那么内部类必须是static的。
        当外部类的静态方法访问,内部类时,内部类也必须是静态的、

*/

class Outter
{
private int x=3;
static class Inter
{
int x=4;
void function()
{
int x=56;
 System.out.println("Inter method:"+Outter.this.x);
}
}
void method()
{
 System.out.println("hheheh");
 Inter in=new Inter();
 in.function();
}

}

class Classdemo 
{

public static void main(String[] args) 
{
//Outter.Inter out=new Outter().new Inter();
//out.function();
new Outter.Inter().function();
}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值