????class??java??class??е?????????
????object??java??object??е?????????????????????
???????????????????????????????????????????????????????
?????????????????????????????????????????????????????????
????????????????????
???????????????????????????????????????????????????????????÷???????????getter???????????????????????????????κη???????????????????????e.g.Integer??Float????String???????????????
??????????????(accessor method)????????????????????????e.g.getter()??????
?????????????(mutator method)???????????????????????e.g.setter()??????
??????????????????
????????????????????????????????????????????
???????????????field?????Щ??????????????????final??????????г??????
??????????????????????????final???Σ????????????????????????????????????????????????????????????????á?
??????????????????????final??????????????????????????????????
?????????????????????????????Щ???????????????????
????????????????????????????final?????????????????????????????????????????????????????á???????????£????????????
????1.??Щ????????????Щ???????е?????
????2.???????Щ???÷?????????????????????磬??????????????????????????????????????Щ???????????????е??????
????3.?????????????????????????????????????????????????????????????????????????????????????
???????????????????i.e. getter?????????????????????i.e.setter??????
????????????????????????????????????μ????????????????????????????????????????
????????????final?????????????????У???????????????????????????????????????
????Java API?в???????????
??????????????????String??????????????????????????String??????е??????
??????????Stirng???г????????????????private????Щ?????????????????????????????????????
????trim concat substring ????????String??????????String???????????Щ????????????????????????????μ????
????string????????final???????κ????????У??????????????
????????????????????
??????????????????????????????ImmutableCircle??
//ImmutableCircle.java
// Point is a mutable class
class Point {
private int xPos?? yPos;
public Point(int x?? int y) {
xPos = x;
yPos = y;
}
public String toString() {
return "x = " + xPos + "?? y = " + yPos;
}
int getX() { return xPos; }
int getY() { return yPos; }
}
// ImmutableCircle is an immutable class – the state of its objects
// cannot be modified once the object is created
public final class ImmutableCircle {
private final Point center;
private final int radius;
public ImmutableCircle(int x?? int y?? int r) {
center = new Point(x?? y);
radius = r;
}
public String toString() {
return "center: " + center + " and radius = " + radius;
}
public int getRadius() {
return radius;
}
public Point getCenter() {
// return a copy of the object to avoid
// the value of center changed from code outside the class
return new Point(center.getX()?? center.getY());
}
public static void main(String []s) {
System.out.println(new ImmutableCircle(10?? 10?? 20));
}
// other members are elided ...
}
????????????????????????
????center: x = 10?? y = 10 and radius = 20
????????????????????????????????
????· ???????????final???????????У???????????????????
????· ????????????????final????????е?
????· ??????????center??????????????????????????????getter?????У?????????point????????
?????????????????????????
?????????????????????????????????????????????????????????????????????????????????????????????
????????????????
?????????????????в?????????????????????????????е?????????????????????????????磬???????????У???ε???getcenter????????????????????point??????????????????????????????Σ????????point?????????????????????????????????????????????immutablecircle????????
????String????????ó????ж??????????????????String????trim??concat??????????????е???substring????????????????μ????String????????java?????Stringbuffer??Stringbuilder???????????String??????????????????????????????????????????????????????String?????Stringbuffer/Stringbuilder??
??????????μ??????仰????????????????????????????????????????