zoukankan      html  css  js  c++  java
  • 使用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;
    }


    }

  • 相关阅读:
    为什么MySQL死锁检测会严重降低TPS
    OneProxy FAQ 之proxy-user-list
    OneProxy主从延迟检测
    MySQL和OneSQL并行插入性能对比
    天下文章一大抄,你抄我来我抄它
    卖软件的尴尬
    死锁检测为什么耗时?
    28岁的我
    突然想把一生都奉献给MySQL
    如何确定编码风格?
  • 原文地址:https://www.cnblogs.com/Koma-vv/p/9561165.html
Copyright © 2011-2022 走看看