Java学习者论坛

 找回密码
 立即注册

QQ登录

只需一步,快速开始

手机号码,快捷登录

恭喜Java学习者论坛(https://www.javaxxz.com)已经为数万Java学习者服务超过8年了!积累会员资料超过10000G+
成为本站VIP会员,下载本站10000G+会员资源,购买链接:点击进入购买VIP会员
JAVA高级面试进阶视频教程Java架构师系统进阶VIP课程

分布式高可用全栈开发微服务教程

Go语言视频零基础入门到精通

Java架构师3期(课件+源码)

Java开发全终端实战租房项目视频教程

SpringBoot2.X入门到高级使用教程

大数据培训第六期全套视频教程

深度学习(CNN RNN GAN)算法原理

Java亿级流量电商系统视频教程

互联网架构师视频教程

年薪50万Spark2.0从入门到精通

年薪50万!人工智能学习路线教程

年薪50万!大数据从入门到精通学习路线年薪50万!机器学习入门到精通视频教程
仿小米商城类app和小程序视频教程深度学习数据分析基础到实战最新黑马javaEE2.1就业课程从 0到JVM实战高手教程 MySQL入门到精通教程
查看: 350|回复: 0

[Java基础知识]Java继承与派生时的多态性与方法重写

[复制链接]
  • TA的每日心情
    开心
    2021-3-12 23:18
  • 签到天数: 2 天

    [LV.1]初来乍到

    发表于 2014-10-1 13:00:06 | 显示全部楼层 |阅读模式
    一、基本概念:
    ① 静态多态性(编译时的特性),java中的静态多态性实现手段-----重载函数。其调用规则是依据对象在定义时的类型相应地调用对应类中的重载函数
    ② 动态多态性(运行时的特性),Java中的动态多态性实现手段---覆盖(替换)基类中的同名成员函数(函数原型一致)。 其调用规则是依据对象在实例化时而非定义时的类型相应地调用对应类中的同名成员函数。
    ③ 父类与子类对象编程规则(赋值兼容原则):子类的对象可当着父类的对象来使用。

    二、应用实例:
       

    1. class Base{
    2. public void fun(){
    3.    System.out.println("Base Class fun()");
    4. }
    5. public void fun(int X){
    6.     System.out.println("Base Class fun(int x)");
    7. }
    8. }
    9. public class Derived extends Base{
    10.   public void fun(int x,int y){
    11.     System.out.println("Derived Class fun(int x,int y)");
    12.   }
    13.   public void fun(){
    14.     System.out.println("Derived Class fun()");
    15.   }
    16.   public static void main(String[] args){
    17.    Base obj=new Base();
    18.    obj.fun(); //调用基类中的fun()
    19.    // obj.fun(1,2); 错误
    20.    obj=new Derived();
    21.    obj.fun(); //调用派生类中的fun()
    22.    obj.fun(1); //调用基类中的fun(int x)
    23.    // obj.fun(1,2); 错误
    24.     Derived ObjD=new Derived();
    25.     ObjD.fun(1,2); //调用派生类中的fun(int x,int y)
    26.     ObjD.fun(); //调用派生类中的fun()
    27.     ObjD.fun(1); //因派生类中未定义出,则调用从基类中继承类来的
    28. }
    29. }
    30. 三、继承与派生时的方法重写权限要求:重写的方法的访问权限不能有比基类更严格
    31. 的访问权限和定义出更多的例外。
    32. 例如: class Base{
    33.         public void fun() throws IOException{
    34.         }
    35.        }
    复制代码

         class Derived extends Base{
    //错误! 重写的方法的访问权限不能有比基类更严格的访问权限和更多的例外定义
    private void fun() throws IOException,InterruptedException{
    }
    }
         
       
    1. 考考你(10):
    2. What is the output of the following program?
    3. 1. class Base {
    4. 2. int x=3;
    5. 3. public Base() {}
    6. 4. public void show() {
    7. 5. System.out.print(" The value is " +x);
    8. 6. }
    9. 7. }
    10. 8. class Derived extends Base {
    11. 9. int x=2;
    12. 10. public Derived() {}
    13. 11. public void show() {
    14. 12. System.out.print(" The value is " +x);
    15. 13. }
    16. 14. }
    17. 15. public class Test {
    18. 16. public static void main(String args[]) {
    19. 17. Base b = new Derived();
    20. 18. b.show();
    21. 19. System.out.println("The value is "+b.x);
    22. 20. }
    23. 21. } // end of class Test
    24. a. The value is 3 The value is 2
    25. b. The value is 2 The value is 3
    26. c. The value is 3 The value is 3
    27. d.The value is 2 The value is 2
    28. [url=../pr/answer.jsp]点击观看答案[/url]
    复制代码

       
      
       
       

         
       

         
       
      
      
      
       
      
       
      

      



                            function TempSave(ElementID)
                            {
                                    CommentsPersistDiv.setAttribute("CommentContent",document.getElementById(ElementID).value);
                                    CommentsPersistDiv.save("CommentXMLStore");
                            }
                            function Restore(ElementID)
                            {
                                    CommentsPersistDiv.load("CommentXMLStore");
                                    document.getElementById(ElementID).value=CommentsPersistDiv.getAttribute("CommentContent");
                            }
    回复

    使用道具 举报

    您需要登录后才可以回帖 登录 | 立即注册

    本版积分规则

    QQ|手机版|Java学习者论坛 ( 声明:本站资料整理自互联网,用于Java学习者交流学习使用,对资料版权不负任何法律责任,若有侵权请及时联系客服屏蔽删除 )

    GMT+8, 2024-5-1 18:16 , Processed in 0.348898 second(s), 34 queries .

    Powered by Discuz! X3.4

    © 2001-2017 Comsenz Inc.

    快速回复 返回顶部 返回列表