网站制作模板图片,阿里云网站建设考试认证题,单页面网站多少钱,039 织梦云idc网站源码目录
嵌套类#xff08;上#xff09;
4. 内部类
内部类对象创建语法
示例
5. 局部内部类
示例
6. 匿名内部类
示例
Java SE文章参考:Java SE入门及基础知识合集-CSDN博客 嵌套类#xff08;上#xff09;
4. 内部类 As with instance methods and variables, an…目录
嵌套类上
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 objects 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 () { Override public void eat () { System . out . println ( 老虎吃肉 ); } }; a . eat (); Student stu new Student ( 好奇怪 , 20 ){ Override public void show () { System . out . println ( age ); } }; stu . show (); } public static int calculate ( int a , int b ){ //匿名内部类跟构造方法的调用很相似不同的地方在于匿名内部类里面还有类的主体 Calculate c new Calculate () { Override public int calculate ( int a , int b ) { return a b ; } }; return c . calculate ( a , b ); } } Java SE文章参考:Java SE入门及基础知识合集-CSDN博客