wangyinxu

java封装性之private

public class TestDemo{

   public static void main(String args[]){

      Person perA= new Person();

      perA.setName("王强");

      perA.setAge(30);

      perA.tell();

}

}

class Person{

    private String name;                                           //private 属性封装,权限只能在本类中使用,因此要想在其它类中使用必须使用setter、getter方法

    private int age;

    public void setName(String n){                          //setter 语法

                name=n;

}

    public void setAge(int a){

       if(a>=0 && a <=250){                              //过滤检查合法性

                age=a;

}

}

   public String getName(){                                    //getter 语法

     return name;

  public int getAge(){

     return age;

}

}

发表于 2017-01-05 09:47  跳动de手指  阅读(111)  评论(0编辑  收藏  举报
 

分类:

技术点:

相关文章:

  • 2021-11-20
  • 2021-09-28
  • 2021-12-21
  • 2021-11-28
  • 2021-06-10
  • 2022-12-23
  • 2022-12-23
  • 2021-05-21
猜你喜欢
  • 2021-12-21
  • 2021-11-29
  • 2021-12-23
  • 2021-10-02
  • 2022-02-15
  • 2021-08-15
  • 2021-06-11
相关资源
相似解决方案