栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

Java重载和覆盖

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Java重载和覆盖

方法重载意味着根据输入来制作功能的多个版本。例如:

public Double doSomething(Double x) { ... }public Object doSomething(Object y) { ... }

在编译时选择要调用的方法。例如:

Double obj1 = new Double();doSomething(obj1); // calls the Double versionObject obj2 = new Object();doSomething(obj2); // calls the Object versionObject obj3 = new Double();doSomething(obj3); // calls the Object version because the compilers see the         // type as Object        // This makes more sense when you consider something likepublic void myMethod(Object o) {  doSomething(o);}myMethod(new Double(5));// inside the call to myMethod, it sees only that it has an Object// it can't tell that it's a Double at compile time

方法覆盖意味着通过原始方法的子类定义方法的新版本

class Parent {  public void myMethod() { ... }}class Child extends Parent {  @Override  public void myMethod() { ... }}Parent p = new Parent();p.myMethod(); // calls Parent's myMethodChild c = new Child();c.myMethod(); // calls Child's myMethodParent pc = new Child();pc.myMethod(); // call's Child's myMethod because the type is checked at runtime    // rather than compile time

希望对您有所帮助



转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/372604.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号