简体   繁体   English

如何将不同的班级用作一个班级的成员?

[英]How do I use different classes as member for one class?

I have a superclass A, two classes X,Y which inheriting from A and another class B where I will use X,Y as member. 我有一个超类A,两个继承自A的类X,Y和另一个将使用X,Y作为成员的类B。

public class A {
    public String strA;
}

public class X extends A {
    public String strX;
}

public class Y extends A {
    public String strY;
}

public class B {
    public A member;

    public B(A member) {
        this.member=member;
    }
}

public static void main() {
    X x = new X();
    Y y = new Y();

    B b1 = new B(x);
    B b2 = new B(y);

    System.out.println(b1.strA); //works
    System.out.println(b1.strX); //does not work!
    System.out.println(b2.strA); //works
    System.out.println(b2.strX); //does not work!
}

How can I solve this problem? 我怎么解决这个问题?

My approach would be that I use the datatype for the member in B as a placeholder? 我的方法是将B中的成员的数据类型用作占位符?

Thanks! 谢谢!

Your code doesn't compile as said by Jon Skeet because you're trying to access a field of your B's field. 您的代码无法像Jon Skeet所说的那样进行编译,因为您试图访问B字段中的一个字段。

To compile, the code would have been 要进行编译,代码应该是

System.out.println(b1.member.strA); //works

What you'll have to do here to use the fields from the inherited instances is explicitely cast your member reference. 要使用继承的实例中的字段,在此处必须执行的操作明确地强制转换了member引用。

If you're using a reference to the motherclass, it will only show you the fields accessible from the motherclass. 如果您使用对母类的引用,它将仅显示从母类可访问的字段。

In this case, only strA is accessible with an A reference. 在这种情况下,只能通过A引用访问strA


Solution

System.out.println(((X)b1.member).strX);

As you see, first we cast b1.member to X and then we call the strX field. 如您所见,首先将b1.memberX ,然后将其strX字段。

And if you want to be sure that b1.member is an instance of X , use an if test like this 而且,如果要确保b1.memberX的实例,请使用类似if的测试

if (b1.member instanceof X) System.out.println(((X)b1.member).strX);

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

相关问题 如何添加两个不同类的ArrayList,将一个抽象类扩展为一个ArrayList? - How do I add two ArrayLists of different classes that extend an abstract class into one ArrayList? 如何同步对其他类成员的访问? - How do I synchronize access to a member of a different class? 如何使用 JOptionPane 使用从不同类中的一个类接收到的用户输入? - How do I use the input received from the user from one class in a different class, using JOptionPane? 如何在一堂课中执行不同的课 - How to execute different classes in one class 我如何使用JUnit在一个Java类中对测试方法进行单元化而又没有在模块中完成其他类? - How do I use JUnit to unit test methods in one Java class, without having completed other classes in the module? 如何在2个不同的类上使用一个数组 - How to use one array on 2 different classes 我如何使用反射为类中使用的ArrayLIst查找类成员名称,例如? - how do i use reflection to find the class member name for an ArrayLIst that is used in the class, example? 如何使用log4j2为不同的类使用不同的日志记录级别? - How do i use different logging levels for different classes with log4j2? 如何正确访问静态成员类? - How do I correctly access static member classes? 如何在ActionListener的其他类中使用方法? - How do I use a method in a different class in ActionListener?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM