java基础试题

一、选择题

1.关于JAVA中的抽象类方法,下面哪个是正确的?A

I.它不包含方法.

II.它不能被定义为PUBLIC.   

(a) 只有I

(b) 只有II

(c) I 和 II

(d) 没有

 

2.下面对抽象类描述不正确的是(C)
A.抽象类只能作为基类使用。
B.抽象类不能定义对象。
C.抽象类可以实例对象。
D.可以实现多态。

 

3.下列关于接口和抽象类的说法不正确的是?(A )
A)
接口也有构造方法
B) 实现接口时,需对接口中的所有方法都要实现
C) 抽象类也有构造方法
D) 抽象类可以派生出子类

 

4.下列变量定义不合法的是 (C )
A) boolean flag=true; B) int k = 1+
‘a’;
C) char ch = “a”; D) floatradius = 1/2;

 

5.有关类的说法正确的是? (B )
A)
类具有封装性,所以类的数据是不能被访问的
B) 类具有封装性,但可以通过类的公共接口访问类中的数据
C) 声明一个类时,必须用public修饰符
D) 每个类中,必须有main方法,否则程序无法运行

 

6.下列说法正确的是(A

A) 非抽象类不能包含抽象方法

B) 非抽象类可以包含抽象方法

C) 抽象类不能包含抽象方法

D) 任何类都不能包含抽象方法

 

7.下列说法正确的是(B

A) 允许申明没有抽象方法的抽象类

B) 不允许申明没有抽象方法的抽象类

C) 允许申明含有抽象方法的普通类

D) 任何类都可以申明抽象方法

 

8.下列说法不正确的是(A

A) 一个抽象父类的子类可以不实现所有的抽象方法

B) 一个抽象父类的子类必须实现所有的抽象方法

C) 如果一个抽象父类的子类不能实现所有的抽象方法,它必须申明为抽象

D) 如果一个抽象父类的子类可以不为抽象

 

9.下列说法正确的是(A

A) 子类可以申明为抽象的,即使它的父类是具体的

B) 子类必须申明为抽象的,即使它的父类是具体的

C) 子类不能申明为抽象的,即使它的父类是具体的

D) 以上说法都错误

 

10.下列说法正确的是(A

A) 可以用NEW创建抽象类实例

B) 不可以用NEW创建抽象类实例

C) 抽象类不可以用作数据类型

D) 以上说法都错误

 

11.下列说法正确的是(A

A) 接口中数据必须是常量

B) 接口中数据可以不是常量

C) 抽象类不可以有非常量的数据域

D) 以上说法都错误

 

12.下列说法正确的是(B

A) 接口中数据必须是常量

B) 接口中数据可以不是常量

C) 抽象类不可以有非常量的数据域

D) 以上说法都错误

 

13.在下面类的定义中,哪个定义了一个合法的抽象类?C

class A{

  abstract void ufinished(){

  }

}

              (a)

class A{

  abstract void unfinished();

}

               (b)

abstract class A{

  abstract void unfinished();

}

               (c)

public class abstract A{

  abstract void unfinished();

}}abstract class A{

  abstract int unfinished();

}

               (d)

 

14.下面哪一个事正确的接口?D

interface A{

  void print(){};

}

              (a) 

abstract interface A extends I1,I2{

  abstract void print(){};

}

             (c)

 

abstract interface A{

  print();

}

           (b)

interface A{

  void print();

}

 

           (d)

 

15.下列说法错误的有(D

A) Integer i=newInterger(“23”);                    

B) Integer i=newInteger(23)

C) Integeri=Interger.valueOf(“23”)

D) Double d=newDouble();

 

16.下列说法错误的有(B

A) Doubled=Double.valueof(“23.45”)                   

B) inti=(Integer.valueOf(23)).intValue()

C) doubled=(Double.valueOf(“23”)).doubleValue()

D) inti=(Double.valueOf(“23.4”)).intValue()

 

 

