[問題] 存取修飾子
請問一下一題scjp的考題
Insert six modifiers into the code such that it meets all of these
requirements:
1.It must be possible to create instances of Alpha and Beta from outside the
packages in which they are defined.
2.When an object of type Alpha (or any potential subclass of Alpha) has been
created, the instance variable alpha may never be changed.
3.The value of the instrance variable alpha must always be "A" for objects of
type Alpha.
package alpha;
________ class Alpha
{
________ String alpha;
________ Alpha() {this("A");}
________ Alpha(String a) {alpha=a;}
}
package beta;
________ class Beta extends alpha.Alpha
{
________ Beta(String a) {super(a);}
}
________可填的選項有private,protected,public
我自己的答案是
第一個提示是要說Alpha和Beta都要能在package外使用,所以這兩個
class都要public。
第二個提示是說Alpha中的alpha建立後就不能更動,第三個提示是說alpha
一定要是A,所以alpha應該要使用final修飾子,其他都用public
所以我的答案是:public,final,public,public,public,public
請問這樣對嗎?謝謝..
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 203.73.98.93
推
12/03 09:54, , 1F
12/03 09:54, 1F
推
12/03 11:13, , 2F
12/03 11:13, 2F
推
12/03 14:01, , 3F
12/03 14:01, 3F
推
12/04 01:07, , 4F
12/04 01:07, 4F
推
12/04 16:02, , 5F
12/04 16:02, 5F
推
12/05 21:54, , 6F
12/05 21:54, 6F