编程语言
首页 > 编程语言> > Java基础(basis)-----关键字this和super的作用

Java基础(basis)-----关键字this和super的作用

作者:互联网

   1.关键字this

package com.keyword;

/**
 * this关键字
 * 
 * @author yyx 2019年2月15日
 */
public class ThisDemo {
    private String stuName;
    private Integer stuAge;
    private String stuSex;        

    public ThisDemo() {
        super();
        System.out.println("无参构造函数");
        this.sayWord(); //this调用方法
    }

    public ThisDemo(String stuName, Integer stuAge) {
        this();  // 调用重载的构造方法
        this.stuName = stuName;
        this.stuAge = stuAge;
        System.out.println("两个参数的构造函数");
    }

    public ThisDemo(String stuName, Integer stuAge, String stuSex) {
        this(stuName, stuAge);  // 调用重载的构造方法
        this.stuSex = stuSex;
        System.out.println("三个参数的构造函数");
    }

    public String getStuName() {
        return stuName;
    }

    public void setStuName(String stuName) {
        this.stuName = stuName;
    }

    public Integer getStuAge() {
        return stuAge;
    }

    public void setStuAge(Integer stuAge) {
        this.stuAge = stuAge;
    }

    public String getStuSex() {
        return stuSex;
    }

    public void setStuSex(String stuSex) {
        this.stuSex = stuSex;
    }        
    
    @Override
    public String toString() {
        return "ThisDemo [stuName=" + stuName + ", stuAge=" + stuAge + ", stuSex=" + stuSex + "]";
    }

    public void sayWord() {
        System.out.println("我会说话!");
    }
    
    public static void main(String[] args) {
        ThisDemo thisDemo=new ThisDemo("张三",23,"男");
        System.out.println(thisDemo.toString());
    }

}

   注意:this只能在类中的非静态方法中使用,静态方法和静态的代码块中绝对不能出现this

   2.关键字super

package com.keyword;

public class SuperDemo {
    public static void main(String[] args) {
        Student student = new Student("李四", 23, "女", "223311");
        student.sayWord();
    }
}

class Person {
    private String name;
    private Integer age;
    private String sex;

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

    public Person(String name, Integer age, String sex) {
        this();// 调用本类重载的构造方法
        this.name = name;
        this.age = age;
        this.sex = sex;
        System.out.println("父类有参的构造方法!");
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Integer getAge() {
        return age;
    }

    public void setAge(Integer age) {
        this.age = age;
    }

    public String getSex() {
        return sex;
    }

    public void setSex(String sex) {
        this.sex = sex;
    }

    public void sayWord() {
        System.out.println("我是父类,人都会说话!");
    }
}

class Student extends Person {
    private String cno;

    public Student(String name, int age, String sex, String cno) {
        // 调用父类构造器
        super(name, age, sex);
        this.cno = cno;
    }

    public String getCno() {
        return cno;
    }

    public void setCno(String cno) {
        this.cno = cno;
    }

    public void sayWord() {
        // 调用父类方法
        super.sayWord();
        System.out.println("我是子类,我也会说话!");
        super.setName("本来叫李四,改名叫张三");
        // 调用父类获取属性方法
        System.out.println(super.getName());
    }

}

 

标签:Java,String,basis,void,父类,super,public,stuAge
来源: https://www.cnblogs.com/fengfuwanliu/p/10376017.html