17.描述JDK1.5中装箱和开箱的特点。在JDK1.5中,下面的哪个语句不正确吗?(D)

A)Number x=3;    

B)Integer x=3;      

C)Double x=3;      

D) y.intValue();      

 

 

18下列说法错误的有(A

A抽象类可以有使用该抽象类构造方法创建的实例。

B抽象类可以被扩展。  

C) 总可以成功地把子类实例转化成父类的对象。

D) 接口编译成独立的字节码文件。(正确)

 

19.下列说法正确的有(C

A非抽象类的子类不能是抽象类。

B总可以成功地把父类实例转化成子类的对象。  

C) 子类不能覆盖父类中的具体方法,将其声明成抽象方法。。

D) 以上全错

 

20.下列说法正确的有(C

A非抽象类的子类不能是抽象类。

B总可以成功地把父类实例转化成子类的对象。  

C) 子类不能覆盖父类中的具体方法,将其声明成抽象方法。。

D) 以上全错

 

21.下列说法正确的有(A

A一个接口只能扩展其他接口,不能扩展类。

B一个接口除了能扩展其他接口,也能扩展类。  

C) 抽象类可以有具体的方法。

D) 以上全错

 

22.下列数据类型的包装类错误的有(A

Abyte->Byte

Bshort->Short  

C) int->Int

D) long->Long

 

23.下列数据类型的包装类错误的有(A

Afloat->Float

Bdouble->Double  

C)char>Char

D) boolean->Boolean

 

24.下列说法错误的有(D

A一个接口可以扩展一个或多个接口。

B一个类仅能继承一个父类。  

C) 即使父类具体,子类也可以是抽象的。

D) 以上全错

25.下面哪个定义的是合法类(C)

A)class A { abstract void unfinished() { } }

B) class A { abstract void unfinished(); }

C) abstract class A { abstract void unfinished(); }

D)public class abstract A { abstract voidunfinished(); }

 

26.下列说法申明抽象方法的有(B

Apublicabstract method()

Bpublicabstract void method()。  

C) public voidabstract Method()

D) public abstractvoid method() {}

 

27.A是一个抽象类,B是一个具体实例,下面正确的是(B)

AA a = newA();

BA a = newB();。  

C)  B b = new A()。

D)  B b = new B()

 

28.假设Calendar calendar = newGregorianCalendar(),下面哪个返回一年中的月份(BD

A.calendar.get(Calendar.MONTH)

 

B.calendar.get(Calendar.MONTH_OF_YEAR)

 

C.calendar.get(Calendar.WEEK_OF_MONTH)

 

D.calendar.get(Calendar.WEEK_OF_YEAR)

 

29.假设Calendar calendar = newGregorianCalendar(),下面哪个返回一年中的星期(A

A. A.calendar.get(Calendar.MONTH)

 

B.calendar.get(Calendar.MONTH_OF_YEAR)

 

 C. calendar.get(Calendar.WEEK_OF_MONTH)

 

 D. calendar.get(Calendar.WEEK_OF_YEAR)

 

30.假设Calendar calendar = newGregorianCalendar(),下面哪个返回一个月有多少天(D

A.calendar.get(Calendar.MONTH)

 

 B. calendar.get(Calendar.MONTH_OF_YEAR)

 

 C. calendar.get(Calendar.WEEK_OF_MONTH)

 

 D. calendar.get(Calendar.WEEK_OF_YEAR)

 

31.下面哪个定义的是正确的接口(D

A)  interface A {void print() { }; }

B)  abstractinterface A { print(); }

C)  abstractinterface A { abstract void print() { };}

D)  interface A {void print();}

 

32.下面程序的运行结果是(D

interface A {
}

class C {
}

class B extends D implements A {
}

public class Test extends Thread {
   public static void main(String[] args) {
     B b = new B();
     if (b instanceof A)
       System.out.println("b is an instanceof A");
     if (b instanceof C)
       System.out.println("b is an instanceof C");
   }
}

class D extends C {
}

A)什么都没有

