简体   繁体   English

什么是Python的属性()的Java等价物?

[英]What is the Java Equivalent of Python's property()?

I'm new to Java, and I'd like to create some class variables that are dynamically calculated when accessed, as you can do in Python by using the property() method. 我是Java的新手,我想创建一些在访问时动态计算的类变量,就像在Python中使用property()方法一样。 However, I'm not really sure how to describe this, so Googling shows me lots about the Java "Property" class, but this doesn't appear to be the same thing. 但是,我不确定如何描述这个,所以谷歌搜索向我展示了很多关于Java“Property”类的内容,但这看起来并不是一回事。 What is the Java equivalent of Python's property()? 什么是Java的属性()的Java等价物?

There's no such facility built into Java language. Java语言中没有这样的工具。 You have to write all the getters and setters explicitly by yourself. 你必须自己明确地编写所有的getter和setter。 IDEs like Eclipse can generate this boilerplate code for you though. 像Eclipse这样的IDE可以为您生成这个样板代码。

For example : 例如 :

class Point{
  private int x, y;

  public Point(int x, int y){
    this.x = x;
    this.y = y;
  }

  public void setX(int x){
    this.x = x;
  }

  public int getX(){
    return x;
  }

  public void setY(int y){
    this.y = y;
  }

  public int getY(){
    return y;
  }
}

You might want to have a look at Project Lombok which provides the annotations @Getter and @Setter that are somewhat similar to Python's property . 您可能想看一下Project Lombok ,它提供的注释@Getter@Setter有点类似于Python的property

With Lombok, the above example reduces to : 使用Lombok,上面的示例简化为:

class Point{
  @Getter @Setter private int x, y;

  public Point(int x, int y){
    this.x = x;
    this.y = y;
  }
}

the built-in property() function does exactly the opposite of what's described here in the answers. 内置的property()函数与答案中描述的完全相反。 it's not about generating getters and setters for member variables. 它不是为成员变量生成getter和setter。 it just allows you to call a method by accessing a property (so, although you just access a variable in te Python class, a function will be called). 它只允许您通过访问属性来调用方法(因此,尽管您只是访问Python类中的变量,但将调用一个函数)。 ( this post ecplains how and why to use it.) 这篇文章说明了如何以及为何使用它。)
this said, Java doesn't offer anything like this. 这就是说,Java不提供这样的东西。 even more, property access is discouraged in Java. 更重要的是,在Java中不鼓励属性访问。 i guess you could do it in Groovy script language and the meta magic though. 我想你可以用Groovy脚本语言和元魔术来做到这一点。 but i don't know out of my head how to do this. 但我不知道如何做到这一点。

They don't really exist. 他们并不存在。 In Java it's common practice to declare members as private or protected and only allow access to them via methods. 在Java中,通常的做法是将成员声明为privateprotected并且只允许通过方法访问它们。 Often this leads to lots of small getFoo() and setFoo(newFoo) methods. 通常这会导致许多小的getFoo()setFoo(newFoo)方法。 Python doesn't really have private and protected and it's more common to allow direct access to members. Python实际上没有privateprotected ,允许直接访问成员更常见。

As others have noted, java has getters and setters, but there is no strict analogon. 正如其他人所指出的那样,java有getter和setter,但没有严格的类比。 There is a third party library called Project Lombok tha uses annotation to generate the getters and setters in the .class files at comile time. 有一个名为Project Lombok的第三方库使用注释在comile时生成.class文件中的getter和setter。 This could be used to make things a little less verbose. 这可以用来使事情变得不那么冗长。

Do you want to create new fields/getters/setters in the class? 你想在课堂上创建新的字段/ getter / setter吗? If you want to do this in runtime, you have to create completely new class with your fields and methods, and load it into the JVM. 如果要在运行时执行此操作,则必须使用字段和方法创建全新的类,并将其加载到JVM中。 To create new class you can use library like ASM or CGLib, but if you're new to Java, this isn't something you want to start with. 要创建新类,您可以使用像ASM或CGLib这样的库,但如果您是Java新手,那么这不是您想要开始的。

Actually you may simulate this behavior in Java. 实际上你可以在Java中模拟这种行为。

WARNING: ugly solution below 警告:下面的丑陋解决方案

You can write a method in an utility class like the code below: 您可以在实用程序类中编写方法,如下面的代码:

public Object getProperty(String property, Object obj) {
  if (obj != null && property != null) {
       Field field = obj.getClass().getDeclaredField(property);
       field.setAccessible(true);
       try {
          return field.get(obj);
       } catch (Exception ex) {
          return null;
       }
  }
  return null;
}

You may actually declare it as a static method and then just import this method into your classes which will need this behavior. 实际上,您可以将其声明为静态方法,然后将此方法导入到需要此行为的类中。

By the way, Groovy support this feature. 顺便说一句,Groovy支持此功能。

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

相关问题 什么是Java相当于Python的reduce函数? - What is the Java equivalent to Python's reduce function? 什么是 Java 的 UnsupportedOperationException 的 Python 等价物? - What is the Python equivalent of Java's UnsupportedOperationException? 什么是Java Collections Framework的Python等价物? - What is/are the Python equivalent(s) to the Java Collections Framework? Java 的 ruby​​ 机架或 python 的 wsgi 的等价物是什么? - What's the equivalent of ruby's rack or python's wsgi for Java? Python与Java InputStream的可用方法等效吗? - What's Python's equivalent to Java InputStream's available method? Java SWING的JFrames中WindowsForms中的DialogResult属性等效于什么? - What is the equivalent of the DialogResult property from WindowsForms in Java SWING's JFrames? 什么是 Java 相当于 Python 的 subprocess shell=True 属性? - what is the Java equivalent of Pythons's subprocess shell=True property? Java 中 python 的原生 hash() 函数的确切等价物是什么? - What's the exact equivalent of python's native hash() function in Java? 什么是Java相当于Java的标准for循环? - What is Python's equivalent of Java's standard for-loop? 什么是 Python 的 Celery 项目的 Java 等价物? - What's the equivalent of Python's Celery project for Java?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM