zoukankan      html  css  js  c++  java
  • Java学习之面向对象---继承

    继承:子继承父,子可以拥有父的所有。
    继承的好处:
      1.提高了代码的复用性
      2.让类与类之间产生了关系。有了这个关系,才有了多态的特性
    Java 只支持单继承,不支持多继承
    class A
    {
        void show()
        {
            System.out.println("a");
        }
    }
    class B
    {
        void show()
        {
            System.out.println("b");
        }
    }
    
    class C extends A,B
    {
        
    }
    new C().show();不确定show方法

    Java 支持多层继承

    因为有继承关系,所以子类就包含父类中的成员
    类中成员:
    1.变量:子父类中有同名的成员变量(不会这样设计类)
      this:代表当前类对象的引用,super:代表当前类的父类对象的引用
    2.函数:有重写特性
      如果子类中也有和父类相同的成员函数,会重写
      如果子类没有父类的成员函数,那子类就直接继承。
      重写:子父类的成员函数要完全一样
    3.构造函数
      子类实例化时,会默认在子类构造函数第一行隐式追加super();
      所以子类只要实例化时,都会访问父类的无参构造函数(super())
      如果想访问指定的父类构造函数可以在子类构造函数第一行中指定父类的构造函数(super("XXXX"))
     1 class ExtendsDemo
     2 {
     3     public static void main(String[] args)
     4     {
     5         Student s=new Student();
     6         //1.子类可以直接使用父类变量
     7         s.name="zhangsan";
     8         s.sleep();
     9         
    10         Student s2=new Student("Student_Name");
    11         
    12         Student s3=new Student("Student_Name",20);
    13     }
    14 }
    15 
    16 class Person
    17 {
    18     //3.
    19     Person()
    20     {
    21         System.out.println("Person Run");
    22     }
    23     
    24     Person(String name)
    25     {
    26         System.out.println("Person Run====="+name);
    27     }
    28     
    29     
    30     int num=4;
    31     String name;
    32     int age;
    33     //2.
    34     void sleep()
    35     {
    36         System.out.println("Person sleep");
    37     }
    38 }
    39 
    40 class Student extends Person
    41 {
    42     //3.
    43     Student()
    44     {
    45         //隐式语句super();
    46         System.out.println("Student Run");
    47     }
    48     
    49     Student(String name)
    50     {
    51         //隐式语句super();
    52         System.out.println("Student Run----"+name);
    53     }
    54     
    55     Student(String name,int age)
    56     {
    57         //指定父类的构造函数
    58         super(name);
    59         System.out.println("Student Run----"+name+"==="+age);
    60     }
    61     
    62     int num =5;
    63     void study()
    64     {
    65         System.out.println("good study");
    66     }
    67     
    68     //2.重写父类方法
    69     void sleep()
    70     {
    71         //保留父类功能,使用super
    72         super.sleep();
    73         System.out.println("Student sleep");
    74     }
    75 }
    76 
    77 class worker extends Person
    78 {
    79     void work()
    80     {
    81         System.out.println("good work");
    82     }
    83 }

    结果:

     知识点:final关键字

    final:最终。是一个修饰符

    1、可以修饰类,函数,变量

    2、修饰类,此类不能被继承,此类不能有子类

    final class XXX{     ..........     }

    3、修饰函数,函数不能被重写

    final void show(){  ....  }

    4、修饰变量,变量就是一个常量(字母全部大写,单词之间下划线分割),变量只能赋值一次

     1 class Demo
     2 {
     3     void show()
     4     {
     5         final int y=4;
     6         //y++;//出错:不可以再赋值
     7         System.out.println(y);
     8     }
     9     
    10     //final int num的作用范围再方法体内,就说明num变量再方法体中是不可改变的。
    11     void show1(final int num)
    12     {
    13         //num++;//这个是错误的
    14         System.out.println(num);
    15     }
    16 }
    17 
    18 class FinalDemo
    19 {
    20     public static void main(String[] args)
    21     {
    22         Demo d=new Demo();
    23         d.show1(1);//这都是正确的
    24         d.show1(2);
    25     }
    26 }

    结果:

  • 相关阅读:
    loadrunner -27778 https连接问题
    https调试
    Session Alerts
    Pause Web Sessions
    Customize Web Sessions List
    单例模式:Java单例模式的几种写法及它们的优缺点
    Activity: launchMode 和 Intent.FLAG_ACTIVITY_CLEAR_TOP
    TextView: android:ellipsize="marquee" 跑马灯效果无效的问题
    Socket通信(1):搭建开发环境
    linux: QT安装时出现段错误segmentation fault
  • 原文地址:https://www.cnblogs.com/WarBlog/p/12054994.html
Copyright © 2011-2022 走看看