简体   繁体   English

Java:将任何类的任何属性作为参数的方法

[英]Java : method that takes in argument any attribute of any class

I need to create a method that takes in argument any attribute of any class . 我需要创建一个接受任何类的任何属性作为参数的方法。 But i dont want it to be of type String, to avoid refactoring problems while renaming an attribute and to get the errors in Markers Tab of eclipse, and not while running my application. 但是我不希望它是String类型的,以避免在重命名属性时避免重构问题并在Eclipse的Markers选项卡中获取错误,而不是在运行应用程序时。

Having a class Person : 上课的人:

public class Person {

    private String name;
    // other attributes...

    // getters and setters...

}

Now the needed method : 现在需要的方法:

void getAnAttributeOfAClass( <which_type_or_class_here?> attr_as_arg){

   // Now I need to get the name of attribute that would be of class Strin...

}

Is there a function or a method, by which we can specify an attribute? 是否存在可以指定属性的函数或方法?

For example : 例如 :

Person.class.name

Would it be of class Property ? 它属于Property类吗?

EDIT 编辑

More exactly (@Smallhacker answer helped me), I need to verify at compile time if the argument is really an attribute of the specified class. 更确切地说(@Smallhacker的回答对我有帮助),我需要在编译时验证参数是否确实是指定类的属性。

Person.class.name // no compile time error
Person.class.nameXXX // compile time error

The closest to what you want is Reflection API's Field or JavaBeans Introspector API's PropertyDescriptor . 最接近您想要的是Reflection API的Field或JavaBeans Introspector API的PropertyDescriptor

But usually things like that are not needed in Java projects because there are libraries which handle these concerns. 但是通常在Java项目中不需要这样的事情,因为有一些库可以处理这些问题。

You could pass a Class object along with a String name, then let your method use Introspector internally to read that property. 您可以将Class对象和String名称一起传递,然后让您的方法在内部使用Introspector来读取该属性。

Not sure I understand you well, but there is a class java.lang.reflect.Field, that has a method getName() that would give your the name of the field. 不确定我是否了解您,但是有一个类java.lang.reflect.Field,它具有方法getName()来为您提供字段名称。

In your example, to get field name, you would do: Person.class.getDeclaredField("name"). 在您的示例中,要获取字段名称,请执行以下操作:Person.class.getDeclaredField(“ name”)。

EDIT: to get the value of a field in an object, you would do: field.get(obj); 编辑:要获取对象中字段的值,您可以这样做:field.get(obj);

OK, let's say You have the following variables: 好的,假设您具有以下变量:

Person person = ...; // initialized with some Person
Field nameField = Person.class.getDeclaredField("name");

Now to get the name of person, you would do: 现在要获取人名,您需要执行以下操作:

String personName = (String)nameField.get(person);

Actually, this would throw an exception because name is a private field. 实际上,这将引发异常,因为name是一个私有字段。 You can however bypass the protection by doing: 但是,您可以执行以下操作来绕过保护:

nameField.setAccessible(true);

Unfortunately, Java lacks an ability to reference member variables in a way that can be analyzed at compile time. 不幸的是,Java缺乏以可以在编译时进行分析的方式引用成员变量的能力。

There may be some kind of library to simplify this somewhat, but it wouldn't provide a full solution due to limitations in the language itself. 可能有某种库可以对此进行某种程度的简化,但是由于语言本身的限制,它无法提供完整的解决方案。

Maybe java generics can help you with this. 也许java泛型可以帮助您。

You can do something like: 您可以执行以下操作:

class YourClass<E> {
    void getAnAttributeOfAClass(E attr_as_arg){

        // some code

    }        
}

someVariable = new YourClass<Person>();

someVariable.getAnAtributeOfAClass(someObject); //this will not compile if someObject is not an instance of Person

But I still don't know what you want to do exactly inside the method. 但是我仍然不知道您到底想在该方法中做什么。

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

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