Java面向对象6——继承中的构造方法

来源:互联网 发布:windows 搭建owncloud 编辑:程序博客网 时间:2024/06/10 07:24
  • 子类的构造过程中必须调用其基类的构造方法。
  • 子类可以在自己的构造方法中使用super(argument_list)调用基类的构造方法
     - 使用this(argument_list)调用本类的另外的构造方法
     - 如果调用super,必须写在子类构造方法的第一行
  • 如果子类的构造方法中没有显式地调用基类构造方法,则系统默认调用基类无参数的构造方法。
  • 如果子类构造方法中既没有显式调用基类构造方法,而基类中又没有无参的构造方法,则编译出错。

    实例1:

    class SuperClass {        private int n;        /*        SuperClass() {            System.out.println("SuperClass()");        }        */        SuperClass(int n) {            System.out.println("SuperClass(" + n + ")");            this.n = n;        }    }    class SubClass extends SuperClass {        private int n;        SubClass(int n) {            //此处相当于调用了super();            System.out.println("SubClass(" + n + ")");            this.n = n;        }        SubClass() {            super(300);//对super的调用必须在第一行            System.out.println("SubClass()");        }    }    public class TestSuperSub {        public static void main(String arg[]) {            //SubClass sc1 = new SubClass();            SubClass sc2 = new SubClass(400);        }    }

  实例2:

    class A{        protected void print(String s){            System.out.println(s);        }        A(){            print("A()");        }        public void f() {            print("A:f()");        }           }    class B extends A{        B(){//子类的构造方法          //自动调用父类无参数的构造方法A();            print("B()");        }        public void f() {        //重写了基类中的方法            print("B:f()");        }        public static void main(String[] args){            B b = new B();            b.f();        }    }

  运行结果:

      这里写图片描述

  
  实例3:

    class Person{        private String name;        private String location;        Person(String name){            this.name = name;            location = "beijing";        }        Person(String name, String location){            this.name = name;            this.location = location;        }        public String info() {            return "name: "+name+" location:"+location;        }    }    class Student extends Person{        private String school;        Student (String name, String school){            //调用了自己这个类另一个构造方法            this(name,"beijing",school);        }        Student (String n,String l, String school){            super(n,l);            this.school = school;        }        public String info() {            return super.info()+" school: "+school;         }    }    class Teacher extends Person{        private String title;        Teacher(String name,String title){            this(name,"beijing",title);        }        Teacher(String n,String l,String title){            super(n,l);            this.title = title;        }        public String info(){            return super.info()+" title: "+ title;        }    }    public class Test{        public static void main (String[] args){            Person p1 = new Person("A");            Person p2 = new Person("B","Shanghai");            Student s1 = new Student ("C","S1");            Student s2 = new Student ("C","shanghai","S2");            Teacher t1 = new Teacher ("D","Professor");            System.out.println(p1.info());            System.out.println(p2.info());            System.out.println(s1.info());            System.out.println(s2.info());            System.out.println(t1.info());        }    }

结果:这里写图片描述

0 0