简体   繁体   English

Object.class / object.getClass()(有什么区别)

[英]Object.class/object.getClass() (what is the difference)

Customer.class and cust.getClass() work, but cust.class does not ? Customer.classcust.getClass() 工作,但是 cust.class不可以 吗? What is the difference? 有什么区别?

public class Customer() {

}


public class Test {

    public Test() {
        Customer cust = new Customer();

        test(Customer.class);
        test(cust.getClass());
    }


    public <T> void test(Class<T> clazz) {
        System.out.println(clazz);
    }

}

Object.class is a "pseudo static field" of Object and returns the class object for the named class. Object.class是Object的“伪静态字段”,并返回命名类的类对象。 It generates essentially zero code. 它基本上生成零代码。

obj.getClass() is a virtual method of Object and returns the ACTUAL class object for the object in the obj reference. obj.getClass()是Object的虚拟方法,并在obj引用中返回该对象的ACTUAL类对象。 It generates an actual call to inspect and retrieve the class (which may be a subclass of the reference's declared class). 它生成一个实际的调用来检查和检索该类(该类可能是引用的声明类的子类)。

I'm not sure if obj.class will compile, but if it does it's a "compiler swizzle" equivalent to coding Object.class -- in general, when you use a reference in place of a literal class name, you get the equivalent as if you'd coded the reference's declared class name. 我不确定obj.class是否会编译,但是如果这样做,则相当于“编译器混乱”,等效于对Object.class进行编码-通常,当您使用引用代替文字类名称时,会得到等效的结果就像您已编码引用的声明的类名一样。

Construction obj.class will not compile at all, because .class will works only for classes. 构造obj.class根本不会编译,因为.class仅适用于类。 Like String.class, for instances of class you need to call getClass() 像String.class一样,对于类的实例,您需要调用getClass()

Edited after OP changes OP更改后进行编辑

Let's redo the example: 让我们重做示例:

public class Test {

    public static void main (String[] args){
        Test t = new Test();
    }

    public Test() {
        Customer customer = new FastFoodCustomer();
        test(Customer.class); 
        test(customer.getClass());
    }


    public <T> void test(Class<T> clazz) {
        System.out.println(clazz);
    }

}

class Customer{

}

class FastFoodCustomer extends Customer{

}

This gives the following output: 这给出以下输出:

class Customer
class FastFoodCustomer

getClass() method is inherited from Object ancestor class. getClass()方法是从Object祖先类继承的。 It will tell you what class the object is. 它将告诉您对象是什么类。 If you want to know a given instance class you need to call thatInstance.getClass() method. 如果您想知道给定的实例类,则需要调用thatInstance.getClass()方法。 If you call Customer.class you are getting just class Customer because you are asking what class Customer is. 如果您致电Customer.class您将获得class Customer因为您正在询问Customer是什么类。 But an object Customer can be a Customer but also it can be any subclass of it (ie FastFoodCustomer in this example). 但是对象Customer既可以是Customer ,也可以是它的任何子类(在此示例中为FastFoodCustomer)。

Your other example cust.class doesn't work because cust doesn't have any class attribute by itself nor inherited. 您的另一个示例cust.class不起作用,因为cust本身没有继承的任何class属性。

Object class has a reference to a class, but obj has the reference to an instance only, so you should use the method obj.getClass() Object class具有对Object class的引用,但是obj具有对实例的引用,因此应使用obj.getClass()方法

PS: If downvoting, please let me know where is my fault. PS:如果投票失败,请让我知道我的错在哪里。

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

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