目录
嵌套类(上)
4. 内部类
内部类对象创建语法
示例
5. 局部内部类
示例
6. 匿名内部类
示例
Java SE文章参考:Java SE入门及基础知识合集-CSDN博客
嵌套类(上)
4. 内部类
As with instance methods and variables, an inner class is associated with an instance of its enclosing class and has direct access to that object's methods and fields. Also, because an inner class is associated with an instance, it cannot define any static members itself.与实例方法和变量一样,内部类与其所在类的实例相关联,并且可以直接访问该对象的方法和字段。 另外,由于内部类与实例相关联,因此它本身不能定义任何静态成员。
内部类对象创建语法
外部类类名 . 内部类类名 对象名 = new 外部类类名 (). new 内部类类名 ();
示例
使用内部类描述一辆汽车拥有一台发动机。
package com . wq . inner . clazz . inner ;public class Car { // 汽车private double price ;private String brand ;private Engine engine ; // 汽车拥有的发动机public Car ( double price , String brand ) {this . brand = brand ;this . engine = new Engine ( " 国产 " , 20000 );this . price = price + engine . price ;}public Car ( Engine engine , String brand , double price ){this . engine = engine ;this . brand = brand ;this . price = price + engine . price ;}public void show (){this . engine . show ();}class Engine { // 发动机private String type ; // 发动机类型private double price ; // 发动机价格public Engine ( String type , double price ) {this . type = type ;this . price = price ;}public void show (){System . out . println ( brand + " 汽车使用的是 " + type + " 发动机,价格为:" + price );//如果内部类中存在于外部类同名的成员变量时,想要使用外部类的同名成员变量,需要加上: 外部类类名.this.变量名System . out . println ( " 汽车总价为: " + Car . this . price );}}}package com . wq . inner . clazz . inner ;public class CarTest {public static void main ( String [] args ) {Car c = new Car ( 100000 , " 奥拓 " ); //c 是汽车类的成员,因此 c 对象中有 Engine 类成员c . show ();Car . Engine engine = new Car ( 100000 , " 奥拓 " ). new Engine ( " 进口 " , 50000 );Car c1 = new Car ( engine , " 奔驰 " , 150000 );c1 . show ();Car . Engine engine1 = c . new Engine ( " 进口 " , 50000 );Car c2 = new Car ( engine1 , " 奔驰 " , 165600 );c2 . show ();}}
5. 局部内部类
Local classes are classes that are defined in a block, which is a group of zero or more statements between balanced braces. You typically find local classes defined in the body of a method.局部类是在一个块中定义的类,该块是一组在平衡括号之间的零个或多个语句。 通常,你会在方法的主体中找到定义的局部类。
示例
使用局部内部类描述使用计算器计算两个数的和。
package com . wq . inner . clazz . local ;public class LocalClass {public static void main ( String [] args ) {int result = calculate ( 1 , 3 );System . out . println ( result );}public static int calculate ( int a , int b ){class Calculator {private int num1 , num2 ;public Calculator ( int num1 , int num2 ) {this . num1 = num1 ;this . num2 = num2 ;}public int calculate (){return num1 + num2 ;}}Calculator c = new Calculator ( a , b );return c . calculate ();}}
6. 匿名内部类
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 only once.匿名类可以使你的代码更简洁。 它们使你在声明一个类的同时实例化它。 除了没有名称外,它们类似于局部类。 如果只需要使用一次局部类,则使用它们。The syntax of an anonymous class expression is like the invocation of a constructor, except that there is a class definition contained in a block of code.匿名类表达式的语法类似于构造方法的调用,不同之处在于代码块中包含类定义。
示例
package com . wq . inner . clazz . anonymous ;public interface Calculate {int calculate ( int a , int b );}package com . wq . inner . clazz . anonymous ;public abstract class Animal {public abstract void eat ();}package com . wq . inner . clazz . anonymous ;public class Student {protected String name ;protected int age ;public Student ( String name , int age ) {this . name = name ;this . age = age ;}public void show (){System . out . println ( name + "\t" + age );}}package com . wq . inner . clazz . anonymous ;public class AnonymousClass {public static void main ( String [] args ) {int result = calculate ( 10 , 20 );System . out . println ( result );Animal a = new Animal () {@Overridepublic void eat () {System . out . println ( " 老虎吃肉 " );}};a . eat ();Student stu = new Student ( " 好奇怪 " , 20 ){@Overridepublic void show () {System . out . println ( age );}};stu . show ();}public static int calculate ( int a , int b ){//匿名内部类跟构造方法的调用很相似,不同的地方在于:匿名内部类里面还有类的主体Calculate c = new Calculate () {@Overridepublic int calculate ( int a , int b ) {return a + b ;}};return c . calculate ( a , b );}}