简体   繁体   English

检查类是否有成员变量?

[英]Check if class has member variable?

I have written a small program, in it a couple of defined classes.我编写了一个小程序,其中包含几个已定义的类。 I am writing tests for these classes.我正在为这些类编写测试。 The constructor I wrote for one of the classes has an argument that is an instance of another class.我为其中一个类编写的构造函数有一个参数,它是另一个类的实例。 So, it looks something like this:所以,它看起来像这样:

interface type1 {}
interface type2 {}

class class1 implements type1{
    String something;
    int somethingElse;
    //...
}

class class2 implements type1{
    String something2;
    int somethingElse2;
    //...
}

class class3 implements type2{
    Object obj1;
    Object obj2;
    String blabla;
    //...

    class3(Object inObj1, Object inObj2, String x){
        this.obj1 = inObj1;
        this.obj2 = inObj2;
        this.blabla = x;
    }
} 

As stated above, I am creating tests.如上所述,我正在创建测试。 There are two test classes (one for each interface).有两个测试类(每个接口一个)。 I want them to look something like this:我希望它们看起来像这样:

class ExamplesType1{
    class1 type1Something1 = new class1(...);
    class2 type1Something2 = new class2(...);

}

class ExamplesType2{
    class3 type2Something = new class3(type1Something1, type1Something2, ...);
}

My question is, how do I access the objects that are members of ExamplesType1 from ExamplesType2 so that I may... run my tests?我的问题是,我如何从 ExampleType2 访问作为 ExamplesType1 成员的对象,以便我可以......运行我的测试? Assume that I cannot test both interfaces in the same class, and that I cannot define once more the objects defined in ExamplesType1, in ExamplesType2.假设我不能在同一个类中测试两个接口,并且我不能在 ExampleType2 中再次定义在 ExampleType1 中定义的对象。

I know I can create a return function (for example, getType1Something()), but they are public, so shouldn't I be able to access them without defining a function?我知道我可以创建一个返回函数(例如,getType1Something()),但它们是公开的,所以我不应该在不定义函数的情况下访问它们吗?

Sorry if this is a newb question... just started Java today.对不起,如果这是一个新手问题……今天刚开始使用 Java。

Thank you for your help!感谢您的帮助! If any clarification is needed, let me know.如果需要任何说明,请告诉我。

Assume ClassA has two instantiated classes, ClassB and ClassC as instantiated by假设ClassA有两个实例化的类, ClassBClassC ,由

ClassB classB = new ClassB();
ClassC classC = new ClassC();

ClassB and ClassC public variables can then be referenced inside ClassA , for example: classB.variableName .然后可以在ClassA引用ClassBClassC public变量,例如: classB.variableName

This can reference any object that is public from primitive types to objects.这可以引用从原始类型到对象的任何public对象。

If the classes are not instantiated, then the public variables must additionally use the static keyword to be referenced.如果类未实例化,则public变量必须另外使用要引用的static关键字。 This static reference allows you to circumvent the encapsulation from a parent class and instantiation.static引用允许您绕过父类和实例化的封装。

In other words, ClassB can reference the parent class, ClassA to retrieve a public static instance of itself, but if it is not a static variable, it must rely on instantiation, static getter methods, or instantiation with getter methods to access those objects.换句话说, ClassB可以引用父类ClassA来检索自身的public static实例,但如果它不是static变量,则必须依靠实例化、静态 getter 方法或使用 getter 方法的实例化来访问这些对象。

Hope this helps :)希望这可以帮助 :)

Assume you have two classes假设你有两个类

class ClassA {
    String flag;
    ...
}

class ClassB {
    String flag;
    ...
}

Them you have a few options to access field member from these classes.他们有几个选项可以从这些类访问字段成员。

  1. The usually recommended method is to create setters and getter for the fields.通常推荐的方法是为字段创建 setter 和 getter。 This way any block holding a reference to an instance of those classes will be able to access and modify these fields.这样,任何持有对这些类的实例的引用的块都将能够访问和修改这些字段。

  2. You can also elect to make these fields public, which will also allow any block of code holding references to these classes to access the public fields.您还可以选择公开这些字段,这也将允许任何持有对这些类的引用的代码块访问公共字段。

  3. If you want some classes to have access to those fields, but not everyone (not public) you can make them package protected.如果您希望某些类可以访问这些字段,但不是每个人(不是公共的),您可以将它们设置为包保护。 This will allow any other class within the same package to access those fields, but not anyone else.这将允许同一包中的任何其他类访问这些字段,但不允许其他任何人访问。 To make a field (or method) package protected, you simply do not specify an access modifier (public or private) for the field in question.要使字段(或方法)包受到保护,您只需不为相关字段指定访问修饰符(公共或私有)。 You can read more here .您可以在此处阅读更多内容。

  4. Lastly, and very handy for testing, is to use reflection to access the private members.最后,对于测试来说非常方便,是使用反射来访问私有成员。 Reflection is not very performant, and kind of hackish, but it can be very handy in certain situations.反射的性能不是很好,而且有点hackish,但它在某些情况下非常方便。 I have mostly used reflection to mock objects in unit tests, but have seen it used in very interesting ways as well.我主要使用反射来模拟单元测试中的对象,但也看到它以非常有趣的方式使用。

    Field field = MyClass.getDeclaredField("fieldName"); Field field = MyClass.getDeclaredField("fieldName"); field.setAccessible( true ); field.setAccessible(true); String flag = field.get( myInstance ); String flag = field.get( myInstance );

PS> Writing this mostly from memory, but I believe this should get you going. PS> 主要是凭记忆写这篇文章,但我相信这应该能让你继续前进。

Hope this helps!希望这可以帮助! Good luck!祝你好运!

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

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