Java怎样对一个属性设置set或get方法的快捷键

具体步骤如下:
  首页,在testApp.java 类中定义属性,例如:public Sting name;
  其次,Alt+Shift+S,  选择Generate Getters and Setter...这一项,然后如图
  
  

  就能得到

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

 

下面是使用demo

  

public class testApp {
    public String name;
    public int age;
    public String address;
    public String getName() {
        System.out.println("我的名字:"+name);
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public int getAge() {
        System.out.println("我的年龄:"+age);
        return age;
    }
    public void setAge(int age) {
        this.age = age;
    }
    public String getAddress() {
        System.out.println("我的地址:"+address);
        return address;
    }
    public void setAddress(String address) {
        this.address = address;
    }
    
    @Test //测试
    public void adad() {
        System.out.println("\n==时间信息===========");
        testApp a = new testApp();
        this.setName("张三");//赋值
        this.setAge(10);
        this.setAddress("郑州·河南");
        
        this.getAddress();//获取值
        this.getName();
        this.getAge();
    }

    
}

 


  

posted on 2019-09-25 14:56  婧星  阅读(5434)  评论(0编辑  收藏  举报

导航