Cat is-a Animal
Cat is-a Jumper
Dog is-a Animal
Dog is-a Jumper
Cat has-a Animal
Beagle has-a Tail
Beagle has-a Jumper
第1题:
interface Playable {
void play();
}
interface Bounceable {
void play();
}
interface Rollable extends Playable, Bounceable {
Ball ball = new Ball("PingPang");
}
class Ball implements Rollable {
private String name;
public String getName() {
return name;
}
public Ball(String name) {
this.name = name;
}
public void play() {
ball = new Ball("Football");
System.out.println(ball.getName());
}
}
这个错误不容易发现。
错。"interface Rollable extends Playable, Bounceable"没有问题。interface 可继承多个
interfaces,所以这里没错。问题出在interface Rollable 里的"Ball ball = new Ball("PingPang");"。
任何在interface 里声明的interface variable (接口变量,也可称成员变量),默认为public static
final。也就是说"Ball ball = new Ball("PingPang");"实际上是"public static final Ball ball = new
Ball("PingPang");"。在Ball 类的Play()方法中,"ball = new Ball("Football");"改变了ball 的
reference,而这里的ball 来自Rollable interface,Rollable interface 里的ball 是public static final
的,final 的object 是不能被改变reference 的。因此编译器将在"ball = new Ball("Football");"
这里显示有错。
第2题:
class A{
void P1(){cout<<"A111"};
void P2(){cout<<"A222"};
};
class B:public A{
void P1(){cout<<"B111"};
virtual void P2(){cout<<"B222"};
}
..........
A*cl=NULL;
cl=new A;
A->P1();
A->P2();
delete cl;
cl=NULL;
cl=new B;
A->P1();
A->P2();
delete cl;
cl=NULL;
..........
写出运行结果;
第3题:
A.
B.
C.
D.
E.
F.
第4题:
Which two are valid declarations within an interface definition?()
第5题:
public interface A { String DEFAULT_GREETING = “Hello World”; public void method1(); } A programmer wants to create an interface called B that has A as its parent. Which interface declaration is correct?()
第6题:
使用jdbc 编写class EnrollmentImpl 实现接口 Enrollment:
public interface Enrollment{
public void createStudent(Student student) throws Exception;
pubic void createCourse(Course course) throws Exception;
public void enroll(Student student, Course course) throws Exception;
public void exam(Student, Course course, float grade) throws Exception;
}
第7题:
10. public class ClassA { 11. public void count(int i) { 12. count(++i); 13. } 14. } And: 20. ClassA a = new ClassA(); 21. a.count(3); Which exception or error should be thrown by the virtual machine?()
第8题:
interface A{
int x = 0;
}
class B{
int x =1;
}
class C extends B implements A {
public void pX(){
System.out.println(x);
}
public static void main(String[] args) {
new C().pX();
}
}
错误。在编译时会发生错误(错误描述不同的JVM 有不同的信息,意思就是未明确的
x 调用,两个x 都匹配(就象在同时import java.util 和java.sql 两个包时直接声明Date 一样)。
对于父类的变量,可以用super.x 来明确,而接口的属性默认隐含为 public static final.所以可
以通过A.x 来明确。
第9题:
10. interface Jumper { public void jump(); } ...... 20. class Animal {} ...... 30. class Dog extends Animal { 31. Tail tail; 32. } ...... 40. class Beagle extends Dog implements Jumper { 41. public void jump() { } 42. } ....... 50. class Cat implements Jumper { 51. public void jump() { } 52. } Which three are true?()
第10题:
interface Data { public void load(); } abstract class Info { public abstract void load(); } Which class correctly uses the Data interface and Info class?()