封装

1.  封装:隐藏类内部细节
2.  步骤:
         第一步:将属性私有化.(private)
         第二步:提供getter/setter方法(getXxxx(),setXxxx()).
         第三步:在getter/setter中加入控制语句.

3.  this关键词
       this:表示当前对象.
             调用属性:this.属性名
             调用方法:this.方法名();
       this():表示调用构造函数.
       注意:this(),必需写在构造函数的第一行.

package 第二章;

public class Tm {
    private int th;
    private String tm;
    private String xx;
    private String xx1;
    private String xx2;
    private String xx3;
    
    public Tm(){
        
    }

    public int getTh() {
        return th;
    }

    public void setTh(int th) {
        this.th = th;
    }

    public String getTm() {
        return tm;
    }

    public void setTm(String tm) {
        this.tm = tm;
    }

    public String getXx() {
        return xx;
    }

    public void setXx(String xx) {
        this.xx = xx;
    }

    public String getXx1() {
        return xx1;
    }

    public void setXx1(String xx1) {
        this.xx1 = xx1;
    }

    public String getXx2() {
        return xx2;
    }

    public void setXx2(String xx2) {
        this.xx2 = xx2;
    }

    public String getXx3() {
        return xx3;
    }

    public void setXx3(String xx3) {
        this.xx3 = xx3;
    }
    public void print(){
        System.out.println(th+tm+xx+xx1+xx2+xx3);
    }
}
View Code
原文地址:https://www.cnblogs.com/liumeilin/p/7018604.html