首页 > 编程 > Java > 正文

10th_JavaSE笔记_super()构造方法

2019-11-11 06:07:26
字体:
来源:转载
供稿:网友
一、子类在其构造方法中必须调用其父类的构造方法。如果不调用,子类就相当于是无源之水,无本之木,所以必须先调用父类的构造方法,再构造自己。

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() { print("B()"); }	public void f() { print("B:f()"); }		public static void main(String[] args) {		B b = new B();		b.f();	}}

运行结果:

A()B()B:f()正好印证了第一点:子类在new一个对象时,会先在构造方法中调用父类的构造方法。

二、子类可以在自己的构造方法中使用super(argument_list)调用父类的构造方法。 1)如果调用super,必须写在子类构造方法的第一行。2)使用this(argument_list)调用本类其它的构造方法。

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 {	String school;		Student(String name, String school) {		this(name, "beijing", school);	}	 	Student(String name, String location, String school) {		super(name, location);		this.school = school;	}		public String info() {		return super.info() + " school: " + school;	}}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");		System.out.println(p1.info());		System.out.println(p2.info());		System.out.println(s1.info());		System.out.println(s2.info());	}} 

运行结果:

name: A location: beijingname: B location: shanghainame: C location: beijing school: S1name: C location: shanghai school: S2

正好印证了第二点:子类可以在自己的构造方法中使用super(argument_list)调用父类的构造方法,super构造方法必须是子类构造方法的第一条语句,子类构造方法可以使用this(argument_list)来调用本类其它的构造方法。

本例中的一些构造方法自动给没有传进来的参数进行了默认值的赋值,如location = "beijing";这是在new对象时还不确定location值为多少的情况下的一种暂定为、默认为"beijing"的处理。

三、如果子类的构造方法没有显式地调用父类构造方法(即未调用super(argument_list)),则系统调用父类无参数的构造方法。

class Person {	String name;	Person() {		System.out.println("Person");	}}class Student extends Person{	String school;	Student(String school) {		this.school = school;	}}如果子类Student构造方法的第一条语句缺省super,系统将自动调用父类无参的构造方法Person()。

四、如果子类构造方法中既没有显式地调用父类构造方法,而父类中又没有无参的构造方法,则编译出错。

class Person {	String name;	Person(String name) {		this.name = name;	}}class Student extends Person{	String school;	Student(String school) {		this.school = school;	}}如果是这种情况,别指望系统能够调用父类无参的构造方法。一旦构造方法被重载,并且没有重载出无参的构造方法,那么该类中就没有无参的构造方法了,此例的父类Person就是如此,因此在编译时就会报错。


发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表