2020年8月12日 两个关键字 this super

package com.atguigu.test01;

/*
 * this:
 * 1、意思:当前对象
 * (1)构造器
 *     正在被创建的对象
 * (2)方法
 *  正在调用该方法的对象
 *  
 * 2、 用法
 * (1)this.属性
 * 当局部变量与成员变量同名时,可以在成员变量的前面加this.
 * (2)this.方法
 * 没有非用不可的时候
 * (3)this()或this(实参列表)
 * this()表示调用本类的无参构造
 * this(实参列表)表示调用本类的有参构造
 * 
 * this()或this(实参列表)必须在构造器的首行
 */
public class TestThis {
    public static void main(String[] args) {
        Circle c1 = new Circle(1.2);//1.2代表半径
        c1.setRadius(2.5);
    }
}
class Circle{
    private double radius;
    
    public Circle(){
        System.out.println("一个圆对象被创建");
    }
    //(double radius)局部变量
    //给成员变量private double radius;赋值
    public Circle(double radius){
        this();
        this.radius = radius;
    }
    
    public void setRadius(double radius){
        this.radius = radius;
    }
    public double getRadius(){
        //return radius;//没有同名的问题,可以不加this.
        return this.radius;//也可以加this.
    }
    
    public double getArea(){
        return 3.14 * radius * radius;
    }
    
    public void printInfo(){
//        System.out.println("半径:" + radius + ",面积:" + getArea());
        System.out.println("半径:" + this.radius + ",面积:" + this.getArea());
    }
}

class Student{
    private String name;
    private int age;
    private char gender;
    private int score;
    
    public Student(){
        
    }
    public Student(String name, int age){
        this.name = name;
        this.age = age;
    }
    public Student(String name, int age, char gender, int score){
        /*this.name = name;
        this.age = age;*/
        this(name,age);//调用上面的有参构造
        this.gender = gender;
        this.score = score;
    }
}
package com.atguigu.test02;

/*
 * super:
 *       从父类中取查找,引用父类的xx
 * 要求:super只能方法在子类中可见的属性、方法、构造器
 * 
 * 用法:
 * 1、super.属性
 * 当子类声明了和父类同名的属性时,可以使用super.属性来访问父类的属性
 * 
 * 2、super.方法
 * 当在子类中需要调用父类被重写的方法时,可以使用super.方法
 * 
 * 3、super()或super(实参列表)
 * super();调用父类的无参构造
 * super(实参列表):调用父类的有参构造
 * 
 * 注意:
 * (1)super()或super(实参列表)必须在子类构造器的首行
 * (2)如果子类的构造器中,没有写super(),它也存在
 *    但是如果子类构造器中写super(实参列表),那么super()就不会存在的
 */
public class TestSuper {
    public static void main(String[] args) {
//        B b = new B();
//        b.printNum(3);
        
//        XueSheng x = new XueSheng("张三", 23, 89);
//        System.out.println(x.getInfo());
        
        XueSheng x = new XueSheng();
    }
}
class A{
    int num = 1;
}
class B extends A{
    int num = 2;
    public void printNum(int num){
        System.out.println(num);//局部变量  (int num)
        System.out.println(this.num);//成员变量,子类自己的int num
        System.out.println(super.num);
    }
}
class Human{
    private String name;
    private int age;
    public Human(String name, int age) {
        super();
        this.name = name;
        this.age = age;
    }
    public Human() {
        super();//调用的是公共父类java.lang.Object类的无参构造
        System.out.println("父类的无参构造");
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public int getAge() {
        return age;
    }
    public void setAge(int age) {
        this.age = age;
    }
    public String getInfo(){
        return "姓名:" + name + ",年龄:" + age;
    }
}
class XueSheng extends Human{
    private int score;

    public XueSheng(String name, int age, int score) {
        super(name, age);//调用父类Human的有参构造
        this.score = score;
    }

    public XueSheng() {
//        super();//调用父类Human的无参构造
    }

    public int getScore() {
        return score;
    }

    public void setScore(int score) {
        this.score = score;
    }
    public String getInfo(){
    //    return "姓名:" + getName() + ",年龄:" + getAge() + ",成绩:" + score;
        return super.getInfo() + ",成绩:" + score;
    }
}

 

posted @ 2020-08-12 13:18  窦云鹏  阅读(134)  评论(0编辑  收藏  举报