• this和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的左外连接丶右外连接与内连接的区别
    常见的异常种类
    Mysql
    JSTL 标签库
    VMware的虚拟机为什么ip地址老是自动变化?
    redis出现的问题
    在Linux系统下启动了tomcat,但是在游览器中进步了服务
    Linux下安装JDK 与 tomcat
    wamp配置多站点域名
    终端链接操作redis
  • 原文地址:https://www.cnblogs.com/Koma-vv/p/9584649.html
Copyright © 2020-2023  润新知