SCJP 1.6版考題 050

出自 陳富國維基館
前往: 導覽搜尋
1. public abstract class Shape{
2.   private int x;
3.   private int y;
4.   public abstract void draw();
5.   public void setAnchor(int x, int y){
6.     this.x = x;
7.     this.y = y;
8.   }
9. }


Which two classes use the Shape class correctly? (Choose two.)

 A.
 public class Circle implements Shape{
   private in radius;
 }
 
 B.
 public abstract class Circle extends Shape{
   private in radius;
 }
 C.
 public class Circle extends Shape{
   private in radius;
   public void draw();
 } 
 
 D.
 public abstract class Circle implements Shape{
   private in radius;
   public void draw();
 }
 E.
 public class Circle extends Shape{
   private in radius;
   public void draw(){/* code here */}
 }
 
 F.
 public abstract class Circle implements Shape{
   private in radius;
   public void draw(){/* code here */}
 }

解答


Ans: B E

解說:

一個類別中若有抽象方法(沒有實作),則類別必須為abstract,Circle繼承抽象類別Shape,
若Circle不為抽象類別,必須對繼承自Shape的抽象方法補上實作
使用implements必須針對介面的類別。(A/D/F錯)