加载中…
个人资料
活到老学到老
活到老学到老
  • 博客等级:
  • 博客积分:0
  • 博客访问:1,588
  • 关注人气:21
  • 获赠金笔:0支
  • 赠出金笔:0支
  • 荣誉徽章:
正文 字体大小:

知识扩充--几个练习题

(2011-10-21 10:34:39)
标签:

杂谈

分类: 第四章类、对象和接口

上机练习

²      基本题

1 方法调用匹配测试

class  methodMatch{

    double x=5,y=6;

    void test( double y ){

      System.out.println(y);

      test(this);  //转换匹配哪个方法

     }

   public String toString(){

      return x+","+y;

   }

   void  test( String  a){

      System.out.println(a);

     }

   void  test( Object  a){

      System.out.println(a);

     }

  public static void main(String args[]) {

     methodMatch  a=new  methodMatch ();

     a.test(8); 

     a.test("hello"); 

   }

 }

分析以上程序运行结果,删除test(String)方法后结果如何?加入一个test(int)方法执行结果又如何?最后,总结方法调用的匹配原则。

2定义一个接口,其中包含一个display()方法用于显示信息;通知类、汽车类、广告类均要实现该接口显示“通知内容”,“汽车油量”,“广告消息”。试编程实现并测试该模型。创建的对象用接口引用,并执行接口的display()方法。

3) 将以下程序补充完整

public class Java_3{
      public static void main(String[] args){
        Person p = new Student("王甜甜", "计算机科学");
        System.out.println(p.getName() + ", "+ p.getDescription());
      }
    }
    abstract class Person{
       public Person(String n){
         name = n;
       }
      //*********Found********
      public ____________ String getDescription();
        public String getName(){
        return name;
      }
      private String name;
     }
     //*********Found********
     class Student _____________ Person{
        public Student(String n, String m){
          super(n);
          major = m;
        }
      //*********Found********
       public String _______________(){
          return "学生专业是:" + major;
       }
      private String major;
   }

 提高题

1 分别下面程序中x的访问权限进行修改,测试访问许可。修改包括:private,protected,public以及无访问控制符4种情形。

程序1:同一类情形(文件accessTest.java)

package test

  class  accessTest{

    static int x=8;   

    public static void main(String args[]) {

       System.out.println(x);

    }

 }

程序2:同一包情形(文件samePackage.java)

package test

  class  accessTest{

static int x=8;   

}

    class samePackage {

    public static void main(String args[]) {

       Sysstem.out.println(accessTest .x);

    }

 }

程序3:不同包的其他类情形

  文件1:accessTest.java

package test;

  public class  accessTest{

static int x=8;   

}

  文件2:anotherPackage.java

    import test.*;

    class anotherPackage {

    public static void main(String args[]) {

       Sysstem.out.println(accessTest .x);

    }

 }

程序4:不同包的子类情形

  文件1:accessTest.java

package test;

  public class  accessTest{

static int x=8;   

}

  文件2: subclass.java

    import test.*;

    class   subclass  extends  accessTest {

    public static void main(String args[]) {

       Sysstem.out.println(accessTest .x);

    }

  }

    总结各种访问修饰符的作用。

 

思考题

1)以下程序调试结果为:

class Base{
   Base(){
      int  i = 100;    
      System.out.print (i);
   }
 }
 public class Pri extends Base{
    static int i = 200;
    public static void main(String argv[]){
         Pri p = new Pri();
         System.out.print(i);
    }
 }
 A.编译错误    B.200   C.100200    D.100

(2) 以下程序调试结果为:

 public class Test {
    int m=5;
    public  void some(int x) {
        m=x;
    }
    public static void main(String args []) {
        new Demo().some(7);
    }
}
class Demo extends Test {
    int m=8;
    public  void some(int x) {
        super.some(x);
       System.out.println(m);
    }
}
A.5    B.8   C.7   D.无任何输出  E.编译错误

3)  试完成下述程序片段:

public class Point()
int x,y;
    public  Point(int x,int y)
        =x;      =y;
     }
     ......
 }
A. Point.x   Point.y     B.无解   C. x1    y1   D.this.x    this.y

(4)考虑如下类:

1. class Test(int i) {

2.    void test(int i) {

3.       System.out.println("I am an int.");

4.     }

5.    void test(String s) {

6.       System.out.println("I am a string.");

7.    }

8.

9.    public static void main(String args[]) {

10.     Test t=new Test();

11.     char ch="y";

12.     t.test(ch);

13.   }

14. }

以下哪条为真?

A.行 5 不能通过编译,方法不能被覆盖.

B.行 12 不能通过编译, 因为没有一个test()方法含字符参数.

C.代码可以编译但在12行将出现异常.

D.代码可以编译且产生如下输出: I am an int.

E.代码可以编译且产生如下输出: I am a String.

(5) 类Test1定义如下:

1.public  class  Test1{

2.        public  float  aMethod(float  a,float  b){   }

3.       

4.}

