whether the results are the same of getClass().getName() called in child class and parent class?

本文通过一个具体的Java示例展示了getClass().getName()方法在父类和子类中的使用情况,并验证了该方法返回的类名是否一致。实验结果显示,无论是在父类还是子类中调用,返回的都是子类的全限定名。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

whether the results are the same of getClass().getName() called in child class and parent class?

UsageGetClass2.java

abstract class AbstractClass {
        public AbstractClass() {
                System.out.println("the current class(in AbstractClass):" + this.getClass().getName());
        }
        abstract public void print();
}

public class UsageGetClass2 extends AbstractClass {
        public UsageGetClass2() {
                super();
        }

        public static void main(String[] args) {
                UsageGetClass2 g = new UsageGetClass2();
                g.print();
                System.out.println("the current class is(in child class): " + g.getClass().getName());
        }

        public void print() {
                System.out.println("hello world!");
        }
}


the result:

xxx:~/java/getclass$ java UsageGetClass2
the current class(in AbstractClass):UsageGetClass2
hello world!
the current class is(in child class): UsageGetClass2

the results are the same.

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值