第04章--接口内部类与Java-API基础.pptVIP

  • 4
  • 0
  • 约1.59万字
  • 约 10页
  • 2018-06-14 发布于福建
  • 举报
第04章--接口内部类与Java-API基础

3. Comparable可比较接口 public interface ComparableT { int compareTo(T o) //比较对象 } 其中,T是Comparable接口的参数,表示一个类。 * MyDate类对象比较大小 public class MyDate implements ComparableMyDate { public int compareTo(MyDate d) //约定比较日期大小的规则,返回-1、0、1 { if (this.year==d.year this.month==d.month this.day==d.day) return 0; return (this.yeard.year || this.year==d.year this.monthd.month || this.year==d.year this.month==d.month this.dayd.day) ? 1 : -1; } } * Comparable 接口举例 public class MyDate implements ComparableMyDate //类声明 {…. public static void main(String args[]) { MyDate dates[]={new MyDate(1995,12,5),new MyDate(2003,2,18),new MyDate(1998,6,18)}; …. Arrays.sort(dates); … } } 4. 基本数据类型的包装类 * 8个Byte、Short、Integer、Long、Float、Double、Character、Boolean。 基本数据类型的包装类 8个Byte、Short、Integer、Long、Float、Double、Character、Boolean。 public final class Integer extends Number implements ComparableInteger { public static final int MIN_VALUE = 0//最小值常量,-231 public static final int MAX_VALUE = 0x7fffffff; //最大值常量,231-1 public 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接口中方法 } * 5. String字符串类 public final class String extends Object implements java.io.Serializable, ComparableString, CharSequence { public String() //构造方法 public String(String original) public String toString() //覆盖Object类中方法 public int length() //返回字符串的长度 public boolean equals(Object obj) //比较字符串是否相等 public boolean equalsIgnoreCase (String s)//忽略字母大小写 pub

您可能关注的文档

文档评论(0)

1亿VIP精品文档

相关文档