Java学习10:内部类
来源:互联网 发布:去公司做淘宝推广员 编辑:程序博客网 时间:2024/05/31 13:16
内部类
将一个类定义到另外一个类中,对里面那个类就称为内部类(内置类,嵌套类)。
访问特点
- 内部类可以直接访问外部类中的成员,包括私有成员,之所以可以直接访问外部类中的成员,是因为内部类中持有一个外部类的引用,格式:外部类名.this;
- 外部类要访问内部类的成员必须要建立内部类的对象。
class Outer{ private int x = 4; class Inter { int x = 5; void function() { int x = 6; System.out.println("x = " + x);//输出6 //System.out.println("x = " + this.x);//输出5 //System.out.println("x = " + Outer.this.x);//输出4 } } void method() { Inter in = new Inter(); in.function(); }}class Demo{ public static void main(String[] args) { Outer.Inter in = new Outer().new Inter(); in.function(); //new Outer().method(); }} //x = 6
访问格式
- 当内部类定义在外部类的成员位置上,而且非私有,可以在外部其它类中直接建立内部类对象,格式:外部类名.内部类名 变量名 = 外部类对象.内部类对象(Outer.Inter in = new Outer().new Inter();)
class Outer{ private int x = 4; class Inter { void function() { System.out.println("x = " + x); } }}class Outer2{ void method() { Outer.Inter in = new Outer().new Inter(); in.function(); }}class Demo{ public static void main(String[] args) { Outer2 out2 = new Outer2(); out2.method();//x = 4 }}
- 当内部类在成员位置上,就可以被成员修饰符所修饰:
private :将内部类在外部类中进行封装;
static:内部类就具备static的特性。当内部类被static修饰后,只能直接访问外部类中的static成员,出现了访问权限。
在外部其他类中,如何直接访问static内部类的非静态成员呢?
new Outer.Inter().function();
class Outer{ private static int x = 4; static class Inter//静态内部类 { void function() { System.out.println("x = " + x); } }}class Demo{ public static void main(String[] args) { new Outer.Inter().function();//x = 4 }}
在外部其他类中,如何直接访问static内部类的静态成员呢?
Outer.Inter().function();
注意:当内部类中定义了静态成员,该内部类必须是static的;
当外部类中的静态方法访问内部类时,内部类也必须是静态的。
class Outer{ private static int x = 4; static class Inter//内部类也必须是静态 { void function() { System.out.println("x = " + x); } } public static void method()//外部类中的静态方法 { new Inter().function(); }}class Demo{ public static void main(String[] args) { new Outer().method();//x = 4 }}
匿名内部类
内部类定义在局部时
- 不可以被成员修饰符修饰;
- 可以直接访问外部类中的成员(x),因为还持有外部类中的引用,但是不可以访问他所在的局部中的变量(y),只能访问被final修饰的局部变量(final int y = 6)。
class Outer{ int x = 4; void method() { int y = 6; //匿名内部类不能为静态,因为static只修饰成员, //Inter为局部的,所以不能,所以类中也不可能有静态成员 class Inter { void function() { System.out.println("x = " + Outer.this.x); } } new Inter().function();//有对象才会运行 }}class Demo{ public static void main(String[] args) { new Outer().method(); }}//x = 4
匿名内部类
- 匿名内部类其实就是内部类的简写形式;
- 定义匿名内部类的前提:内部类必须是继承一个类或者实现接口;
- 匿名内部类的格式:new 父类或接口(){定义子类的内容};
- 其实匿名内部类就是一个匿名子类对象,而且此对象有点胖,可以理解为带内容的对象;
- 匿名内部类中定义的方法最好不要超过3个。
/*class Outer{ int x = 4; class Inter//内部类 { void show() { System.out.println("x = " + x); } } void method() { new Inter().show(); }}class Demo{ public static void main(String[] args) { new Outer().method(); }}//x = 4*///上述代码可以用匿名内部类重写abstract class ShowDemo{ abstract void show();}class Outer{ int x = 4; void method() { new ShowDemo()//匿名内部类实现 { void show() { System.out.println("x = " + x); } }.show(); }}public class Demo{ public static void main(String[] args) { new Outer().method(); }}//x = 4
练习1 通过内部类补全代码
interface Inter{ void method();}class Test{ //通过内部类补足代码// //内部类实现// static class Inner implements Inter// {// public void method()// {// System.out.println("method!");// }// }// static Inter function()// {// return new Inner();// } //匿名内部类实现 static Inter function() { return new Inter() { public void method() { System.out.println("method!"); } }; }}class Demo{ public static void main(String[] args) { //Test.function():Test类中有一个静态的方法function //.method():function这个方法运算后的结果是一个对象,而且是一个Inter类型的对象 //因为只有是Inter类型的对象,才可以调用method方法 /* Inter in = Test.function(); in.method(); */ Test.function().method(); }}
练习2 无继承和接口时,定义匿名内部类,然后直接调用其方法
class Demo{ public static void main(String[] args) { //有分号代表Object对象, 有{}代表Object的子类对象 //new Object(); //代表Object对象 new Object() //代表Object的子类对象 { void function() { System.out.println("function"); } }.function(); }}
阅读全文
1 0
- Java学习10:内部类
- Java学习:内部类
- Java内部类学习
- java内部类学习!!
- java学习---内部类
- Java 内部类学习
- 【JAVA学习】内部类
- java学习-内部类
- java内部类学习
- JAVA学习:内部类
- JAVA学习-内部类
- JAVA学习--内部类
- JAVA内部类学习
- Java内部类学习
- Java学习:内部类
- java学习--内部类
- java 内部类学习
- 学习java内部类
- React Native iOS打包配置和打包
- mysql锁表解锁
- JAVA使用Jedis操作Redis
- 对c语言内存的思考
- 简单的使用xBanner
- Java学习10:内部类
- 在ubantu14.04和win7双系统下忘记ubantu系统password的解决办法
- 宏观 量子计算一句话
- ES6书写规范
- matplotlib.pyplot基础(一)
- yum 本地源配置
- 第八周项目1
- 坚持#第223天~零基础自学云计算基础语言应用之Shell13~17节
- 2017年10月25日关于哈希函数去重的方法