B)b is an instance of A.

C)b is an instance of C.

D)b is an instance of A followed by b is aninstance of C

 

33.下面哪个是从String 转换为Double(D)

A)d = Double.parseDouble(s);

B)d = (new Double(s)).doubleValue();

C)d = Double.valueOf(s).doubleValue();

D)All of the above

 

34.下面哪个是从Double转换为String(A)

A)s = (new Double(d)).toString();

B)s = (Double.valueOf(s)).toString();

C)s = new Double(d).stringOf();

D)s = String.stringOf(d)

 

35. Integer.parseInt("10", 2)输出是什么(B

A)1

B)2

C)10

D)无效的申明

 

36.下面的程序输出什么(A

public classTest {
   public static void main(String[] args) {
     java.math.BigInteger x = newjava.math.BigInteger("3");
     java.math.BigInteger y = newjava.math.BigInteger("7");
     x.add(y);
     System.out.println(x);
   }
}

A)3

B)4

C)10

D)11

 

37.判断下面代码正误(A

public class Test {
   public static void main(String[] args) {
     int x = new Integer(3);
     System.out.println(x.intValue());
     System.out.println((Integer)x.compareTo(newInteger(4)));
   }
}

A)错误,X不能转为整数

B)错误,Integer不能转为int

C)错误,int不能转为Integer

D)正确

 

38.下面说法正确的是(D

A)以下全错

B)抽象类不可以具有数据和方法

C)常规类也可以包含抽象方法

D)抽象类和常规类一样具有数据和方法

 

39. 下面说法正确的是(C

A)抽象类不可以被多次继承

B)抽象类可以继承多个类

C)抽象类可以被多次继承

D)以上说法全错

 

40. 下面说法正确的是(D

A)接口中所有数据域都是PUBLIC

B)接口中所有数据域都是FINAL

C)接口中所有数据域都是STATIC

D)以上说法全对

 

41. 下面说法正确的是(A

A)接口中所有方法都是PUBLIC ABSTRACT

B)接口中所有方法都是PUBLIC

C)接口中所有方法都是PRIVATE

D)接口中所有方法都是PROTECTED

 

42.接口如何继承其他接口(B

A)使用EXTENDS

B) 使用EXTEND

C)使用IMPLEMENTS

D)使用IMPLEMENT

 

43.接口如何实现其他接口(C

A)使用EXTENDS

B) 使用EXTEND

C)使用IMPLEMENTS

D)使用IMPLEMENT

 

44.抽象类如何继承其他抽象类(A

A)使用EXTENDS

B) 使用EXTEND

C)使用IMPLEMENTS

D)使用IMPLEMENT

 

45.下列说法正确的是(B

A)一个类可以有多个父类,也能有多个接口

B)一个类只能有一个父类,但能有多个接口

C)一个类只能有一个父类和一个接口

D)一个类可以有多个父类,但只能有一个接口

 

46.接口中所有方法的默认修饰是什么(D

A)private

B)privateabstract

C)public

D)public abstract

 

47.下列说法正确的是(A

A)接口可以继承其他接口,抽象类也可以继承其他抽象类

B) 接口不可以继承其他接口,抽象类也可以继承其他抽象类

C)接口可以继承其他接口,抽象类不可以继承其他抽象类

D)接口不可以继承其他接口,抽象类也不可以继承其他抽象类

 

48.写列说法正确的是(D

A)一个抽象类中可以含有Data Fields,接口不可以

B) 一个抽象类中不可以含有Data Fields,接口也不可以

C)一个抽象类中可以含有Data Fields,接口也可以

D)一个抽象类中可以含有Data Fields,接口也可以

 

49.下列关于抽象类和接口的说的法有误的是(D

A)接口的好处是将实现与表现相分离

B)抽象类与接口都用于抽象

C)抽象类可以有自己部分实现

D)以上说法全错

 

