get and set method

The get and set method is used to modify the private(私有数据 )。

If a data has been setting as a private , then it cannt be called by other classes or changed.

So we need the get and set method to call the private data.

package test;

public class Test7 {
    private String name;
    
    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }





    public static void main(String[] args) {
        // TODO 自动生成的方法存根
        Test7 test = new Test7();
        test.setName("mike");
        System.out.println(test.getName());
    }

}

 

posted @ 2016-05-18 12:48  包大人2  阅读(102)  评论(0)    收藏  举报