4.2 类JAVA程序是对象的集合,而对象是类的实例化源程序就是一个个的Java类Java本身提供的类(核心API)见Java文档中描述程序员可以对其进行调用j2sdk1.4.1_01Vjre)/lib\rt.jar(22.4MB)程序员自己定义的类16
16 ◼ 程序是对象的集合,而对象是类的实例化 ◼ 源程序就是一个个的Java类 ◼ Java本身提供的类(核心API) ◼ 见Java文档中描述 ◼ 程序员可以对其进行调用 ◼ j2sdk1.4.1_01\jre\lib\rt.jar(22.4MB) ◼ 程序员自己定义的类 4.2 类
4.2.1类的定义JAVA类的定义格式[类的修饰符lclass类名[extends父类名]implements[接口名]类型成员变量1;类型成员变量2;类型成员方法1(参数1,[参数2,])[方法体;类型成员方法2参数1,[参数2,...J[方法体;人17
17 ◼ 类的定义格式 [类的修饰符]class 类名 [extends 父类名] implements[接口名] { 类型 成员变量1; 类型 成员变量2; . . . . . . 类型 成员方法1(参数1, [参数2, .]) { 方法体; } 类型 成员方法2(参数1, [参数2, .]) { 方法体; } . . . . . . } 4.2.1 类的定义
4.2.2类的描述JAVA类的定义格式[类的修饰符|class类名[extends父类名[implements接口名]]类的修饰符public:公共类,可以被其他类所使用,declaresthatthe class can be used by any class regardless of itspackage(无任何限制)无修饰/默认说明:a class can beusedonlybyotherclassesin the same package (仅仅能在同一个包中的其他类引用)abstract: declaresthatthe class cannot be instantiated(宣布该类不能被实例化)final:declares that the class cannot be subclassed(宣 布该类不能有子类)18
18 ◼ 类的定义格式 [类的修饰符] class 类名 [extends 父类名] [implements 接口名] { . . . . . . } ◼ 类的修饰符 ◼ public: 公共类,可以被其他类所使用,declares that the class can be used by any class regardless of its package (无任何限制) ◼ 无修饰/默认说明: a class can be used only by other classes in the same package (仅仅能在同一个包中的其 他类引用) ◼ abstract: declares that the class cannot be instantiated (宣布该类不能被实例化) ◼ final: declares that the class cannot be subclassed (宣布 该类不能有子类) 4.2.2 类的描述
4.2.3类的修饰符JAVA类的修饰符final -- Declares that the class cannot besubclassed.(宣布该类不能有子类)final class ChessAlgorithm 了classBetterChessAlgorithm extendsChessAlgorithm7Can't subclassfinal classes:class ChessAlgorithmclassBetterChessAlgorithm extendsChessAlgorithmΛ1error
19 ◼ 类的修饰符 ◼ final - Declares that the class cannot be subclassed.(宣布该类不能有子类) 4.2.3类的修饰符 final class ChessAlgorithm { . . . } class BetterChessAlgorithm extends ChessAlgorithm { . . . } Can't subclass final classes: class ChessAlgorithm class BetterChessAlgorithm extends ChessAlgorithm { ^ 1 error
4.2.4类的定义格式JAVA类的定义格式[类的修饰符] class 类名[extends 父类名][implements 接口名][extends:继承的关系implements:实现哪些接口(interface)的方法,实现多重继承public class Test extends Frameimplements ActionListener, ItemListener 20
20 ◼ 类的定义格式 [类的修饰符] class 类名 [extends 父类名] [implements 接口名] { . . . . . . } ◼ extends: 继承的关系 ◼ implements: 实现哪些接口(interface)的方法,实 现多重继承 public class Test extends Frame implements ActionListener, ItemListener { . . . . } 4.2.4 类的定义格式