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

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

How to pass a Class as an function parameter

问题

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

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

  1. public class A {}
  2. public class A.a extends A {}
  3. public class A.b extends A {}

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

  1. int countA.a(A[] array) {
  2. int counter = 0;
  3. for (int i = 0; i < array.length; i++) {
  4. if (array[i] instanceof A.a) {
  5. counter++;
  6. }
  7. }
  8. return counter;
  9. }
  10. int countA.b(A[] array) {
  11. int counter = 0;
  12. for (int i = 0; i < array.length; i++) {
  13. if (array[i] instanceof A.b) {
  14. counter++;
  15. }
  16. }
  17. return counter;
  18. }

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

  1. int countInstances(A[] array, Class n) {
  2. int counter = 0;
  3. for (int i = 0; i < array.length; i++) {
  4. if (n.isInstance(array[i])) {
  5. counter++;
  6. }
  7. }
  8. return counter;
  9. }

感谢您的耐心!

英文:

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:

  1. Public class A{}
  2. Public class A.a extends A{}
  3. 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:

  1. int countA.a(A[] array){
  2. int counter = 0;
  3. for(int i = 0; i &lt; array.length; i++){
  4. if(array[i] instanceof A.a){
  5. counter++;
  6. }
  7. }
  8. return counter;
  9. }
  10. int countA.b(A[] array){
  11. int counter = 0;
  12. for(int i = 0; i &lt; array.length; i++){
  13. if(array[i] instanceof A.b){
  14. counter++;
  15. }
  16. }
  17. return counter;
  18. }

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

  1. int countInstances(A[] array, class n){
  2. int counter = 0;
  3. for(int i = 0; i &lt; array.length; i++){
  4. if(array[i] instanceof n){
  5. counter++;
  6. }
  7. }
  8. return counter;
  9. }

Thanks for your patience!

答案1

得分: 3

你可以使用 Class 类:

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

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

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

用法:

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

You can use the Class class:

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

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

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

Usage:

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

答案2

得分: 0

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

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

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

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

  1. int instances = countInstances(myArray, A.b.class);
  2. int countInstances(A[] array, Class<?> n) {
  3. int counter = 0;
  4. for (int i = 0; i < array.length; i++) {
  5. if (array[i].getClass() == n) {
  6. counter++;
  7. }
  8. }
  9. return counter;
  10. }

一些示例:

  1. boolean hasSameClass(Object type, Class<?> n) {
  2. return type.getClass() == n;
  3. }
  4. hasSameClass(new LinkedList(), ArrayList.class); // false
  5. hasSameClass(new ArrayList(), LinkedList.class); // false
  6. hasSameClass(new ArrayList(), ArrayList.class); // true
  7. hasSameClass(new ArrayList(), List.class); // false
英文:

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

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

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

So in your example, something like:

  1. int instances = countInstances(myArray, A.b.class);
  2. int countInstances(A[] array, Class&lt;?&gt; n){
  3. int counter = 0;
  4. for(int i = 0; i &lt; array.length; i++){
  5. if(array[i].getClass() == n){
  6. counter++;
  7. }
  8. }
  9. return counter;
  10. }

Some examples:

  1. boolean hasSameClass(Object type, Class&lt;?&gt; n){
  2. return type.getClass() == n;
  3. }
  4. hasSameClass(new LinkedList(), ArrayList.class); // false
  5. hasSameClass(new ArrayList(), LinkedList.class); // false
  6. hasSameClass(new ArrayList(), ArrayList.class); // true
  7. 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:

确定