资源描述
10.1
在下面类的定义中,哪个定义了一个合法的抽象类?
public class abstract A{
abstract void unfinished();}
(d)
class A{
abstract void unfinished(){
}}
(a)
(a)
abstract class A{
protected void unfinished();}
(e)
class A{
abstract void unfinished();}
(b)
abstract class A{
abstract int unfinished();}
(f)
abstract class A{
abstract void unfinished();}
(c)
答案:(c)(f)是合法的抽象类;
分析:(a)中的方法虽然声明abstract,但是实现方法体的方法不能为抽象方法;
(d)中的abstract写在class关键字的后面不合法;
(b)声明abstract方法的类必须为抽象类,在class前应该添加abstract;
(e)没有方法体的方法必须声明为抽象方法,在void前应该添加abstract ;
10.2
getArea 和 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;
5
6 /** Construct a default geometric object */
7 protected GeometricObject() {
8 dateCreated = new java.util.Date();
9 }
10
11 /** Return color */
12 public String getColor() {
13 return color;
14 }
15
16 /** Set a new color */
17 public void setColor(String color) {
18 this.color = color;
19 }
20
21 /** Return filled. Since filled is boolean,
22 so, the get method name is isFilled */
23 public boolean isFilled() {
24 return filled;
25 }
26
27 /** Set a new filled */
28 public void setFilled(boolean filled) {
29 this.filled = filled;
30 }
31
32 /** Get dateCreated */
33 public java.util.Date getDateCreated() {
34 return dateCreated;
35 }
36
37 /** Return a string representation of this object */
38 public String toString() {
39 return "created on " + dateCreated + "\ncolor: " + color +
40 " and filled: " + filled;
41 }
42
43 /** Abstract method getArea */
44 public abstract double getArea();
45
46 /** Abstract method getPerimeter */
47 public abstract double getPerimeter();
48 }
10.3
下面哪一个是正确的接口?
abstract interface A extends
I1,I2{
abstract void print(){};}
(c)
interface A{
void print();}
(d)
interface A{
void print(){};}
(a)
abstract interface A{
print();}
(b)
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;
5
6 public House(int id, double area) {
7 this.id = id;
8 this.area = area;
9 whenBuilt = new java.util.Date();
10 }
11
12 public double getId() {
13 return id;
14 }
15
16 public double getArea() {
17 return area;
18 }
19
20 public java.util.Date getWhenBuilt() {
21 return whenBuilt;
22 }
23
24 /** Override the protected clone method defined in the Object
25 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 }
34
35 /** 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 else
42 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 int[10], new String[100], new Object[50], 或new Calendar[20] 赋值给Object[] 类型的变量吗?
10.11
描述基本类型的包装类,为什么需要这些包装类?
int
10.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 Integer[2];
numberArray[0] = 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(pareTo(new Integer(4)));
}
}
10.17
下面代码有什么错误?
public class Test {
public static void main(String[] args) {
Number x = new Integer(3);
System.out.println(x.intValue());
System.out.println((Integer)pareTo(new Integer(4)));
}
}
10.18
下面代码的输出是什么?
public class Test {
public static void main(String[] args) {
java.math.BigInteger x = new java.math.BigInteger("3");
java.math.BigInteger y = new java.math.BigInteger("7");
x.add(y);
System.out.println(x);
}
}
10.19
描述JDK 1.5中装箱和开箱的特点. 在JDK 1.5中,下面的语句正确吗?
Number x = 3;
Integer x = 3;
Double x = 3;
Double x = 3.0;
int x = new Integer(3);
int x = new Integer(3) + new Integer(4);
double y = 3.4;
y.intValue();
JOptionPane.showMessageDialog(null, 45.5);
10.20
给出下列术语的定义: 抽象类abstract class,接口 interfaces. 抽象类和接口的相同与不同之处是什么?
10.21
判断下列语句的对与错:
· 抽象类可以有使用该抽象类构造方法创建的实例.
· 抽象类可以被继承.
· 接口编译成独立的字节码文件.
· 非抽象父类的子类不能是抽象类.
· 子类不能覆盖父类中的具体方法,将其声明成抽象方法.
· 抽象方法必须是非静态的
· 接口可以有静态方法.
· 接口可以继承一个或多个接口.
· 接口可以继承抽象类.
· 抽象类可以继承接口.
展开阅读全文