• Java继承与组合



    Java继承与组合


    继承

    java 中使用extends关键字表示继承关系,当创建一个类时,如果没有明确指出要继承的类,则是隐式地从根类Object进行继承。

    • 子类继承父类的成员变量
      1. 子类能够继承父类的public和protected成员变量,不能够继承父类的private成员变量
      2. 如果在子类中出现了同名称的成员变量,则子类的成员变量会屏蔽掉父类的同名成员变量。若要在子类中访问父类中同名成员变量,需要使用super关键字来进行引用。 
    public class Main {
    
        /**
         * @param args
         */
        public static void main(String[] args) {
            // TODO Auto-generated method stub
            Person p = new Student();
            System.out.println(p.name);
        }
    
    }
    class Person {
        public String name;
        public String sex;
        public short age;
        public Person(){
            name = "John";
            System.out.println("It is a person");
        }
    }
    class Student extends Person{
        public String name;
        public Student(){
            this.name = super.name;    //利用super引用父类变量
            System.out.println("It is a student");
        }
    }
    
    //输出
    It is a person
    It is a student
    John
    View Code 
    • 子类继承父类的方法
      1. 能够继承父类的public和protected成员方法,不能够继承父类的private成员方法
      2. 如果在子类中出现了同名称的成员方法,则称为覆盖,即子类的成员方法会覆盖掉父类的同名成员方法。如果要在子类中访问父类中同名成员方法,需要使用super关键字来进行引用
      3. 对于父类的静态方法,可以被继承但是不能被子类中的同名方法覆盖;子类中也可以隐藏父类的成员变量,只需要声明的变量名称相同,就算变量的数据类型不同,也一样可以隐藏父类成员变量。
      4. 构造方法是在新建对象的时候才会执行,静态语句块和静态方法在类加载到内存的时候就已经执行了,当新建一个A类对象时,先调用A的父类的静态方法,再调用A类的静态方法,最后才是A的父类的构造函数、A类的构造函数。
    public class Main {
    
        /**
         * @param args
         */
        public static void main(String[] args) {
            // TODO Auto-generated method stub
            Person p = new Student();
            p.printName();
            p.printAge();
        }
    
    }
    class Person {
        private Pet p = new Pet();
        /*static{
            System.out.println("Person");
        }*/
        public String name;
        public String sex;
        public short age;
        public Person(String name){
            this.name = name;
        }
        public void printName(){
            System.out.println("My name is" + name);
        }
        public static void printAge(){
            System.out.println("This is used to print the people's age");
        }
    }
    class Student extends Person{
        public String name;
        public int age;
        public Student(){
            super("John");
            this.name = super.name;    //利用super引用父类变量
        }
        public static void printAge(){
            System.out.println("This is used to print the student's age");
        }
        public void printName(){
            System.out.println("It is used to print the student's name");
        }
    }
    
    //输出
    It is used to print the student's name
    This is used to print the people's age
    View Code
    • 构造器
      1. 子类是不能够继承父类的构造器,但是,如果父类的构造器都是带有参数的,则子类的构造器中必须显示地通过“super”关键字调用父类的构造器并配以适当的参数列表。且必须是子类构造器的第一个语句。如果父类有无参构造器,则在子类的构造器中用super关键字调用父类构造器不是必须的,如果没有使用super关键字,系统会自动调用父类的无参构造器。
    class Person {
        public String name;
        public Person(String name){
            this.name = name;
        }
        public void printName(){
            System.out.println("My name is" + name);
        }
    }
    class Student extends Person{
        public Student(){
            super("John");
        }
        public void printName(){
            System.out.println("It is used to print the student's name");
        }
    }
    View Code

    组合

    组合是指在设计类的同时,把将要组合的类的对象加入到该类中作为自己的成员变量。

    class Student extends Person{
        public String name;
        public School school;        //组合
        public Student(){
            super("John");
            this.name = super.name;    //利用super引用父类变量
        }
        public void printName(){
            System.out.println("It is used to print the student's name");
        }
    }
    class Person {
        public String name;
        public Person(String name){
            this.name = name;
        }
        public void printName(){
            System.out.println("My name is" + name);
        }
    }
    class School {
        public String name;
        public String address;
        public void printName(){
            System.out.println("School name:" + name);
        }
        public void printAddress(){
            System.out.println("School address" + address);
        }
    }
    View Code

    程序中选择继承或者组合的原则:

    • 如果存在一种IS-A的关系(比如Student是一个Person),并且一个类需要向另一个类暴露所有的方法接口,那么更应该用继承的机制。
    • 如果存在一种HAS-A的关系(比如Student必有一所School),那么更应该运用组合。 
  • 相关阅读:
    入门菜鸟
    FZU 1202
    XMU 1246
    Codeforces 294E Shaass the Great 树形dp
    Codeforces 773D Perishable Roads 最短路 (看题解)
    Codeforces 814E An unavoidable detour for home dp
    Codeforces 567E President and Roads 最短路 + tarjan求桥
    Codeforces 567F Mausoleum dp
    Codeforces 908G New Year and Original Order 数位dp
    Codeforces 813D Two Melodies dp
  • 原文地址:https://www.cnblogs.com/zhanglei93/p/5751280.html
Copyright © 2020-2023  润新知