package com.StandardForm;/**** @author phil616* This class are written as a document to standard code style.*/class StandardClass {/** This class are constructed by a rather standard form.* Here are some precondition:* 1. I want property 'data' can only initialize when the object constructed.* 2. I want property 'PI' is a constant value and cannot be changed at all condition.* *///There are three properties defined by private and one by public.private int data;private String str;private double number;public final static double PI = 3.14;//This constructor is expected to initialize all properties.StandardClass(){this.data = 0;this.number = 0.0;this.str = null;}//This constructor is expected to initialize properties by users.public StandardClass(int data, String str, double number){this(); //null parameter constructor can be called before set-methodssetData(data); //set datasetStr(str); //set stringsetNumber(number); //set number}/** If I want to set 'data' only at the beginning, I should set this method to private,* in this case, once this method called at constructor and set a data, one cannot* set data anymore because its set method is private.* */private void setData(int data){this.data = data;}//set other properties.public void setStr(String str){this.str = str;}public void setNumber(double number){this.number = number;}/** Attention, when we name a method/properties/parameter/local variables,* lowerCamelCase is recommended, which means that the first letter is lower-case* and the first letter of next word and other words are upper-case, looks like these:* getData / setUserName / makeUserInfoByFunction* and UpperCamelCase is looks like this:* PublicClass TcpUdpDeal* */public int getData(){return this.data;}public String getStr(){return this.str;}public double getNumber(){return this.number;}}
几个前提条件:
属性 data 是一个只能在构造时修改的量
属性 PI 是一个不能被修改的共享常量
四个属性中,三个属于私有属性不可被外部修改,PI是一个公开但是不能被修改的常量
且该常量被放置在静态区
无参构造函数将所有属性初始化或将指针置空
有参构造将会把传进来的参数通过get方法初始化各个属性
先调用无参构造
如果我们想把data设置成只能修改一次的值,那么需要把他的权限设置成私有
这样的话可以设计成除了构造函数在类内调用一次,没有其他办法访问该set方法
注意,当我们命名一个方法,属性,参数或是局部变量时,建议使用小写的驼峰命名法
而类名等使用大写驼峰命名法
获取单个属性使用get+属性名的方式获取
set同理,这里的get推荐使用带返回值的类型,由于设计的应用类在设计完成后几乎不会更改,
因此在设计get函数时应返回值而非打印值,保持功能单一极简以保证代码的健壮性。