50.关于抽象类和接口的异同,下列说法错误的是(A

A)实现程度相同

B)接口是抽象类的一个特列

C)接口中只申明方法,不实现

D)抽象类可以实现,也可以不实现

 

 

二、填空题

1、含有抽象方法的类必须(声明为抽象类)

2、一个类能有(一)个父类,可以有(多)个接口.

3、在接口中所有方法的修饰默认是( public abstract)..

4、抽象方法只有(方法头标志),而没有(实现)

5、包含抽象方法的类必须(声明为抽象类)

6、在一个由抽象类扩展出来的非抽象类中,必须实现(所有)抽象方法。

7、接口是一种与类相似的结构,只包含(常量)和(抽象方法)

8、体为空的接口称为(标记接口)。

9、关键字native表明这个方法(不是用Java编写的),而是在java虚拟机中(为本地平台实现的)

10、在java中,(基本数据)类型不作为对象使用,这样做的原因是效率问题

11、Java提供了一个方便的方法,将基本数据类型并入对象 或包装成对象,对应的类称为(包装类)

12、每个数值包装类都实现了抽象方法(doubleValue)、(intValue)、(longValue)、(shortValue)和(floatValue),它们都定义在(Number)类中。

13、把数值字符串转换为Int值,使用(Integer)类中的(pareInt)方法

14、Integer、Double、Character和String都实现了接口(Comparable),所以这些类的对象都可以使用(CompareTo)方法进行比较

15、将基本类型的值转换为包装类对象,称为(装箱(boxing))相反的过程称为(开箱(unboxing))

16、一个接口可以扩展(一或多)个接口

17、byte的包装类是(Byte),short的是Short,int 的是(Integer),long的是Long,float的是Float,double的是(Double),char的是Character,boolean的是(Boolean).

18、非抽象类(不能)包含抽象方法。

19、在接口中,所有的数据域的修饰符都是(public final static)

.20、抽象类的子类(覆盖父类的所有抽象方法),否则的话该子类也应该为抽象类

 

三、简答题

1描述基本类型的包装类,为什么需要这些包装类?

答:在java中,基本数据类型不作为对象使用,这样做的原因是效率问题。因为处理对象需要额外的系统开销,如果将基本类型处理为对象,就会给语言的效率带来大量负面影响。然而,许多java中的方法需要对象作为参数。Java提供了一个方便的方法,将基本数据类型并入对象或包装成对象。对应的类称为包装类。

 

2.可以在类中定义compareTo方法而不实现Comparable接口,实现Compareble接口的好处是什么?

答:通用编程;实现接口即类似于一种“多重继承”;

 

3.给出下面术语的定义:抽象类、接口。抽象类和接口的相同和不同之处是什么?

     答:抽象类:没有具体的实例,无法实现多重继承,可以有非抽象方法。

        接口:只包含常量和抽象方法。

        相同点:没有具体的实例,可以编译为独立的字节码

        不同点:抽象类除了包含常量和抽象方法外,还包含变量和具体的方法

4.什么是抽象类?

答:抽象类表示该类中可能已经有一些方法的具体定义,但是接口就仅仅只能定义各个方法的界面(方法名,参数列表,返回类型),并不关心具体细节。

5.抽象类的用法?

答:1)在继承抽象类时,必须覆盖该类中的每一个抽象方法,而每个已实现的方法必须和抽象类中指定的方法一样,接收相同数目和类型的参数,具有同样的返回值,这一点与接口相同。

  2)当父类已有实际功能的方法时,该方法在子类中可以不必实现,直接引用的方法,子类也可以重写该父类的方法(继承的概念)。

  3)而实现 (implement)一个接口(interface)的时候,是一定要实现接口中所定义的所有方法,而不可遗漏任何一个。

  4)另外,抽象类不能产生对象的,但可以由它的实现类来声明对象。

  有鉴于此,在实现接口时,我们也常写一个抽象类,来实现接口中的某些子类所需的通用方法,接着在编写各个子类时,即可继承该抽象类来使用,省去在每个都要实现通用的方法的困扰。

