简体   繁体   English

Java中此类/模板的用法是什么意思?

[英]What does this class/template usage mean in Java?

package MyTest;

import java.lang.reflect.*;

public class CallingMethod {
    public static void main(String[] args) {
        String firstName = new String("Deepak");
        String lastName = new String("Kumar");
        String result = new String("");

        Class<String> cls = String.class;
        Class[] parametertype = new Class[] { String.class };
        Object[] arguments = new Object[] { lastName };
        try {
            Method concatMethod = cls.getMethod("concat", parametertype);
            result = (String) concatMethod.invoke(firstName, arguments);
            System.out.println(" Concatenated String is => " + result);
        } catch (Exception e) {
            System.out.println(e);
        }
    }
}

For the above code, I don't understand below parts. 对于上面的代码,我不理解以下部分。

Class<String> cls = String.class;
Class[] parametertype = new Class[] { String.class };
Object[] arguments = new Object[] { lastName };

So what does String.class mean? 那么String.class是什么意思? And String.class in new Class[] { String.class } means what? String.classnew Class[] { String.class }意味着什么? It means the first item in the array is a String.class type? 这意味着数组中的第一项是String.class类型吗? Or all the items are String.class type? 还是所有项目都是String.class类型? And new Object[] { lastName } ? 还有new Object[] { lastName } It means the first item/instance of the array is lastName? 这意味着数组的第一项/实例是lastName? Hope to get your expertise! 希望得到您的专业知识! Thanks! 谢谢!

The code is using reflection and method invocation to call the concat method in the String class, on the object firstName , using the arguments parameters. 该代码使用反射和方法调用来使用参数parameters在对象firstName上的String类中调用concat方法。 It is similar to calling: 它类似于调用:

result = firstName.concat(lastName);
  • Class<String> cls = String.class; is a Class object which refers to the String class. 是引用String类的Class对象
  • Class[] parametertype = new Class[] { String.class }; is an array of Class objects, containing one element: String.class 是Class对象的数组,包含一个元素: String.class
  • Object[] arguments = new Object[] { lastName }; is an array of Objects containing one element too 是也包含一个元素的对象数组

So what does String.class mean? 那么String.class是什么意思?

A reference to the String Class 对字符串类的引用

And String.class in new Class[] { String.class } means what? 新的Class [] {String.class}中的String.class是什么意思?

An array of Classes which has one reference to the String Class. 具有对字符串类的一个引用的类的数组。

means the first item in the array is a String.class type? 意味着数组中的第一项是String.class类型?

The type of the first element is Class or Class<String> 第一个元素的类型是Class或Class<String>

And new Object[] { lastName }? 还有新的Object [] {lastName}? It means the first item/instance of the array is lastName? 这意味着数组的第一项/实例是lastName?

Yes. 是。

I don't understand below parts. 我不明白下面的部分。

It appears you are correct in all your questions so it appears you were right all along. 看来您在所有问题中都是正确的,因此您一直以来都是正确的。

Do you have any other questions? 你还有别的问题吗?

Every object in Java is associated with a type , eg, String or MyClass . Java中的每个对象都与一种类型关联,例如StringMyClass The type is itself implemented by another object. 类型本身是由另一个对象实现的。 So String.class and MyClass.class are referring to the class objects for those types. 因此, String.classMyClass.class引用这些类型的类对象。

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

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