将以下哪种方法插入行3是不合法的。(          

A.public  float  aMethod(float  a, float  b,float  c){  }

B.public  float  aMethod(float  c,float d){  }

C.public  int  aMethod(int  a, int b){  }

D.private float  aMethod(int a,int b,int c){  }

6)考虑如下代码:

class Tree{}

class Pine extends Tree{}

class Oak extends Tree{}

public class Forest {

public static void main( String[] args ) {

      Tree tree = new Pine();

      if( tree instanceof Pine )

          System.out.println( "Pine" );

      if( tree instanceof Tree )

          System.out.println( "Tree" );

      if( tree instanceof Oak )

       System.out.println( "Oak" );

      else
System.out.println( "Oops" );

   }

}

则输出结果中有哪些?

A.Pine   B.Tree   C.Forest   D.Oops   E.无输出

7)以下程序的编译和运行结果为?

abstract class Base{
    abstract public void myfunc();
    public void another(){
        System.out.println("Another method");
    }
}
public class Abs extends Base{
    public static void main(String argv[]){
        Abs a = new Abs();
        a.amethod();
     }
    public void myfunc(){
         System.out.println("My Func");
     }
    public void amethod(){
        myfunc();     
     }
}

A.输出结果为 My Func
B.编译指示 Base 类中无抽象方法
C.编译通过,但运行时指示Base 类中无抽象方法
D.编译指示Base 类中的myfunc方法无方法体,没谁会喜欢该方法。

8) 以下程序的调试结果为?

class Base{

 

public final void amethod(){

        System.out.println("amethod");

        }

 

}

 

public class Fin extends Base{

 

   public static void main(String argv[]){

 

        Base b = new Base();

 

        b.amethod();

 

   }

 

}

A.编译指示带有final 方法的类自己必须定义为final
B.编译指示不能继承含有final 方法的类
C.运行错误,原因是Base类没有定义为final类
D.运行输出 amethod

9) 在同一目录编译和运行以下两文件结果如何?

//文件 P1.java

 

package MyPackage;

 

class P1{

 

    void afancymethod(){

 

        System.out.println("What a fancy method");

 

    }

 

}

 

//文件 P2.java

 

public class P2 extends P1{

 

    public static void main(String argv[]){

 

        P2 p2 = new P2();

 

        p2.afancymethod();

 

    }

 

}

A.两个均通过编译,P2运行时输出 What a fancy method
B.没一个通过编译
C.两个均通过编译,但P2运行时出错
D.P1 通过编译,但P2出现编译错误

10)以下程序的调试结果为?

public class Outer{

 

public String name = "Outer";

 

public static void main(String argv[]){

 

     Inner i = new Inner();

 

     i.showName();

 

}

 

private class Inner{

 

    String name =new String("Inner");

 

    void showName(){

 

          System.out.println(name);

 

    }

 

  }

}

A.输出结果 Outer
B.输出结果 Inner
C.编译错误,因Inner类定义为私有访问
D.在创建Inner类实例的行出现编译错误

11) 设有如下代码:

class Base{}

 

public class MyCast extends Base{

 

    static boolean b1=false;

 

    static int i = -1;

 

    static double d = 10.1;

 

    public static void main(String argv[]){

 

        MyCast m = new MyCast();

 

        Base b = new Base();

 

        //Here

 

    }

 

}

 

则在 //Here处插入哪个代码将不出现编译和运行错误。

 

A.b=m;   B.m=b;   C.d =i;   D.b1 =i;

12)  设有如下代码:

interface IFace{}

 

class CFace implements IFace{}

 

class Base{}

 

public class ObRef extends Base{

 

    public static void main(String argv[]){

 

        ObRef obj = new ObRef();

 

        Base b = new Base();

 

        Object obj1 = new Object();

 

        IFace obj2 = new CFace();

//Here

 

    }

 

}

则在 //Here处插入哪个代码将不出现编译和运行错误。

A.obj1=obj2;   B.b=obj;   C.obj=b;  D.obj1=b;

13) 设有类定义如下:

class Base{

 

public Base(int i){}

}

public class MyOver extends Base{

 

public static void main(String arg[]){

 

     MyOver m = new MyOver(10);

 

}

 

MyOver(int i){

 

     super(i);

 

}

 

MyOver(String s, int i){

 

      this(i);

 

      //Here

 

}

}

以下哪条语句可以安排在//Here处 ?

A.MyOver m = new MyOver();
B.super();
C.this("Hello",10);
D.Base b = new Base(10);

14) 设有类定义如下:

class InOut{
String s= new String("Between");
public void amethod(final int iArgs){
    int iam;
    class Bicycle{
        public void sayHello(){
            //Here
        }
     }
 }
 public void another(){
     int iOther;
 }

}

以下哪些语句可以安排在//Here处 ?

A. System.out.println(s);

B.System.out.println(iOther);

C. System.out.println(iam);

D.     System.out.println(iArgs);

0

阅读 收藏 喜欢 打印举报/Report
  

新浪BLOG意见反馈留言板 欢迎批评指正

新浪简介 | About Sina | 广告服务 | 联系我们 | 招聘信息 | 网站律师 | SINA English | 产品答疑

新浪公司 版权所有