简体   繁体   English

如何通过对象传递在另一个泛型类中访问类的属性?

[英]How to access a class's property inside another generic class by object passing?

In my code, Generic class Public will receive a generic object in check() method. 在我的代码中,通用类Public将在check()方法中收到一个通用对象。 These generic objects might be from JuniorInstructor class or SeniorInstructor class.I wanna access the properties of JuniorInstructor class or SeniorInstructor class. 这些通用对象可能来自JuniorInstructor类或SeniorInstructor类。我想访问JuniorInstructor类或SeniorInstructor类的属性。 I tried with below implementation and it works. 我尝试了以下实现,它可以工作。 But, Is there any better way to do this? 但是,还有更好的方法吗?

   class JuniorInstructor extends Instructor {

    public JuniorInstructor(String id, String name) {
        super(id, name);

    }

}

class Public<T> {
    T object;
    JuniorInstructor j1;
    SeniorInstructor s1;

    public void check(T object) {

        this.object = object;
        if (object instanceof SeniorInstructor) {
            s1 = (SeniorInstructor) object;
            System.out.println("name: " + s1.name + "\tid: " + s1.id);
        } else {
            j1 = (JuniorInstructor) object;
            System.out.println("name: " + j1.name + "\tid: " + j1.id);
        }
    }// check
}
public static void main(String[] args) {
        SeniorInstructor s1 = new SeniorInstructor("120", "Ashik");
        JuniorInstructor j1 = new JuniorInstructor("677", "Tareq");
        Public p = new Public();

        p.check(j1);

    }

You can further qualify T : 您可以进一步限定T

class Public<T extends Instructor>

And simply make sure all the methods you need to access are defined on Instructor . 并且只需确保您需要访问的所有方法都在Instructor上定义。 You'll then be able to call them directly in Public . 然后,您可以直接在Public调用它们。

[Edit] @JBNizet has an excellent point in comments that Public doesn't really need to be generics if Instructor and its subclasses are all it needs to handle. [JBNizet]在评论中有一个很好的论点,即如果Instructor及其子类仅需要处理的话, Public确实不需要成为泛型。

You can declare the Public class like 您可以像这样声明Public类

class Public<T extends Instructor> {
      ...
   }

in this way you can avoid to check for instance type, as long as you need to access to 'Instructor' property. 这样,只要您需要访问“教师”属性,就可以避免检查实例类型。

Hope this help 希望这个帮助

I think might want to use polymorphism instead of generics . 我认为可能要使用多态而不是泛型

Create 2 overloaded versions of check method: 创建2个重载版本的check方法:

  • one that accepts a JuniorInstructor 接受JuniorInstructor
  • one that accepts a SeniorInstructor 接受SeniorInstructor

In this way, there is no need to use instanceOf or casting objects to a given type. 这样,就无需使用instanceOf或将对象强制转换为给定类型。

class Public {
    JuniorInstructor j1;
    SeniorInstructor s1;

    public void check(SeniorInstructor object) {
        s1 = object;
        System.out.println("name: " + s1.getName() + "\tid: " + s1.getId());
    }

    public void check(JuniorInstructor object) {
        j1 = object;
        System.out.println("name: " + j1.getName() + "\tid: " + j1.getId());
    }
}

As Ward mentioned, 正如沃德所说,

One of the benefits of generics is that you can write generic code, without the need for using instanceof and casting etc... 泛型的好处之一是您可以编写泛型代码,而无需使用instanceof和cast等。

You can modify the Public class as below : 您可以如下修改Public类:

class Public<T extends Instructor> {
   T instructor;

   public void check(T object) {
       this.instructor = object;
       System.out.println("name: " + this.instructor.name + "\tid: " + this.instructor.id);
   }
}
public class Instructor {

private String id;
private String name;

public Instructor(String id, String name) {
        this.id= id; this.name= name;
}

//getters and setters
 }

Then a class public : 然后是一个班级公众:

class Public<T extends Instructor> {
T object;

public void check(T object) {
        System.out.println("name: " + object.getName() + "\tid: " + object.getId());
}
}

However, if your class has only 1 method using this T as parameter, you can modify it to: 但是,如果您的类只有一个使用此T作为参数的方法,则可以将其修改为:

class Public {

public <T extends Instructor> void check(T object) {
        System.out.println("name: " + object.getName() + "\tid: " + object.getId());
}
 }

Or since usage of generics is not clear here, we can simply use: 或者由于在这里不清楚泛型的用法,我们可以简单地使用:

class Public {

public  void check(Instructor object) {
        System.out.println("name: " + object.getName() + "\tid: " + object.getId());
}
 }

And in main: 而在主要方面:

 public static void main(String[] args) {
    Public obj= new Public();
    obj.check(new JuniorInstructor("1","Payal"));
}

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

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