Java 2013-2014 期末

Java 2013-2014 期末

1.

1)能够生成java文档的注释命令是(javadoc)。

完整命令是:javadoc -d 文档存放目录 -author -version 源文件名.java

2)若使用命令行:

​ java Add 88 66 33

运行带有main方法的Java程序Add.,则开始运行时,args[1]中存放的内容为( 66 ),args[2]中存放的内容为( 33 )。

3)main方法的声明修饰包括 ( public static void )

4)JVM是( Java Virtual Machine )的缩写。

5)缩写API代表( Application Programming Interface )。


2.请找出代码中的错误并改正

2.1

final class FirstClass {
private  int  a = 1;
int  b = 2;
} 
class SecondClass  extends  FirstClass { 
public void method() {
System.out.println(a + b);  
} 
}

final类不可被继承

私有数据成员子类不可访问


2.2

public  class  TestClass  {
int  n;     
public  static  void  main(string[]  args)	{ 
int i = 10;  
n = i*10;
system.out.println("n= " + n);  
}
}

main方法访问不到类的数据成员n

string应该是String

system应该是System


2.3

public class MyClass {
	int x, y, z;   
	public MyClass() {
		x = 10;
		y = 12;
	}
	public void Calculate() {
		int xx, yy;
		xx = x*x;		yy = y*y;
		z = Math.sqrt(xx+yy);               
		System.out.println("z= " + z);
	}
	public static void main(string[] args) 	{
		new MyClass;             
	} 
}

sqrt()返回double,不能隐式转换成int

String[] args

new MyClass()


3.阅读下列程序,如果methodA()抛出的异常不是IOException异常,该程序的执行结果是

class ExceptionTest{ 
   public static void main(String args[]){ 
     try{ 
        methodA(); 
     }catch(IOException e){
        System.out.println("caught IOException"); 
     }catch(Exception e){ 
        System.out.println("caught Exception"); 
     } 
  } 
} 

caught Exception

在IDEA中只会得到Error:(10, 10) java: 在相应的 try 语句主体中不能抛出异常错误java.io.IOException


4.分析并写出下面程序执行的结果

class Variable {
  int x, y, z;
  public Variable() {
    this(5, 5, 5);
  }
  public Variable(int x, int y, int z) {
    this.x = x;
    this.y = y;
    this.z = z;
  }
  void change(int x, int y) {
    this.x = x;
    this.y = y;
    z = x + y;
    int z = 10;
    System.out.println("x=" + x + " y=" + y + " z=" + z);
  }
}
public class VariableTest {
  public static void main(String args[]) {
    Variable v = new Variable();
    System.out.println("x=" + v.x + " y=" + v.y + " z="+v.z);
    v.change(9, 17);
    System.out.println("x=" + v.x + " y=" + v.y + " z=" + v.z);
  }
}

x= 5 y= 5 z= 5

x= 9 y= 17 z= 10

x= 9 y= 17 z= 26


5.下列程序将从file1.dat文件中读取全部数据,然后写出到file2.dat文件中,请补充完整下列程序代码

import java.io.*;
class filestreamInOut {
  public static void main(String args[])  {
    try {
      File inFile=new File("      1     ");
      File outFile=new File("     2     ");
      FileInputStream fis=new FileInputStream(     3     );
      FileOutputStream fos=new  FileOutputStream(      4      );
      int c;
      while((c=fis.read())!=-1)  {
        fos.write(c);
      }
      fis.close();
          5    ;
    }
    catch(FileNotFoundException e) {
      System.out.println("FileStreamsTest: "+e);
    }
    catch(IOException e) {
      System.err.println("FileStreamsTest: "+e);
    }
  }
}

1.file1.dat

2.file2.dat

3.inFile

4.outFile

5.fos.close()


6

6.1 sleep() 和 wait() 有什么区别?

1、这两个方法来自不同的类分别是Thread和Object,sleep方法属于Thread类中的静态方法,wait属于Object的成员方法。
2、sleep()是线程类(Thread)的方法,不涉及线程通信,调用时会暂停此线程指定的时间,但监控依然保持,不会释放对象锁,到时间自动恢复;wait()是Object的方法,用于线程间的通信,调用时会放弃对象锁,进入等待队列,待调用notify()/notifyAll()唤醒指定的线程或者所有线程,才进入对象锁定池准备获得对象锁进入运行状态。
3、wait,notify和notifyAll只能在同步控制方法或者同步控制块里面使用,而sleep可以在任何地方使用(使用范围)。
4、sleep()方法必须捕获异常InterruptedException,而wait()、notify()以及notifyAll()不需要捕获异常。


6.2 实现多态性有哪两种方式,它们有什么相同点和不同之处?

1、上转型对象实现多态

2、接口回调实现多态

接口回调是指:可以把使用某一接口的类创建的对象的引用赋给该接口声明的接口变量,那么该接口变量就可以调用被类实现的接口的方法。实际上,当接口变量调用被类实现的接口中的方法时,就是通知相应的对象调用接口的方法,这一过程称为对象功能的接口回调。

interface People{
   void peopleList();
}

class Student implements People{
   public void peopleList(){
    System.out.println("I’m a student.")}
}

class Teacher implements People{
  public void peopleList(){
    System.out.println("I’m a teacher.");
}
}

public class Example{
  public static void main(String args[]){
    People a;            
	a=new Student();     
	a.peopleList();
	a=new Teacher();     
	a.peopleList();   
}
}

接口回调父类方法更加的简洁,是抽象方法不需要有方法体;

都是编译时显示父类方法行为特征,运行时显示子类行为特征。

(Thinking in Java)

使用接口的核心原因:为了能够向上转型为多个基类型。即利用接口的多实现,可向上转型为多个接口基类型。

从实现了某接口的对象,得到对此接口的引用,与向上转型为这个对象的基类,实质上效果是一样的。

这两个概念是从两个方面来解释一个行为。接口回调的概念,强调使用接口来实现回调对象方法使用权的功能。而向上转型则牵涉到多态和运行期绑定的范畴。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值