使用super调用父类的构造方法

package com.bjpowernode.t02inheritance.c09;

/*
* 使用super调用父类的构造方法
*/
public class TestSuper02 {

public static void main(String[] args) {
IPhoneX iPhoneX1 = new IPhoneX(10);

IPhoneX iPhoneX2 = new IPhoneX();

// System.out.println(iPhoneX.color);
// System.out.println(iPhoneX.model);
}

}

class IPhone{

String color;

String model;

public IPhone() {
System.out.println("父类的无参构造方法");
}

public IPhone(String color, String model) {
System.out.println("父类的有参构造方法");
this.color = color;
this.model = model;
}


}

class IPhoneX extends IPhone{

int a;

public IPhoneX() {
//系统会默认调用父类的无参构造方法
//通过下面语句去调用构造方法
//如果没有显示的编写的话,系统会提供默认的
//如果显示编写的话,一定要保证super出现在构造方法中的第一行
//this(10);//this和super不能同时出现在构造方法中
//super("土豪金","iPhone x");//手动调用父类中的有参构造方法
System.out.println("子类无参构造");
System.out.println(this.a);
}

public IPhoneX(int a) {

this();
//super("土豪金","iPhone x");
System.out.println("子类有参的构造方法");
this.a = a;
}


}

转载于:https://www.cnblogs.com/Koma-vv/p/9561165.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值