close

一.題目

Given this class: 

Which two changes would encapsulate this class and ensure that the area field is always equal to length * height whenever the Rectangle class is used?

(哪兩個改變可以封裝這個類別和確保面積變數不論什麼情況下被使用,都會等於長度乘以高度呢?)

A. Call the setArea method at the end of the setHeight method. (在設高度方法的最後呼叫設面積方法)

B. Call the setArea method at the beginning of the setHeight method. (在設高度方法的開始呼叫設面積方法)

C. Call the setArea method at the end of the setLength method. (在設長度方法的最後呼叫設面積方法)

D. Call the setArea method at the beginning of the setLength method. (在設長度方法的開始呼叫設面積方法)

E. Change the setArea method to private. (設面積方法改成私有化)

F. Change the area field to public. (面積變數改成公開)

 

 

二.答案

AE

 

 

三.解析

  1. 因為在  setLength( )方法裡面時,變數height 還是 0,所以必須放在setHeight( )裡面,面積才會等於長度 * 高度
  2. 原本 setArea( )是 public,所有人都可以任意使用,所以用 private封裝起來。
arrow
arrow
    文章標籤
    Java OCA 1Z0-808 封裝
    全站熱搜
    創作者介紹
    創作者 書童 的頭像
    書童

    小書童

    書童 發表在 痞客邦 留言(0) 人氣()