2021腾讯云限时秒杀,爆款1核2G云服务器298元/3年!(领取2860元代金券),
地址:https://cloud.tencent.com/act/cps/redirect?redirect=1062
2021阿里云最低价产品入口+领取代金券(老用户3折起),
入口地址:https://www.aliyun.com/minisite/goods
相关推荐:Java基础笔记 – 内部类 静态内部类 成员内部类 局部内部类 匿名内部类anonymous inner classes
Anonymous classes enable you to make your code more concise. They enable you to declare and instantiate a class at the same time. They are like local classes except that they do not have a name. Use them if you need to use a local class onl
1.什么是内部类
定义在一个类里面的类 里面的类被称为内部类
包含内部类的类 称为外部类
内部类 也可以设置访问权限public、private 等修饰符
内部类可以访问外部类的所有成员包括private成员
2.如何创建一个内部类对象
在外部类之外的任何地方创建内部类对象 需要指明内部类类型方式如下
相关推荐:Java中内部类揭秘(一):外部类与非静态内部类的”相互可见性“
好文值得转~ 我们都知道,非静态内部类可以访问外部类的私有成员(包括私有变量和方法),这也正是使用非静态内部类与普通类的一个重要区别:非静态内部类是依赖于外部类对象而存在的,这种依赖就包括它要能自由地访问外部类对象的所有成员(因为private成员
OuterClassName.InnerClassName
在内部类是非static类时,构建内部类需要外部类对象引用例如下面代码
package com.example; public class Outer { private int a; public class Inner { public void printf() { System.out.println(a); } } }
import com.example.Outer; public class Final { public static void main(String[] args) { Outer.Inner i = new Outer().new Inner(); i.printf(); } }
package com.example; public class Outer { private static int a; public static class Inner { public void printf() { System.out.println(a); } } }static内部类 只能访问外部类的static成员
import com.example.Outer; public class Final { public static void main(String[] args) { Outer.Inner i = new Outer.Inner(); i.printf(); } }在构建static内部类对象是不需要指定外部类对象引用了直接通过外部类名就可以调用内部类的构造方法了。