简体   繁体   English

Java与JavaScript getter和setter方法

[英]Java vs JavaScript getters and setters methods

Sometimes when i have some object with lots of attributes (for example 30-40) it is really anoying to write getter and setter methods so in javascript i do something like this: 有时,当我有一些具有很多属性的对象(例如30-40)时,编写getter和setter方法确实很烦人,因此在javascript中我会执行以下操作:

function SomeObject( properties ) 
{ 
   // Iterate through the properties of the object, and make sure 
   // that it's properly scoped.
   for ( var i in properties ) 
   { 
       (function(){ 
        // Create a new getter for the property 
        this[ "get" + i ] = function() 
        { 
           return properties[i]; 
        };
        // Create a new setter for the property 
        this[ "set" + i ] = function(val) 
        { 
           properties[i] = val; 
        }; 
    })(); }
 }

So i am just wondering if it is possible to do something like this in JAVA? 所以我只是想知道是否有可能在JAVA中做这样的事情?

Code-generating annotations can do this sort of thing in Java. 生成代码的注释可以在Java中完成此类操作。 You might want to take a look at Project Lombok . 您可能想看看Lombok项目

Related, I think: Using Java Annotations - Generating Code 相关,我认为: 使用Java注释-生成代码

In Eclipse: 在Eclipse中:

Rightclick -> Source -> Generate Getters and Setters 右键单击->源->生成Getter和Setter

There are also other nice generators, for example, for constructors, hashCode/Equals. 还有其他漂亮的生成器,例如,用于构造函数的hashCode / Equals。

All common IDEs have such a functionality, which saves alot of time. 所有常见的IDE都具有这种功能,可以节省大量时间。

Suppose if i give you a class with 30-40 getters/setters. 假设我给您一个30-40个吸气器/吸气器的课程。 How would you feel while using. 使用时感觉如何。 I don't think its a good idea to have 30-40 getter/setters in a single class. 我认为在一个班级里有30-40个吸气剂/吸气剂不是一个好主意。 Rather you break/distribute class by subclassing properties 而是通过子类化属性来中断/分发类

The example you gave from javascript is avery good example. 您从javascript给出的示例非常好。 On how we should access if we so many properties in a single class. 如果我们在一个类中有这么多属性,应该如何访问。 In javascript you can tread a object like a Map. 在javascript中,您可以踩踏类似地图的对象。

I would apply the same idea if i have the requirement of 30/40 getter setters. 如果我有30/40 getter setter的要求,我会采用相同的想法。

ie Either i would use java.util.Properties 即要么我将使用java.util.Properties

or made My Custom Class 或进行我的自定义课程

  //Only if you have 30/40 properties
  class MyClass {

       private Map<String, Object> data = new ConcurrentHashMap<String,Object>();

       public void set(String fieldName, Object value) {
               data.put(fieldName, value);
       }

       public Object get(String fieldName) {
               return data.get(fieldName);
       }

   }

在Eclipse中,您可以使用函数“ Source> Getter and Setter”,并自动将您想要的所有setter和getter添加到您的代码中!

Maybe try ascepects and Spring Roo like : 也许像下面这样尝试ascepects和Spring Roo:

@RooJavaBean
public calss SimpleClass {
    private Attr1 attr1;
    private Attr2 attr2;

} 

Tutor 导师

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM