Java this and super keywords

Copyright: Welcome ssm build personal blog https://www.yanzhaochang.top all copyright wait bloom all https://blog.csdn.net/qq_42081709/article/details/89924340

A this keyword

    definition

        1. this keyword on behalf of the caller of the function object belongs, this represents the memory address of the object, which points to object calls this method.

        2. this keyword represents the reference object, that is, at this point to an object, the object pointed to is to call the function of the object reference.

    effect:

        1. The difference between members and local variables can be invoked by this keyword.

        2. In the constructor can call another constructor to initialize the object.

        3. this may represent the current object as a parameter in the process.

        4. this method can represent the object reference

     note:

        1. When the presence of members of the same name and local variables inside a method of accessing local variables (Java uses the principle of proximity mechanism).

        2. In the case of a variable exists only member variable of Access, Java editor adds this default keyword in front of the variable.

        3. this keyword to call other constructors time, this keyword must be located on a statement constructor.

        4. this case call each other keywords not appear in the constructor.

        5. not call other constructors (non constructor) in the conventional method.

    Example:

  public class Demo {
	public static void main(String[] args) {
	    new People("410927XXXXXXX...", "张三",19);
	}
  }

  class People {
	private String id;
	private String name;
	private Integer age;

	public People(String id, String name) {
		this.id = id;// 区别局部变量和成员变量
		this.name = name;
	}
	public People(String id, String name, Integer age) {
		this(id, name);// 调用其他构造函数
		this.age = age;
	}
	public String getId() {
		return id;
	}
	public void setId(String id) {
		this.id = id;
	}
	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;
	}
	
}

Two super keyword

     On behalf of the parent class of super keyword refers to the space.

    effect:

        A member of the same name (variables and methods) 1. parent child exist, the default access in subclass subclass are members, members of the parent class may be directed by the super keyword

        2. When you create a sub-class, the default will call the parent class constructor with no arguments, you can specify the parent class constructor calls the super

    note:

       1. If the constructor calls the parent class is not specified in the subclass constructor, then the java compiler by default in the first line of the subclass constructor plus super () statement.

       2. super keyword when calling the constructor of the parent class, the statement must be the first statement in the subclass constructor.

       3. super and this keyword can not appear in a constructor, because the two keywords must be the first statement in the method of appearance, but you can use this. Variables.

      Example:

        People like to use the above test class

    public class Demo {
	public static void main(String[] args) {
	    new Worker("410927XXXXXXX...", "张三",99999d);
	}
    }

    class Worker extends People {
	private Double money;
	
	public Worker(String id, String name,Double money) {
	    super(id, name);
	    System.out.println("身份证号:"+super.getId());
	    System.out.println("姓名:"+super.getName());
	    this.money = money;
	}
    }

 

    

  The difference between this and super        

      1. The representative of different things

            On behalf of super is a reference to the parent class space (on behalf of the object is not just a memory object)

            this represents the caller of the function of the object belongs

      2. Use a different premise

            super keyword must have inherited a prerequisite to use

            this keyword is no need to use inheritance

      3. Call the constructor difference

            super keyword to call the constructor of the parent class

            this is called keyword constructor subclass

 

    

    

Guess you like

Origin blog.csdn.net/qq_42081709/article/details/89924340