3、程序编译能通过,输出结果为“动物叫!”和“汪汪……”,因为在程序中调用shout()方法时,首先会通过super.shout()调用父类的方法说出“动物叫!”之后再输出“汪汪……”
4、程序编译不通过,因为接口中定义的方法不能有方法体,所以定义的eat()方法是错误的。接口中的方法必须在子类中全部实现,由于run()方法在子类中并没有重新实现,所以这也是错误的。
五、简答题
1、在继承关系中,子类的方法与父类的某一方法具有相同的方法名、返回类型和参数列表,则称子类的该方法重写(覆盖)父类的方法。
2、多态意味着一个对象有着多种形态,可以在特定的情况下,表现不同的状态,从而对应着不同的属性和方法。简单的说,多态就是使用父类类型的变量引用子类对象,根据被引用子类对象的特性,程序会得到不同的运行效果。
3、在Java中,使用abstract关键字修饰的类称之为抽象类。抽象类是不能被实例化的,通常需要写一个子类来继承抽象类,同时实例化子类来获得该类的对象。抽象类通常用于表示一种抽象的概念。 接口可以说是一种特殊的抽象类,接口中只能定义常量和抽象方法。由于接口的特殊性,在定义时需要使用interface关键字。
六、编程题
1、参考答案
class Student { }
class UnderGraduate extends Student{ }
public class Test01{
public static void main(String[] args) { }
Student student = new Student(\student.show();
UnderGraduate underGraduate = new UnderGraduate(%underGraduate.show(); public String degree;
public UnderGraduate(String name,int age,String degree){ }
public void show(){ }
System.out.println(\super(name, age); this.degree=degree; public String name; public int age;
public Student(String name,int age){ }
public void show(){ }
System.out.println(\this.name=name; this.age=age;
}
2、参考答案
interface Shape { }
class Square implements Shape{ }
class Circle implements Shape{ }
public class Test02 { }
public static void main(String[] args) { }
Shape square = new Square(); Shape circle = new Circle();
System.out.println(square.area(2)); System.out.println(circle.area(3)); public double area(double r) { }
return Math.PI*r*r;
public double area(double sideLength) { }
return sideLength*sideLength;
double area(double givenValue);
3、参考答案
class NoThisSongException extends Exception{ }
class Player{ }
public class Test03 {
public static void main(String[] args) {
Player player = new Player(); try {
public void play(int index)throws NoThisSongException{ }
if(index>10){ }
System.out.println(\正在播放歌曲\
throw new NoThisSongException(\您播放的歌曲不存在\
public NoThisSongException(){ }
public NoThisSongException(String message){ }
super(message); super();
}
}
player.play(13);
} catch (NoThisSongException e) { }
System.out.println(\异常信息为: \
第5章 多线程
一、填空题
1、 线程、通信
2、 Thread、Runnable 3、 就绪
4、 synchronized、对象、this 5、 进程 6、 新建状态(New)、就绪状态(Runnable)、运行状态(Running)、阻塞状态(Blocked)、死亡状态(Terminated) 7、 10、1
8、 开启一个新线程、run()方法 9、 wait()、notify()、notifyAll() 10、setDaemon(true)、start() 二、判断题
1、错 2、对 3、对 4、错 5、错 三、选择题
1、B 2、AC 3、ABC 4、BC 5、ABD 6、ABC 7、C 8、D 9、AB 10、ABCD 四、程序分析题
1、程序不能编译通过,因为RunHandler类没有实现Runnable接口,因此RunHandler的实例对象不能作为参数传递给Thread的构造方法。
2、程序不能编译通过,因为Thread的子类A重写的run()方法的访问级别不能低于父类run()方法的。访问级别
3、程序不能编译通过,因为同步方法中调用wait()方法的对象必须为同步锁对象。 4、t.start(); 五、简答题
1、一种是继承java.lang包下的Thread类,覆写Thread类的run()方法,在run()方法中实现运行在线程上的代码。
new Thread() {
public void run(){} }.start();
另一种就是实现java.lang.Runnable接口,同样是在run()方法中实现运行在线程上的代码。
new Thread(new Runnable(){ public void run(){} }).start()
2、调用sleep()方法,正在执行的线程主动让出CPU去执行其他线程,在sleep()方法指定的时间过后,
CPU才会回到这个线程上继续往下执行,如果当前线程进入了同步锁,sleep()方法并不会释放锁,即使当前线程使用sleep()方法让出了CPU,但其它被同步锁挡住了的线程也无法得到执行。wait()
在一个已经进入了同步锁的线程内进行调用,让当前线程暂时让出同步锁,以便其它正在等待此锁的线程可以得到同步锁并运行。当其它线程调用了notify()方法后,调用wait()方法的线程就会解除wait状态,当再次获得同步锁后,程序可以继续向下执行。
六、编程题
1、参考答案
public class MyThread extends Thread{
public MyThread(String name) { }
public void run() { }
public static void main(String[] args) { }
new MyThread(\new MyThread(\
super(name);
System.out.println(this.getName());
}
2、参考答案
public class MyRunnable implements Runnable { }
public void run() { }
public static void main(String[] args) { }
new Thread(new MyRunnable()).start(); for (int i = 0; i < 100; i++) { }
System.out.println(\for (int i = 0; i < 50; i++) { }
System.out.println(\
3、参考答案
public class Test01 { }
class Teacher implements Runnable {
private int notes = 80; public void run() {
while (true) {
public static void main(String[] args) { }
Teacher t = new Teacher(); new Thread(t, \陈老师\new Thread(t, \高老师\new Thread(t, \李老师\
}
}
}
dispatchNotes(); // 调用售票方法 if (notes <= 0) { }
break;
private synchronized void dispatchNotes() { }
if (notes > 0) { }
try { }
System.out.println(Thread.currentThread().getName() + \发出的笔记\
+ notes--);
Thread.sleep(10); // 经过的线程休眠10毫秒 e.printStackTrace();
} catch (InterruptedException e) {
4、参考答案
public class Accumulator extends Thread {
private int stratNum; public static int sum;
public Accumulator(int startNum) { }
public static synchronized void add(int num) { }
public void run() { }
public static void main(String[] args) throws Exception {
Thread[] threadList = new Thread[10]; for (int i = 0; i < 10; i++) { }
for (int i = 0; i < 10; i++) { }
threadList[i].join();
threadList[i] = new Accumulator(10 * i + 1); threadList[i].start(); int sum = 0;
for (int i = 0; i < 10; i++) { }
add(sum);
sum += stratNum + i; sum += num;
this.stratNum = startNum;