简体   繁体   English

Java类对象和类Class

[英]Java class object and class Class

I have been reading this book on java about runtime type information and I am very confused about class objects and the class Class. 我一直在阅读关于运行时类型信息的java这本书,我对类对象和类Class非常困惑。 The question maybe silly but I am kind of confused. 问题可能很愚蠢,但我有点困惑。 When we normally write a class we use smaller case c for the word class. 当我们通常编写一个类时,我们使用较小的case c作为word类。 eg. 例如。 public class Foo with the class name starting with upper case. 公共类Foo,类名以大写字母开头。 so the class Class is upper case C for the class name. 所以类Class是类名的大写字母C. Now the confusion is about the class object. 现在混淆是关于类对象。 which is also upper case for that class. 这也是该类的大写。 so now forexample if we have say a method in a class such as 所以现在例如,如果我们在一个类中说一个方法,比如

public void countClass(Class<?>  type) {
 Class<?> superClass = type.getSuperclass();
}

public TypeCounter (Class<?> baseType) {
this.baseType = baseType;
}

Now in the first method what I understand is that the method countClass of type void has an argument of any type of class object? 现在在第一个方法中,我理解的是void类型的方法countClass有任何类型的对象的参数? The Class referring to here is what exactly?? 这里提到的类是什么? The class CLass? CLass班? or the Class object? 还是Class对象? if it is a class object of any type why cant we just write the name of the superclass here instead of complicating things so much? 如果它是任何类型的类对象,为什么我们不能在这里写出超类的名称而不是那么复杂的东西?

In the second method there is no type stated at all for the method TypeCounter? 在第二种方法中,TypeCounter方法根本没有说明类型? how does that work? 这是如何运作的? and again it has a type of Class in the argument passed? 并且它在参数传递中有一种类型?

If these are referring to the Class class then what does it really mean? 如果这些是指Class类那么它究竟意味着什么? and if it is just the Class object then what does the class object really do in this case here? 如果它只是Class对象,那么这个类对象在这种情况下真正做了什么?

There are classes like this: 有这样的类:

static class PetCounter extends LinkedHasMap<Class<? extends pet>,Integer> {
....}

now the Class is what really? 现在班级真的是什么? class Class or Class object? class Class还是Class对象? I'm sorry if I am confusing you with this question. 如果我对这个问题感到困惑,我很抱歉。 But please try to answer. 但请尝试回答。

Thanks in advance. 提前致谢。

type is a reference to a class of unknown type (hence <?> ). type是对未知类型的引用(因此<?> )。

Lets say you have a Class Car you would get the runtime representation of this car passed to countClass. 假设您有一辆Class Car您可以将此车的运行时表示传递给countClass。 Then with getSuperClass you would get the super class of Car . 然后使用getSuperClass,您将获得Car的超级类。 This may be Object or something else. 这可能是对象或其他东西。

The second one is a constructor which has one Parameter of type Class for a unknown type (hence the <?> ). 第二个是一个构造函数,它有一个类型为Class的类型,用于未知类型(因此<?> )。

You last example is a static inner class with the Name PetCounter which derives from a LinkedHashSet which in turn only accepts objects of type Class which are instantiated of pet. 最后一个示例是一个带有Name PetCounter的静态内部类,它派生自LinkedHashSet,而LinkedHashSet又只接受实例化pet的Class类型的对象。 (I guess pet should be upper case). (我猜pet应该是大写的)。

What this means is this: You can only add objects of type Class which represent some type of pets. 这意味着:您只能添加Class类型的对象,它们代表某种类型的宠物。