6.介绍1个你熟悉的接口?

答:略

 

7.引入接口的原因?

答:在Java中引进接口主要是为了解决多继承的问题。 
1)接口中不能有非抽象方法,但抽象类中可以有。 
2)一个类能实现多个接口,但只能有一个父类。 
3)接口并不属于继承结构,它实际与继承无关,因此无关的类也可以实现同一个接口。 

8、下面的代码有什么错误?

Public class Test

{

  Publicstatic void main(String [] agrs)

{

  GeometricObject x=new Circle(3);

  GeometricObject y=x..clone();

  System.out.println(x==y);

}

}

答:其中clone()方法位Object类中的保护方法,如果想用clone方法,可以(1)在类中重写clone方法;(2)实现java.lang.Cloneable接口。

 

9.如何把整数转换为字符串?如何把数值字符串转换为整数?如何把double数转换为字符串?如何把数值字符串转换为double数?

答:用newInterger(int).toString()方法将整数转化为字符串;Integer.pareInt(String)方法将数值字符串转换为整数;Double(double).toString()方法将double数转换为字符串;Double.pareDouble(String)方法将数值字符串转换为double 数;

 

10、接口如何使用?

答:在AWT中,java使用了大量的监听器。这些监听器都是回调方法。在XML解析器SAX中,也使用了回调方法来解析XML文档。 

接口要比抽象类还要抽象。抽象类需要子类继承,而Java是单继承,所以抽象类被限制了。而接口不同,一个类可以实现多个接口。好比人类与程序员类之间的关系。可以说程序员是人类的子类,如果程序员是一个接口。用人类的子类来实现它就会更好。这个子类还可以去实现会计接口、音乐家接口等等。 

 

四、是非题

1、 抽象类可以使用该抽象类的构造方法创建的实例         (错)

2、 抽象类可以被扩展                                 (对)

3、 总可以成功地把子类的实例转化为父类的实例         (对)

4、 总可以成功地把父类的实例转化为子类的实例           (错)

5、 接口编译成独立的字节码文件                      (对)

6、 非抽象父类的子类不能是抽象类               (错)

7、 子类不能覆盖父类中的具体方法,将其声明成抽象方法           (错)

8、 抽象类不能用new运算符实例化,但仍然可以定义它的构造方法  (对)

9、 有抽象方法的类不一定是抽象类    (错)

10、             接口是一种与类相似的结构,包含的常量、变量和抽象方法  (错)

11、             接口中每个方法只有头标记,没有实现部分    (对)

12、             JAVA对类的扩展允许多重继承    (错)

13、             所有的类共享同一个根:Object,但接口没有共同的根  (对)

14、             每一个包装类都是从抽象类Number扩展而来的   (对)

15、             一个接口只能扩展一个接口   (错)

16、             Number numberRef=newInterger[2] 不会导致运行时错误   (错)

17、             Java.lang.Cloneable是一个标记接口,使得它的每个类的对象都课克隆  (对)

18、             Java.util.Arrays.sort(Object[])对可比较对象数组进行排序    (对)

19、             每个数值包装类都实现了抽象方法doubleValue   (对)

20、             所有包装类的实例都是永久的,一旦对象创建后它们内部的值就不能再改变  (对)

 

五、编程题

1.创建一个ComparableCylinder的类,它扩展Cylider类并实现Comparable接口。实现compareTo方法,使其根据体积比较两个圆柱。编写测试程序找出两个ComparableCylinder对象中的较大者。

答:public classExercise9_2 {

  //Main method

 public static void main(String[] args) {

   // Create two comarable rects

   ComparableCylinder cylinder1 = new ComparableCylinder(2, 5);

   ComparableCylinder cylinder2 = new ComparableCylinder(3, 4);

 

   // Display the max rect

   ComparableCylinder rect = (ComparableCylinder)Max.max(cylinder1,cylinder2);

   System.out.println("The max rect's width and height are " +

     rect.getWidth() + " and " + rect.getHeight());

   System.out.println(rect);

  }

}

 

