区别

类名.class叫做“类字面量”,因class是关键字, 所以类名.class编译时确定。

getclass()运行时根据实际实例确定,getClass()是动态而且是final的。

String.class 是能对类名的引用取得在内存中该类型class对象的引用,

new String().getClass() 是通过实例对象取得在内存中该实际类型class对象的引用。

 

例子

1.抽象类
package com.abc;
public abstract class Animal {
}

 

2.实例类
package com.abc;
public class Dog extends Animal {
    public static void main(String[] args) {
       Animal animal = new Dog();
       System.out.println(animal.getClass().getName());
       System.out.println(Animal.class.getName());
    }
}

 

3.结果
com.abc.Dog

com.abc.Animal

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2021-12-03
  • 2021-06-13
  • 2021-08-26
  • 2021-07-10
  • 2022-03-03
  • 2022-12-23
猜你喜欢
  • 2021-12-03
  • 2021-06-07
  • 2021-09-28
  • 2022-12-23
  • 2021-11-01
相关资源
相似解决方案