Java构造器、this关键字、super关键字

Java构造器详解以及this、super关键字

一、构造器
(1)构造器用于初始化对象。
(2)构造器的调用是在new此类的对象时调用的,可返回一个初始化完成的对象。
(3)若一个类不写构造器,java会自动为该类提供一个无参构造器。
(4)构造器语法格式:
  修饰符 构造器名(参数){}
  ▶构造器修饰符一般是public;
  ▶构造器名和类名相同;
  ▶参数可以有多个;
(5)代码示例:

public class BigDog {
    private String name;
    private String color;
    private int age;

    public BigDog(String name) {
        this.name = name;
    }

    public BigDog(String name, String color) {
        this.name = name;
        this.color = color;
    }

    public BigDog(String name, String color, int age) {
        this.name = name;
        this.color = color;
        this.age = age;
    }
}

 也可写成如下:

public class BigDog {
    private String name;
    private String color;
    private int age;

    public BigDog(String name) {
        this.name = name;
    }

    public BigDog(String name, String color) {
        this(name);
        this.color = color;
    }

    public BigDog(String name, String color, int age) {
        this(name, color);
        this.age = age;
    }
}

 ▶概念①→构造器重载:一个类中可以定义多个构造器(构造器名必然相同),但要求形参列表不同,以上代码中三个构造器就是构造器重载,构造器可重载不可继承;
 ▶概念②→this引用:this关键字后面紧跟一个点,如this.name、this.age、this.方法名,对方法(非static方法)来说谁调用它,this就代表谁,对构造器来说构造器在初始化谁,this就代表谁;
 ▶概念③→this调用:this后面紧跟小括号,this(参数),this调用代表调用同一个类中重载的构造器,this调用只能出现在构造器第一行,如以上代码第二段中的this(name);和this(name, color);即this调用。

二、this关键字
(1)this出现在非static方法中、构造器中
(2)this出现在非static方法中时,this代表了该方法的调用者,谁调用此方法,this就代表谁,
(3)this可用于区分方法或构造器的局部变量(与成员变量同名时)

 代码示例如下:

class Camel {
    String color;
    double weight;

    public Camel(String color,double weight){
    	//构造器中的局部变量和此类中的成员变量同名时,this还起到区分的作用
        this.color = color;
        this.weight = weight;
    }

    public void test(){
        System.out.println(this.color);
        System.out.println(this.weight);
    }
}
class PigTest {
    public static void main(String[] args){
        Camel c1 = new Camel("白色",300.0);    
        /*在new的时候,相当于正在初始化c1这个变量所指向的对象,所以Camel类的构造器中this.color就代表c1的color,this.weight就代表c1的weight*/
        c1.test();   //输出“白色”和“300.0”
        /*c1调用test方法,所以test方法中的this就代表c1*/
    }
}

三、super关键字
(1)super用于限定访问父类的实例变量或实例方法

 访问父类实例变量如下:

class grandpa{
    int age = 80;
}
class dad extends grandpa{
    int age = 40;
    public void test(){
        int age = 30;
        System.out.println(age);            //输出30  访问此方法局部变量
        System.out.println(this.age);       //输出40  访问本类成员变量
        System.out.println(super.age);      //输出80  访问父类成员变量
    }
}
class SubTest{
    public static void main(String[] args) {
        dad d = new dad();
        d.test();
    }
}

 访问父类实例方法如下:

class grandpa{
    public void go(String name){
        System.out.println("父类的go方法,参数为:" + name);
    }
}
class dad extends grandpa{
    @Override
    public void go(String name){
        System.out.println("子类重写的go方法,参数为:" + name);
    }
    public void test(){
        go("小明");            //输出“子类重写的go方法,参数为:小明”
        super.go("阿伟");      //输出“父类的go方法,参数为:阿伟”
    }
}
public class SubTest {
    public static void main(String[] args) {
        dad d = new dad();
        d.test();
    }
}

四、子类构造器调用父类构造器

子类构造器会调用父类构造器。
如下例子:

class Meat{
    private double weight;
    public Meat(double weight){
        this.weight = weight;
    }
}
class Pork extends Meat{
    public Pork() {
    }
}

以上例子的代码会编译报错,原因是:子类若没有写调用父类的构造器,会默认在子类构造器第一行先调用父类无参构造器,但父类没有无参构造器,所以报错。

正确写法:

class Meat{
    private double weight;
    public Meat(double weight){
        this.weight = weight;
    }
}
class Pork extends Meat{
    private String health;
    public Pork(double weight) {
        super(weight);    //super指定调用父类的某一个构造器,出现在构造器第一行
    }
    public Pork(double weight,String health){
        this(weight);     //this指定调用本类的某一个构造器,出现在构造器第一行
        this.health = health;
    }
}

(1)父类若没写无参构造器,子类的构造器必须在构造器第一行用super指定调用父类的哪一个构造器;
(2)super后面紧跟小括号称为super调用,即调用父类构造器,且只能出现在构造器第一行;
(3)super调用和this调用不能同时出现(如以上例子中的super(weight)和this(weight)不能在同一个构造器中);

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值