// Rectangle1.java: The Rectangle1 classthat extends GeometricObject

class Rectangle1 extends GeometricObject1 {

 protected double width;

 protected double height;

 

  //Default constructor

 public Rectangle1() {

   this(1.0, 1.0, "white", 1.0);

  }

 

  // Construct a rectangle with specified widthand height

 public Rectangle1(double width, double height) {

   this.width = width;

   this.height = height;

  }

 

  //Construct a rectangle with specified width, height, weight, and color

 public Rectangle1(double width, double height,

   String color, double weight) {

   super(color, weight);

   this.width = width;

   this.height = height;

  }

 

  //Getter method for width

 public double getWidth() {

   return width;

  }

 

  //Setter method for width

 public void setWidth(double width) {

   this.width = width;

  }

 

  //Getter method for height

 public double getHeight() {

   return height;

  }

 

  //Setter method for height

 public void getHeight(double height) {

   this.height = height;

  }

 

  //Implement the findArea method in GeometricObject

 public double findArea() {

   return width*height;

  }

 

  //Implement the findPerimeter method in GeometricObject

 public double findPerimeter() {

   return 2*(width + height);

  }

 

 public int compareTo(Object object) {

   if (this.findArea() > ((Rectangle)object).findArea())

     return 1;

   else if (this.findArea() == ((Rectangle)object).findArea())

     return 0;

   else

     return -1;

  }

 

 /**Override the equals() method defined in the Object class*/

 public boolean equals(Rectangle1 rectangle) {

   return (width == rectangle.getWidth()) &&

     (height == rectangle.getHeight());

  }

 

  //Override the toString() method defined in the Object class

 public String toString() {

   return "[Rectangle1] width = " + width + " and height =" + height;

  }

}

 

class ComparableCylinder extends Rectangle1implements Comparable {

  //Construct a CompareCircle with specified radius

 public ComparableCylinder(double w, double h) {

    super(w, h);

  }

 

  //Implement the compareTo method defined in Comparable

 public int compareTo(Object o) {

   if (findArea() > ((Rectangle1)o).findArea())

     return 1;

   else if (findArea() < ((Rectangle1)o).findArea())

     return -1;

   else

     return 0;

  }

}

 

2.编写一个方法,求数组中所有几何对象的面积之和。方法头如下:

  Publicstatic double sumArea(GeometricObject[] a)

  编写测试程序,创建三个对象(一个圆、一个圆柱和一个矩形)的数组,并使用sumArea方法求它们的总面积。

答:public classExercise9_8 {

  public staticvoid main(String[] args) {

    newExercise9_8();

  }

 

  publicExercise9_8() {

   GeometricObject[] a = {new Circle(5), new Cylinder(4, 5),

      newRectangle(2, 3)};

 

   System.out.println("The total area is " + sumArea(a));

  }

 

  public staticdouble sumArea(GeometricObject[] a) {

    double sum =0;

 

    for (int i =0; i < a.length; i++)

      sum +=a[i].findArea();

 

    return sum;

  }

 

  //Rectangle.java: The Rectangle class that extends GeometricObject

public class Rectangle extends GeometricObject {

  private doublewidth;

  private doubleheight;

 

  /**Defaultconstructor*/

  publicRectangle() {

    this(1.0,1.0);

  }

 

  /**Construct arectangle with width and height*/

  publicRectangle(double width, double height) {

    this(width,height, "white", false);

  }

 

  /**Construct arectangle with specified width, height,

     filled, andcolor*/

  publicRectangle(double width, double height,

    String color,boolean filled) {

    super(color,filled);

    this.width =width;

    this.height =height;

  }

 

  /**Returnwidth*/

  public doublegetWidth() {

    return width;

  }

 

  /**Set a newwidth*/

  public voidsetWidth(double width) {

    this.width =width;

  }

 

  /**Returnheight*/

  public doublegetHeight() {

    return height;

  }

 

  /**Set a newheight*/

  public voidsetHeight(double height) {

    this.height =height;

  }

 

  /**Implement thefindArea method in GeometricObject*/

  public doublefindArea() {

    returnwidth*height;

  }

 

  /**Implement thefindPerimeter method in GeometricObject*/

  public doublefindPerimeter() {

    return2*(width + height);

  }

 

  /**Override theequals method defined in the Object class*/

  public booleanequals(Rectangle rectangle) {

    return (width== rectangle.getWidth()) &&

      (height ==rectangle.getHeight());

  }

 

  /**Override thetoString method defined in the Object class*/

  public StringtoString() {

    return"[Rectangle] width = " + width +

      " andheight = " + height;

  }

}

}

 3.写4个类

