资源预览内容
第1页 / 共6页
第2页 / 共6页
第3页 / 共6页
第4页 / 共6页
第5页 / 共6页
第6页 / 共6页
亲,该文档总共6页全部预览完了,如果喜欢就下载吧!
资源描述
10.1在下面类的定义中,哪个定义了一个合法的抽象类? (a)答案:(c)(f)是合法的抽象类;分析:(a)中的方法虽然声明 abstract,但是实现方法体的方法不能为抽象方法;(d)中的 abstract 写在 class 关键字的后面不合法;(b)声明 abstract 方法的类必须为抽象类,在 class 前应该添加 abstract;(e)没有方法体的方法必须声明为抽象方法,在 void 前应该添加 abstract ;10.2getArea 和 getPerimeter 方法可以从 GeometricObject 类中删除. 在 GeometricObject 类中定义 getArea 和 getPerimeter 方法为抽象方法的好处是什么? 1 public abstract class GeometricObject 2 private String color = white;3 private boolean filled;4 private java.util.Date dateCreated;56 /* Construct a default geometric object */7 protected GeometricObject() 8 dateCreated = new java.util.Date();9 1011 /* Return color */12 public String getColor() 13 return color;14 class Aabstract void unfinished()(a)class Aabstract void unfinished();(b)abstract class Aabstract void unfinished();(c)public class abstract Aabstract void unfinished();(d)abstract class Aprotected void unfinished();(e)abstract class Aabstract int unfinished();(f)1516 /* Set a new color */17 public void setColor(String color) 18 this.color = color;19 2021 /* Return filled. Since filled is boolean,22 so, the get method name is isFilled */23 public boolean isFilled() 24 return filled;25 2627 /* Set a new filled */28 public void setFilled(boolean filled) 29 this.filled = filled;30 3132 /* Get dateCreated */33 public java.util.Date getDateCreated() 34 return dateCreated;35 3637 /* Return a string representation of this object */38 public String toString() 39 return created on + dateCreated + ncolor: + color +40 and filled: + filled;41 4243 /* Abstract method getArea */44 public abstract double getArea();4546 /* Abstract method getPerimeter */47 public abstract double getPerimeter();48 10.3下面哪一个是正确的接口? 10.4如下程序定义了两个 max 方法,解释为什么签名 max(Comparable, Comparable)的方法优于签名 max(Object, Object)的方法. 下面的语句正确吗?String s = Max.max(abc, efg);Date date = Max.max(new Date(), new Date();10.5可以在类中定义 compareTo 方法而不去实现 Comparable 接口.实现Comparable 接口的好处是什么 ? 10.6如下程序清单中,1 public class House implements Cloneable, Comparable 2 private int id;3 private double area;4 private java.util.Date whenBuilt;56 public House(int id, double area) 7 this.id = id;8 this.area = area;9 whenBuilt = new java.util.Date();10 1112 public double getId() 13 return id;14 1516 public double getArea() 17 return area;18 1920 public java.util.Date getWhenBuilt() 21 return whenBuilt;22 2324 /* Override the protected clone method defined in the interface Avoid print();(a)abstract interface Aprint();(b)abstract interface A extendsI1,I2abstract void print();(c)interface Avoid print();(d)Object25 class, and strengthen its accessibility */26 public Object clone() 27 try 28 return super.clone();29 30 catch (CloneNotSupportedException ex) 31 return null;32 33 3435 /* Implement the compareTo method defined in Comparable */36 public int compareTo(Object o) 37 if (area (House)o).area)38 return 1;39 else if (area (House)o).area)40 return -1;41 else42 return 0;43 44 如果 House 类没有覆盖 clone() 方法或者 House 没有实现 java.lang.Cloneable 接口,将会出现什么问题?10.7下面代码的输出是什么? java.util.Date date = new java.util.Date();java.util.Date date1 = (java.util.Date)(date.clone();System.out.println(date = date1);System.out.println(date.equals(date1);10.8下面代码的输出是什么? java.util.ArrayList list = new java.util.ArrayList();list.add(New York); list.add(new java.util.Date();java.util.ArrayList list1 =(java.util.ArrayList)(list.clone();System.out.println(list = list1);System.out.println(list.get(0) = list1.get(0);System.out.println(list.get(1) = list1.get(1);10.9下面代码有什么错误? public class Test public static void main(String args) GeometricObject x = new Circle(3); GeometricObject y = x.clone();System.out.println(x = y);10.10可以把 new int10, new String100, new Object50, 或 new Calendar20 赋值给 Object 类型的变量吗? 10.11描述基本类型的包装类,为什么需要这些包装类?int10.12下列语句正确吗? Integer i = new Integer(23);Integer i = new Integer(23);Integer i = Integer.valueOf(23);Integer i = Integer.parseInt(23, 8);Double d = new Double();Double d = Double.valueOf(23.45);int i = (Integer.valueOf(23).intValue();double d = (Double.valueOf(23.4).doubleValue();int i = (Double.valueOf(23.4).intValue();String s = (Double.valueOf(23.4).toString();10.13如何把整数转换为字符串? 如何把数值字符串转换为整数?如何把double 数转换为字符串?如何把数值字符串转换为 double 数?10.14为什么下面两行代码可以编译,但是会导致运行错误?Number numberRef = new Integer(0);Double doubleRef = (Double)numberRef;10.15为什么下面两行代码可以编译,但是会导致运行错误?Number numberArray = new Integer2;numberArray0 = new Double(1.5);10.16下面代码有什么错误?public class Test public static void main(String args) Number x = new Integer(3);System.out.println(x.intValue();System.out.println(x.comp
收藏 下载该资源
网站客服QQ:2055934822
金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号