1. package com.StandardForm;
    2. /**
    3. *
    4. * @author phil616
    5. * This class are written as a document to standard code style.
    6. */
    7. class StandardClass {
    8. /*
    9. * This class are constructed by a rather standard form.
    10. * Here are some precondition:
    11. * 1. I want property 'data' can only initialize when the object constructed.
    12. * 2. I want property 'PI' is a constant value and cannot be changed at all condition.
    13. * */
    14. //There are three properties defined by private and one by public.
    15. private int data;
    16. private String str;
    17. private double number;
    18. public final static double PI = 3.14;
    19. //This constructor is expected to initialize all properties.
    20. StandardClass(){
    21. this.data = 0;
    22. this.number = 0.0;
    23. this.str = null;
    24. }
    25. //This constructor is expected to initialize properties by users.
    26. public StandardClass(int data, String str, double number){
    27. this(); //null parameter constructor can be called before set-methods
    28. setData(data); //set data
    29. setStr(str); //set string
    30. setNumber(number); //set number
    31. }
    32. /*
    33. * If I want to set 'data' only at the beginning, I should set this method to private,
    34. * in this case, once this method called at constructor and set a data, one cannot
    35. * set data anymore because its set method is private.
    36. * */
    37. private void setData(int data){
    38. this.data = data;
    39. }
    40. //set other properties.
    41. public void setStr(String str){
    42. this.str = str;
    43. }
    44. public void setNumber(double number){
    45. this.number = number;
    46. }
    47. /*
    48. * Attention, when we name a method/properties/parameter/local variables,
    49. * lowerCamelCase is recommended, which means that the first letter is lower-case
    50. * and the first letter of next word and other words are upper-case, looks like these:
    51. * getData / setUserName / makeUserInfoByFunction
    52. * and UpperCamelCase is looks like this:
    53. * PublicClass TcpUdpDeal
    54. * */
    55. public int getData(){
    56. return this.data;
    57. }
    58. public String getStr(){
    59. return this.str;
    60. }
    61. public double getNumber(){
    62. return this.number;
    63. }
    64. }

    几个前提条件:
    属性 data 是一个只能在构造时修改的量
    属性 PI 是一个不能被修改的共享常量
    四个属性中,三个属于私有属性不可被外部修改,PI是一个公开但是不能被修改的常量
    且该常量被放置在静态区
    无参构造函数将所有属性初始化或将指针置空
    有参构造将会把传进来的参数通过get方法初始化各个属性
    先调用无参构造

    如果我们想把data设置成只能修改一次的值,那么需要把他的权限设置成私有
    这样的话可以设计成除了构造函数在类内调用一次,没有其他办法访问该set方法

    注意,当我们命名一个方法,属性,参数或是局部变量时,建议使用小写的驼峰命名法
    而类名等使用大写驼峰命名法
    获取单个属性使用get+属性名的方式获取
    set同理,这里的get推荐使用带返回值的类型,由于设计的应用类在设计完成后几乎不会更改,
    因此在设计get函数时应返回值而非打印值,保持功能单一极简以保证代码的健壮性。