在 Java 中使用 class 关键字

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12982717/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 11:03:04  来源:igfitidea点击:

Working with the class keyword in Java

javaclass

提问by Mario Dennis

I don't really understand how the classkeywords work in some instances.

我真的不明白class关键字在某些情况下是如何工作的。

For example, the get(ClientResponse.class)method takes the ClientResponse.class. How does it use this when it gets it, and what are the advantages over just passing an instance of it?

例如,该get(ClientResponse.class)方法采用 ClientResponse.class. 当它得到它时它如何使用它,以及只传递它的一个实例有什么优势?

采纳答案by smcg

SomeClass.class

returns a Java Class object. Class is genericized, so the actual type of SomeClass.class will be Class<SomeType>.

返回一个 Java Class 对象。Class 是泛化的,因此 SomeClass.class 的实际类型将是Class<SomeType>.

There are lots of uses for this object, and you can read the Javadoc for it here: http://docs.oracle.com/javase/6/docs/api/java/lang/Class.html

这个对象有很多用途,你可以在这里阅读它的 Javadoc:http: //docs.oracle.com/javase/6/docs/api/java/lang/Class.html

回答by Bhesh Gurung

In ClientResponse.class, classis not a keyword, neither a static field in the class ClientResponse.

In ClientResponse.class,class不是关键字,也不是类中的静态字段ClientResponse

The keyword is the one that we use to define a class in Java. e.g.

关键字是我们用来在 Java 中定义类的关键字。例如

public class MyClass { } /* class used here is one of the keywords in Java */


The classin ClientResponse.classis a short-cut to the instance of Class<T>that represents the class ClientResponse.

classClientResponse.class是一个捷径,以实例Class<T>表示类ClientResponse

There is another way to get to that instance for which you need an instance of ClientResponse. e.g

还有另一种方法可以访问您需要ClientResponse. 例如

ClientResponse obj = new ClientResponse();
Class clazz = obj.getClass(); 

what are the advantage over just passing a instance of it?

与仅传递它的实例相比有什么优势?

In the above example you can see what would happen in case objwas null (an NPE). Then there would be no way for the method to get the reference to the Classinstance for ClientResponse.

在上面的示例中,您可以看到如果obj为 null(NPE)会发生什么。然后,该方法将无法获取对 的Class实例的引用ClientResponse

回答by duffymo

The Classclass, which is different from the classkeyword, is meta-data describing instances. It tells you about the methods, data members, constructors, and other features of the instances that you create by calling new.

Classclass关键字不同的类是描述实例的元数据。它告诉您通过调用创建的实例的方法、数据成员、构造函数和其他功能new

For example get(ClientResponse.class) method takes the ClientResponse.class how does it uses this when it gets it and what are the advantage over just passing a instance of it?

例如,get(ClientResponse.class) 方法采用 ClientResponse.class 获取它时它如何使用它以及仅传递它的实例有什么优势?

You can't pass an instance of ClientResponseto this method; it's expecting meta-data about allinstances of ClientResponse. If you passed an instance, you'd expect that the method might change the state of that instance. But passing the meta-data about all instances might allow the method to create a new kind of instance (e.g. a dynamic proxy) or do something else that depends on the meta-data about all instances of ClientResponse. See the difference?

您不能将 的实例传递ClientResponse给此方法;该公司预计,元数据有关的所有的实例ClientResponse。如果您传递了一个实例,您会期望该方法可能会更改该实例的状态。但是传递关于所有实例的元数据可能允许该方法创建一种新的实例(例如动态代理)或做一些其他的事情,这取决于关于所有实例的元数据ClientResponse。看到不同?

回答by Chip

A class is a "blueprint" of the object. The instance is a object.

类是对象的“蓝图”。实例是一个对象。

If we have

如果我们有

public class SomeClass {
   int a;
   SomeClass(int a) {
      this.a = a
   }
}

We can have an instance of this class

我们可以有一个这个类的实例

SomeClass c = new SomeClass(10);

cis an instance of the class. It has a integer awith value 10.

c是类的一个实例。它有一个a值为的整数10

The object SomeClass.classrepresents a Class.

该对象SomeClass.class代表一个Class

Here SomeClass.classis a objectof the type Classwhich has the information that SomeClassis

这里SomeClass.class是一个object的类型的Class,其具有这样的信息SomeClass

  1. a concrete classwith
  2. one constructor
  3. with a integer member variable

    and lots more other metadataabout the class SomeClass. Note that it does not have a value for a.

  1. 一个具体的类
  2. 一个构造函数
  3. 带有整数成员变量

    以及更多metadata关于该课程的其他信息SomeClass。请注意,它没有 的值a

You should use get(c)incase you are planning to do something with a instanceof clike call c.aor other useful functions to manupulate/get data of the instance.

您应该使用get(c)柜面你打算做一个东西instancec如呼叫c.a或其它有用的功能manupulate /获取实例的数据。

You should use get(SomeClass.class)when the get returns something based on the fact that the argument is some type of class. For example, if this is a method on a Registryclass which has a map which retrieves a implementation classbased on type of class passed in.

get(SomeClass.class)根据参数是某种类型的类这一事实,您应该在 get 返回某些内容时使用。例如,如果这是一个Registry类上的方法,该类具有implementation class根据传入的类类型检索 a 的映射。

回答by Krzysztof Jab?oński

The very most important fact is - you don't need to have an instance to call the method. It's critically useful in situations when you cannot for some reason instantiate a class, e.g. it's abstract, or have only private constructor, or can only be correctly instantiated by some framework, like Spring or JSF.

最重要的事实是 - 您不需要有一个实例来调用该方法。当您由于某种原因无法实例化一个类时,它非常有用,例如它是抽象的,或者只有私有构造函数,或者只能由某些框架正确实例化,例如 Spring 或 JSF。

You can then call getto obtain an object of a requested type without even knowing where it does come from and how it get's created.

然后您可以调用get以获取请求类型的对象,甚至不知道它来自哪里以及它是如何创建的。

回答by Tomasz Nurkiewicz

Here ClientResponse.classis an instance of Class<ClientResponse>. In general Classobject represents type of an object. When you create new instance:

ClientResponse.class是 的一个实例Class<ClientResponse>。通常,Class对象表示对象的类型。创建新实例时:

Object obj = new ClientResponse()

you can retrieve the class (type) of that object by calling:

您可以通过调用来检索该对象的类(类型):

obj.getClass()

So, why would you pass Classobjects around? It's less common, but one reason is to allow some method create arbitrary number of instances of a given class:

那么,为什么要传递Class对象呢?它不太常见,但一个原因是允许某些方法创建给定类的任意数量的实例:

ClientResponse resp = ClientResponse.newInstance();

回答by Kurtymckurt

There's a lot of ways Class objects can be used. This is used for Reflection. Below is a link that can help you understand more.

可以通过多种方式使用 Class 对象。这用于反射。下面是一个链接,可以帮助您了解更多信息。

http://docs.oracle.com/javase/tutorial/reflect/class/classNew.html

http://docs.oracle.com/javase/tutorial/reflect/class/classNew.html

回答by Ashutosh

Whenever we compile any Java file, the compiler will embed a public, static, finalfield named class, of the type java.lang.Class, in the emitted byte code. Since this field is publicand static, we can access it using dotted notation along with class name as in your case it is ClientResponse.class.

每当我们编译任何Java文件,编译器将嵌入一个publicstaticfinal场命名class的类型,java.lang.Class在所发出的字节码。由于此字段是publicand static,我们可以使用点号和类名来访问它,就像您的情况一样ClientResponse.class