标签:style blog class code c java
class Point{ public double x; public double y; }
对于可变的类来说,应该用包含私有域和公有设值方法的类来代替:
class Point{ private double x; private double y; Point(double x, double y) { this.x = x; this.y = y; } double getX() { return x; } void setX(double x) { this.x = x; } double getY() { return y; } void setY(double y) { this.y = y; } }
总之,公有类永远都不应该暴露可变的域。
14 在公有类中使用访问方法而非公有域,布布扣,bubuko.com
标签:style blog class code c java
原文地址:http://www.cnblogs.com/lsf90/p/3735484.html