Eclipse如何生成setter和getter?

时间:2014-10-20 04:49:07

标签: java eclipse reflection javabeans

很想知道Eclipse如何创建setter和getter(而不是我自己)。我确信它必须是Java反射,但只是想寻求更多信息。

2 个答案:

答案 0 :(得分:2)

Eclipse实际上嵌入了一个完整的编译器,允许它进行渐进式编译,并在代码中出现语法错误时仍然提供有用的信息。生成器查看代码的抽象语法树(AST)表示,标识字段,并使用JavaBeans公式为它们创建getter和setter。您可以使用Eclipse中的“Outline”视图来查看Eclipse的类模型的图形表示。

答案 1 :(得分:-1)

step1:

A simple User.java, with 4 fields, but no getters and setters method.

 public class user{

 private String firstname();
 private  String lastname();
 private String username();
 private String password();

 }
  step2: Generate It

 Right click on the file, select “Source” –> “Generate Getters and Setters…”

-> Choose which field you want to generate, and click on “OK” button.

 step3:

 All selected getters and setters methods will be generated automatically.


   public class user{

    private String firstname();
     private  String lastname();
    private String username();
     private String password();
   public String getFirstName(){
   return firstname;
  }
   public void setFirstName(String firstname){
   this.firstname=firstname;
   }
    public String getFirstName(){
   return lastname;
  }
   public void setLastName(String lastname){
   this.lastname=lastname;
   }
    public String getUserName(){
   return username;
  }
   public void setUserName(String username){
   this.username=username;
   }
     public String getPasword(){
   return password;
  }
   public void setPassword(String password){
   this.password=password;
   }
   }