类名.class和getClass()区别

class叫做“类字面量”,因class是关键字, 所以class编译时确定,getclass()运行时根据实际实例确定。
String.class 是能对类名的引用取得在内存中该类型class对象的引用,
new String().getClass() 是通过实例对象取得在内存中该实际类型class对象的引用
(这个方法是从java.lang.Object类继承过来的)
getClass()是动态而且是final的
1.抽象类
package com.chinacreator.yxg.bean;
public abstract class PersonInfo {
    private String name;
    public int getAge() {
       return age;
    }
    public void setAge(int age) {
       this.age = age;
    }
}

2.实例类
package com.chinacreator.yxg.bean;
public class ManInfo extends PersonInfo {
    private int height;
    public int getHeight() {
       return height;
    }

    public void setHeight(int height) {
       this.height = height;
    }

    public static void main(String[] args) {
       PersonInfo personInfo = new ManInfo();
       System.out.println(personInfo.getClass().getName());
       System.out.println(PersonInfo.class.getName());
    }
}

结果:
com.chinacreator.yxg.bean.ManInfo

com.chinacreator.yxg.bean.PersonInfo


转载网址:http://hi.baidu.com/develop_skill/item/443fccfcfc3df4db6225d244

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值