一个测试类PersonTest.java

一个抽象类Person.java

一个继承Person类的类Employee.java

一个继承Person类的类student.java

答:类PersonTest.java

importjava.text.*;
public class PersonTest
{
   public static void main(String [] args)
   {
      Person [] people=new Person[2];
      people[0]=new Employee("张西门",6000);
      people[1]=new student("李东门","计算机应用");
      for(int i=0;i<people.length;i++)
      {
         Person e =people[i];
        System.out.println(e.getname()+", "+e.getJieShao());
      }
   }
}

类Person.java

abstract classPerson
{
    public Person(String aName)
    {
        name=aName;
    }
  
    public abstract String getJieShao();
  
    public String getname()
    {
        return name;
    }

     private String name;
}

类Employee.java

class Employeeextends Person
{
  public Employee(String aName,double aGongZi)
  {
    super(aName);
    GongZi=aGongZi;
  }
  
  public String getJieShao()
  {
    return "职工"+name+"的月工资为:"+GongZi;
  }
  
  private double GongZi;
}

类student.java

class studentextends Person
{
  public student(String aName,String aZhuanYe)
  {
    super(aName);
    ZhuanYe=aZhuanYe;
  }
  
  public String getJieShao()
  {
    return "学生"+name+"的专业是:"+ZhuanYe;
  }
  
  private String ZhuanYe;
}

 

4. 在某个包中创建一个接口,内含3个方法,然后在另一个包中实现此接口

// interfaces.TestEx5.java

// TIJ4 Chapter Interfaces, Exercise 5, page 320

// Create an interface containing three methods, inits own package. Implement it

// in another package.

/* Solution includes, in package interfaces.ex5:

* package interfaces.ex5;

*

* public interface Ex5 {

*   voidsayOne();

*   voidsayTwo();

*   voidsayThree();

* }

*/

import interfaces.ex5.*;

 

public class TestEx5 implements Ex5 {

    public voidsayOne() { System.out.println("one"); }

    public voidsayTwo() { System.out.println("two"); }

    public voidsayThree() { System.out.println("three"); }

    public staticvoid main(String[] args) {

        TestEx5 x= new TestEx5();

        x.sayOne();

        x.sayTwo();

        x.sayThree();

    }

}

 

 

5.证明接口内所有方法都自动是PUBLIC

 

 //interfaces.TestEx6.java

// TIJ4 Chapter Interfaces, Exercise 6, page 320

// Prove that all the methods in an interface areautomatically public

/* Solution includes, in package interfaces.ex6:

* package interfaces.ex6;

*

* interface Ex6 {

*   voidsayOne();

*   voidsayTwo();

*   voidsayThree();

* }

*

* public class Ex6b implements Ex6 {

*   // Error:cannot assign weaker access to public methods:

*   // voidsayOne() { System.out.println("one"); } // implies package *                            // access

*   // protectedvoid sayTwo() { System.out.println("two"); }

*   // privatevoid sayThree() { System.out.println("three"); }

*   // must bemaintained public:

*   public voidsayOne() { System.out.println("one"); }

*   public voidsayTwo() { System.out.println("two"); }

*   public voidsayThree() { System.out.println("three"); }

* }

*/