You have to look at it this way: In java everything is a object except primitives (int, boolean etc). 你必须这样看待它:在java中,一切都是一个对象,除了基元(int,boolean等)。 This means, if I call myObjectInstance.getClass() I get an object representing the metainformation of this object. 这意味着,如果我调用myObjectInstance.getClass()我会得到一个表示该对象的元信息的对象。 This object is of type Class . 此对象的类型为Class Since I have multiple classes in my java project, Eg Car Vehilce etc. I can further specify what this meta information will contain hence I can specify the type of this classes with a generic. 因为我在我的java项目中有多个类,例如Car Vehilce等。我可以进一步指定这个元信息将包含什么,因此我可以用泛型指定这个类的类型。 If I don't know this, I can specify <?> . 如果我不知道这个,我可以指定<?>

The <?> is the so-called wildcard argument meaning, that it can be an object of any type, without specifying the superclass. <?>是所谓的通配符参数,意思是它可以是任何类型的对象,而不指定超类。 We could narrow it down, by defining a superclass ( <? extends A> ), or a subclass ( <? super B> ), but in this case we don't. 我们可以通过定义超类( <? extends A> )或子类( <? super B> )来缩小范围,但在这种情况下我们不会。

And the second method is actually a constructor, which is kind of a class boilerplate, used to pass values to initialize class fields. 第二种方法实际上是一个构造函数,它是一种类样板文件,用于传递值以初始化类字段。 It's a fundamental OOP concept, be sure to read more about it. 这是一个基本的OOP概念,请务必阅读更多相关信息。

Class type variables in java holds the metadata (like methods details, annotations , implemented interfaces by class etc..) of the classes. java中的类型变量包含类的元数据(如方法详细信息,注释,按类等实现的接口等)。 It is not associated to any particular instance of class but is same for all instances of a given class. 它不与任何特定的类实例相关联,但对于给定类的所有实例都是相同的。

Whereas Lower case class is a reserved keyword for class declaration. 小写是类声明的保留关键字。

In first method argument is of type Class, and it is not an instance but a metadata holder created from 在第一个方法中,参数是Class类型,它不是实例,而是从中创建的元数据持有者

Class<?> metadata = AnyClass.class;

This probably won't answer your entire question but hopefully give you a direction to think upon. 这可能不会回答你的整个问题,但希望能给你一个思考的方向。

Let's have a look at the javadoc for the Class class: 我们来看看Class类的javadoc:

Instances of the class Class represent classes and interfaces in a running Java application. 类Class的实例表示正在运行的Java应用程序中的类和接口。 ... ...

Class has no public constructor. 类没有公共构造函数。 Instead Class objects are constructed automatically by the Java Virtual Machine as classes are loaded and by calls to the defineClass method in the class loader. 相反,类对象由Java虚拟机在加载类时自动构造,并通过调用类加载器中的defineClass方法来构造。

... ...

It is also possible to get the Class object for a named type (or for void) using a class literal. 也可以使用类文字获取命名类型(或void)的Class对象。 See Section 15.8.2 of The Java™ Language Specification. 请参阅Java™语言规范的第15.8.2节。 For example: 例如:

 System.out.println("The name of class Foo is: "+Foo.class.getName()); 

The keyword class is used for a class declaration (define a class) or as a class literal (express the Class object of a class for runtime use) 关键字class用于类声明(定义类)或类文字(表示类的Class对象以供运行时使用)

public class Foo {}
Class<Foo> fooClass = Foo.class;

The Class class explained above is used at runtime to represent the class of an object as an object. 上面解释的Class类在运行时用于将对象的类表示为对象。

Methods[] methods = fooClass.getMethods();
Foo foo = fooClass.newInstance();

Another example, getting the Class from an instance of an object: 另一个例子,从一个对象的实例获取Class

Foo foo2 = new Foo();
Class<Foo> fooClass2 = foo2.getClass();

首先,你必须知道“类”和“类”之间的区别。我们把类似的东西放到同一类别,即“类”。例如,汽车和自行车可以在车辆类。当然,所有类都可以放入相同的类别,即“类”。“类”表示“类”的原型。类的每个对象都有自己的类,它保留其信息,如名称,属性,方法等。

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

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