zoukankan      html  css  js  c++  java
  • java中的继承和覆盖

    继承

           作用:缩减代码,简化重复代码

       通过关键字extends来实现,格式如:class 子类 extends 父类{}

    继承的本质

       子类继承父类之后,实例化子类对象的时候,系统会首先实例化父类对象,而此时调用的是父类没有参数的构造函数,如果父类的构造函数带有参数,我们可以使用super给父类构造函数传参数,“super();”必须写在子类构造函数的第一句,传入的参数必须和父类构造函数中参数列表类型匹配;也可以给父类增加一个不带参数的空构造函数。例:

    package extends1;
    class Dialog{
          protected String title;
          //public Dialog(){}改错方法2:不带参数的构造函数
          public Dialog(String title){
                this.title = title;
          }
          public void show(){
                System.out.println(title+"对话框显示");
          }
    }
    class FontDialog extends Dialog{
         private String fontName;
         public FontDialog(String title,String fontName){//报错
                //super(title);改错方法1:super传参数
                this.title = title;
                this.fontName = fontName;
         } 
    }
    public class Main {
          public static void main(String[] args){
                FontDialog fd = new FontDialog("字体","宋体");
          }
    }

    注:1、如果一个成员要被子类继承之后使用,这个成员不能是 private 的,因为私有的成员不能在类的外部使用,当然也不能被子类使用。一般,成员变量定义为 protected类型,成员函数定义为 public 类型。

        2、Java 不支持多重继承,一个子类只能有一个父类,不允许出现:class 子类 extends 父类 1,父类 2 {}

        3、Java 中可以有多层继承,比如 A 继承了 B,B 又继承了 C。此时相当于 A 间接继承了 C。

    成员覆盖

            一般发生在子类继承父类的成员函数之间(成员变量之间一般很少使用),当子类函数的定义和父类相同时(名称相同,参数列表相同,返回类型相同),最后调用的是子类中的方法,这叫做覆盖或重写(Override)。

        注:1、如果在子类中定义了一个名称和参数列表与父类相同的函数,但是返回类型不同,此时系统会报错。

        2、 在重写时,子类函数的访问权限不能比父类的更加严格。比如,父类的成员函数访问权限是 public,子类重写时,就不能定义为 protected。

        3、在覆盖的情况下,如果一定要在子类中调用父类的成员函数,可以使用 super关键字,调用父类的成员函数方法是:super.函数名

    成员覆盖的作用

              在不改变源代码的情况下,能够对一个模块的功能进行改造。对父类进行继承时,拥有的一部分功能无法完全满足需要,我们就可以通过覆盖完善。例:

    现有:

    package extends2;
    public class ImageOpe {
          public void read(){
                System.out.println("从硬件读取图像");
          }
           public void handle(){
                 System.out.println("图像去噪声");
            }
            public void show(){
                  System.out.println("显示图像");
            }
    }

    我们需要:

    package extends5;
    public class MyImageOpe extends ImageOpe{
          public void read(){//进行重写
                System.out.println("从文件读取图像");
          }
          public void handle(){//继承后加改造
                super.handle();
                System.out.println("图像锐化");
          }
          public void show(){//继承
                super.show();
          }
    }
    public class Main {
          public static void main(String[] args){
                MyImageOpe mio = new MyImageOpe();
                mio.read();
                mio.handle();
                mio.show();
           }
    }

    输出为:

    从文件读取图像
    图像去噪声
    图像锐化
    显示图像
    

      

  • 相关阅读:
    LeetCode212. Word Search II
    LeetCode132. Palindrome Partitioning II
    LeetCode239. Sliding Window Maximum
    LeetCode解题报告—— Interleaving String
    LeetCode解题报告—— Maximal Rectangle
    LeetCode解题报告—— Minimum Window Substring && Largest Rectangle in Histogram
    LeetCode解题报告—— N-Queens && Edit Distance
    LeetCode解题报告—— Trapping Rain Water
    在CentOS上安装第三方软件库EPEL
    lunix存取windows共享文件夹
  • 原文地址:https://www.cnblogs.com/541wsy/p/12511827.html
Copyright © 2011-2022 走看看