import interfaces.ex6.*;

 

public class TestEx6 {

    public staticvoid main(String[] args) {

        Ex6b x =new Ex6b();

        x.sayOne();

        x.sayTwo();

        x.sayThree();

    }

}

6.证明在接口中的域隐式地是STATIC,FINAL

 //interfaces/Ex17.java

// TIJ4 Chapter Interfaces, Exercise 17, page 335

// Prove that the fields in an interface areimplicitly static and final.

 

interface Days {

    int SUNDAY =1, MONDAY = 2, TUESDAY = 3, WEDNESDAY = 4,

        THURSDAY= 5, FRIDAY = 6, SATURDAY = 7;

}

 

class Week implements Days {

    privatestatic int count = 0;

    private intid = count++;

    public Week(){ System.out.println("Week() " + id); }

}

 

public class Ex17 {

    public staticvoid main(String[] args) {

        //Without any objects, static fields exist:

        System.out.println("SUNDAY= " + Days.SUNDAY);

        System.out.println("MONDAY= " + Days.MONDAY);

        Week w0 =new Week();

        Week w1 =new Week();

        // Error:cannot assign a value to final variable SUNDAY:

        //w.SUNDAY = 2;

        // Error:cannot assign a value to final variable MONDAY:

        //w1.MONDAY = w0.MONDAY;

    }

}

7.创建一个不包含任何抽象方法的抽象类,并验证我们不能为该类创建任何实例

 

// interfaces/Ex2.java

// TIJ4 Chapter Interfaces, Exercise 2, page 315

// Create a class as abstract without including anyabstract methods, and verify

// that you cannot create any instances of that class.

 

abstract class Nogo1 {

    Nogo1() {System.out.println("Nogo1()"); }

}

 

abstract class Nogo2 {}

 

class Go1 extends Nogo1 {

    Go1() {System.out.println("Go1()"); }

}

 

public class Ex2 {

    public staticvoid main(String[] args) {

        // Nogo1and Nogo2 cannot be instantiated:

        // Nogo1ng1 = new Nogo1();

        // Nogo2ng2 = new Nogo2();

        // ButNogo1() constructor called during instatiation of child:

        Go1 g1 =new Go1();

    }

}

8.创建一个基类,让它包含抽象方法PRINT(),并在导出类中覆盖该方法,覆盖后的方法版本可以打印导出类中定义的某个整形变量的值,在定义该变量之处,赋予它非零值,在基类的构造器中调用这个方法,现在,在MAIN()中,创建一个导出类对象,然后调用它的PRINT()方法。

// interfaces/Ex3.java

// TIJ4 Chapter Interfaces, Exercise 3, page 315

/* Create a base class with an abstract print() methodthat is overridden in a

* derived class. The overridden version of the methodprints the value of an int

* variable defined in the derived class. At the pointof definition of this

* variable, give it a nonzero value. In the base-classconstructor, call this

* method. In main(), create an object of the drivedtype, and then call its

* print() method. Explain the results.

*/

import static org.greggordon.tools.Print.*;

 

abstract class Dad {

    protectedabstract void print();

    Dad() {print(); }

}

 

class Son extends Dad {

    private int i= 1;

    @Overrideprotected void print() { println("Son.i = " + i); }

}

 

public class Ex3 {

    public staticvoid main(String[] args) {

        /*Process of initialization:

        * 1.Storage for Son s allocated and initialized to binary zero

        * 2.Dad() called

        * 3.Son.print() called in Dad() (s.i = 0)

        * 4.Member initializers called in order (s.i = 1)

        * 5. Bodyof Son() called

        */

        Son s =new Son();

        s.print();

    }

}

 

  • 0
    点赞
  • 15
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值