2009年2月9日 星期一

第64題

Given:
class A {
String name = "A";
String getName() {
return name;
}
String greeting()
{ return "class A"; }
}
class B extends A {
String name = "B";
String greeting()
{ return "class B"; }
}
public class Client {
public static void main( String[] args ) {
A a = new A();
A b = new B();
System.out.println(a.greeting() + "has name" + a.getName());
System.out.println(b.greeting() + "has name" + b.getName());
}
}

Place the names "A" and "B" in the following output Names
class Place here has name Place here
class Place here has name Place here
Names
A B
答案:

class A has name A
class B has name A


參考:7-9 多型
-------------------------------------------------------------------
{ return "class A"; }
return name;
class A has name A
{ return "class B"; }
沒有getName...所以用父類別class B extends A {
String getName() {
return name;}
class B has name A

沒有留言:

張貼留言