如何将一个类作为函数参数传递

huangapple go评论96阅读模式
英文:

How to pass a Class as an function parameter

问题

知道这之前已经有人问过,但我无法完全理解/答案没有帮助到我,所以我会很感谢在这个问题上能得到一些帮助,因为我开始学习Java。

我想创建一个函数,用来计算一个类的实例数量并返回一个数字,例如我有:

public class A {}
public class A.a extends A {}
public class A.b extends A {}

我有一个包含A、A.a和A.b实例的A[]数组,我想在该数组中计算A、A.a或A.b的实例数量,因此我创建了以下函数:

int countA.a(A[] array) {
    int counter = 0;
    for (int i = 0; i < array.length; i++) {
        if (array[i] instanceof A.a) {
            counter++;
        }
    }
    return counter;
}

int countA.b(A[] array) {
    int counter = 0;
    for (int i = 0; i < array.length; i++) {
        if (array[i] instanceof A.b) {
            counter++;
        }
    }
    return counter;
}

我的问题是,我如何做类似于这样的事情:

int countInstances(A[] array, Class n) {
    int counter = 0;
    for (int i = 0; i < array.length; i++) {
        if (n.isInstance(array[i])) {
            counter++;
        }
    }
    return counter;
}

感谢您的耐心!

英文:

I know this has been asked before, but I couldn't quite understand/the answers didn't help me, so I would appreciate some help on this matter, as I'm starting to learn Java.

I want to create a function that counts the instances of a class and returns me a number, for example i have:

Public class A{}
Public class A.a extends A{}
Public class A.b extends A{}

I have an array of A[] that has instances of A, A.a and A.b and I want to count the instances of A, A.a or A.b in that array, so i created these functions:

int countA.a(A[] array){
int counter = 0;
  for(int i = 0; i &lt; array.length; i++){
    if(array[i] instanceof A.a){
      counter++;
    }
  }
  return counter;
}

int countA.b(A[] array){
int counter = 0;
  for(int i = 0; i &lt; array.length; i++){
    if(array[i] instanceof A.b){
      counter++;
    }
  }
  return counter;
}

My question is, how can i do something like this:

int countInstances(A[] array, class n){
int counter = 0;
  for(int i = 0; i &lt; array.length; i++){
    if(array[i] instanceof n){
      counter++;
    }
  }
  return counter;
}

Thanks for your patience!

答案1

得分: 3

你可以使用 Class 类:

int countInstances(A[] array, Class&lt;? extends A&gt; n){

而不是使用 instanceof,你应该在 Class 类中使用 isInstance 方法。

if(n.isInstance(array[i])){
  counter++;
}

用法:

int count = countInstances(someArray, SubclassOfA.class);
英文:

You can use the Class class:

int countInstances(A[] array, Class&lt;? extends A&gt; n){

And instead of instanceof, you should use the isInstance method in the Class class.

if(n.isInstance(array[i])){
  counter++;
}

Usage:

int count = countInstances(someArray, SubclassOfA.class);

答案2

得分: 0

你可以使用 A.class 获取类型的 Class,或者通过实例使用以下方式获取:

A myA = new A();
Class<?> clazzOfA = myA.getClass();

并将其作为类型为 Class<?> 的参数传递。

所以在你的示例中,类似于:

int instances = countInstances(myArray, A.b.class);

int countInstances(A[] array, Class<?> n) {
    int counter = 0;
    for (int i = 0; i < array.length; i++) {
        if (array[i].getClass() == n) {
            counter++;
        }
    }
    return counter;
}

一些示例:

boolean hasSameClass(Object type, Class<?> n) {
    return type.getClass() == n;
}

hasSameClass(new LinkedList(), ArrayList.class);    // false
hasSameClass(new ArrayList(), LinkedList.class);    // false
hasSameClass(new ArrayList(), ArrayList.class);     // true
hasSameClass(new ArrayList(), List.class);          // false
英文:

you can get the Class of a type by using A.class, or from an instance by using

A myA = new A();
Class&lt;?&gt; clazzOfA = myA.getClass();

and pass it as a parameter of type Class&lt;?&gt;

So in your example, something like:

int instances = countInstances(myArray, A.b.class);

int countInstances(A[] array, Class&lt;?&gt; n){
int counter = 0;
  for(int i = 0; i &lt; array.length; i++){
    if(array[i].getClass() == n){
      counter++;
    }
  }
  return counter;
}

Some examples:

boolean hasSameClass(Object type, Class&lt;?&gt; n){
    return type.getClass() == n;
}

hasSameClass(new LinkedList(), ArrayList.class);    // false
hasSameClass(new ArrayList(), LinkedList.class);    // false
hasSameClass(new ArrayList(), ArrayList.class);     // true
hasSameClass(new ArrayList(), List.class);          // false

huangapple
  • 本文由 发表于 2020年4月7日 04:34:37
  • 转载请务必保留本文链接:https://go.coder-hub.com/61068518.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定