gpt4 book ai didi

java - JVM——子类方法的执行顺序和@override的使用

转载 作者:搜寻专家 更新时间:2023-11-01 01:43:55 26 4
gpt4 key购买 nike

<分区>

这是一个新手问题。我读到 JVM 的执行从层次结构中最低类搜索方法名开始,如果该方法在该类中不可用,它会遍历到父类寻找该方法。

如果是这种情况,那么为什么我们需要使用“@override”来向继承类添加自定义逻辑?

下面的例子说明了我的问题

class superclassA
{

method()
{
}

}

class subclassB extends superclassA
{

@Override
//While executing if JVM starts looking for the method name from the lowest hierarchy
//then why do we have to use "override" as the methodname will be matched from the lowest level itself?
method()
{
--custom subclass specific code...
}

}

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com