资源预览内容
第1页 / 共32页
第2页 / 共32页
第3页 / 共32页
第4页 / 共32页
第5页 / 共32页
第6页 / 共32页
第7页 / 共32页
第8页 / 共32页
第9页 / 共32页
第10页 / 共32页
亲,该文档总共32页,到这儿已超出免费预览范围,如果喜欢就下载吧!
资源描述
第4章 接口、内部类和 Java API基础 4.1 接口 4.2 内部类和内部接口 4.3 java.lang包中的基础类库 4.4 java.util包中的工具类库程序设计实用教程(第3版)4.1 接口n4.1.1 接口与实现接口的类n4.1.2 用接口实现多重继承程序设计实用教程(第3版)4.1.1 接口与实现接口的类n声明接口public interface 接口 extends 父接口列表 public static final 数据类型 成员变量=常量值;public abstract 返回值类型 成员方法(参数列表); public interface Area /可计算面积接口 public abstract double area(); /计算面积 接口不能被实例化程序设计实用教程(第3版)2. 声明实现接口的类修饰符 class 类 extends 父类 implements 接口列表 例如, public class Rectangle implements Area public class Ellipse implements Area 实现接口的非抽象类必须实现所有接口中的 所有抽象方法,否则声明为抽象类程序设计实用教程(第3版)【例4.1】可计算面积接口与 实现该接口的矩形类。 程序设计实用教程(第3版)3. 接口是引用数据类型Area g = new Rectangle(10,20); /接口变量g引用实现接口的类的对象 g.toString() /执行Rectangle的方法 g = new Ellipse(10,20); /g引用椭圆对象 g.toString() /执行Ellipse的方法程序设计实用教程(第3版)【例4.2】可计算面积接口与 实现该接口的矩形类。程序设计实用教程(第3版)【例4.3】 球类实现多个接口 。程序设计实用教程(第3版)4.1.2 Java用接口实现多重继 承n接口的多继承n接口与抽象类的区别程序设计实用教程(第3版)3. 单继承和多继承 程序设计实用教程(第3版)图4.6 多继承的“钻石继承” 类型会导致二义性 程序设计实用教程(第3版)4.2 内部类和内部接口 public class Line /直线类,外层类型 class Point /点类,内嵌类型 程序设计实用教程(第3版)1.作为类型的特性n内嵌类型不能与外层类型同名。n内部类中可以声明成员变量和成员方法 。n内部类可以继承父类或实现接口。n内部类可以声明为抽象类或内部接口。程序设计实用教程(第3版)2. 作为成员的特性n使用点运算符“.”引用内嵌类型: 外层类型.内嵌类型 Line.Pointn彼此信任,能访问对方的所有成员。n具有类中成员的4种访问控制权限。n内部接口总是静态的。程序设计实用教程(第3版)【例4.4】 直线类声明内嵌 的点类和方向接口。 程序设计实用教程(第3版)4.3 java.lang包中的基础 类库n4.3.1 Object类n4.3.2 Math数学类n4.3.3 Comparable可比较接口n4.3.4 基本数据类型的包装类n4.3.5 String字符串类n4.3.6 Class类操作类n4.3.7 System系统类和Runtime运行 时类程序设计实用教程(第3版)4.3.1 Object类package java.lang; public class Object public Object() /构造方法public final native Class getClass(); /返回当前对象所在的类public boolean equals(Object obj) /比较当前对象与obj是否相等public String toString() /返回当前对象的信息字符串protected void finalize() throws Throwable /析构方法 程序设计实用教程(第3版)4.3.2 Math数学类public final class Math extends Object public static final double E = 2.7182818284590452354; /常量public static final double PI = 3.14159265358979323846;public static double abs(double a) /求绝对值public static double max(double a,double b) /最大值public static double min(double a,double b) /最小值public static double random() /返回一个0.01.0之间的随机数 程序设计实用教程(第3版)4.3.3 Comparable可比较接 口public interface Comparable int compareTo(T o) /比较对象 其中,是Comparable接口的参数,表 示一个类。 程序设计实用教程(第3版)4.3.4 基本数据类型的包装 类n8个Byte、Short、Integer、Long、Float、Double、Character、 Boolean。 public final class Integer extends Number implements Comparable public static final int MIN_VALUE = 0x80000000;/最小值常量,-231public static final int MAX_VALUE = 0x7fffffff; /最大值常量,231-1public Integer(int value) /构造方法public Integer(String s) throws NumberFormatException public static int parseInt(String s) throws NumberFormatException/将字符串转换为整数,静态方法public boolean equals(Object obj) /覆盖Object类中方法public String toString() /覆盖Object类中方法public int compareTo(Integer anotherInteger)/比较两个对象的大小,返回两者之间的差值,实现Comparable接口中方法 程序设计实用教程(第3版)4.3.5 String字符串类public final class String extends Objectimplements java.io.Serializable, Comparable, CharSequence public String() /构造方法public String(String original)public String toString() /覆盖Object类中方法public int length() /返回字符串的长度public boolean equals(Object obj) /比较字符串是否相等public boolean equalsIgnoreCase (String s)/忽略字母大小写public int compareTo(String anotherString)/比较字符串的大小,实现Comparable接口方法public int compareToIgnoreCase(String str) /比较字符串的大小,忽略字母大小写 程序设计实用教程(第3版)4.3.6 Class类操作类public final class Class public String getName() /返回当前类名字符串public Class getSuperclass(); /返回当前类的父类public Package getPackage() /返回当前类所在的包 System.out.print(this.getClass().getName()+“ “); this.getClass().getSuperclass().getName() this.getClass().getPackage().getName() 程序设计实用教程(第3版)4.3.7 System系统类和 Runtime运行时类public final class System extends Object public final static InputStream in = nullInputStream();public final static PrintStream out = nullPrintStream();public final static PrintStream err = nullPrintStream();public static native viod arraycopy(Object src, int src_pos, Object dst, int dst_pos, int length) /复制数组public static void exit(int status) /结束当前运行的程序public static native long currentTimeMillis();/获得当前日期和时间,返回从1970-1-1 00:00:00开始至当前时 间的累计毫秒数public static Properties getProperties() /获得系统全部属性public static String getProperty(String key) /获得指定系统属性 程序设计实用教程(第3版)Runtime运行时类public class Runtime extends Object public static Runtime getRuntime() /返回与当前应用程序相联系的运行时环境public long totalMemory() /返回系统内存空间总量public long freeMemory() /返回系统内存剩余空间的大小 程序设计实用教程(第3版)4.4 java.util包中的工具类 库n4.4.1 日期类n4.4.2 Arrays数组类n4.4.3 Random随机数序列类 程序设计实用教程(第3版)4.4.1 日期类nDate日期类 public class Date extends Object implements java.io.Serializable, Cloneable, Comparable public Date() /构造方法,获得系统当前日期和时间的Date对象this(System.currentTimeMillis();public Date(long date) /
收藏 下载该资源
网站客服QQ:2055934822
金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号