this和super的区别

来源:互联网 发布:软件兼容性测试 编辑:程序博客网 时间:2024/05/23 17:50

this表示对本类成员方法的引用

super表示对父类成员和方法的的引用

两者不能同时出现在一个构造函数中,都需要出现在构造方法的第一行

package no2;class Person{//父类int i=10;Person(){System.out.println("super()调用父类构造函数");}}class Student extends Person{//子类int i=100;Student(){super();System.out.println("this()调用该类本身的构造函数");}Student(int age){this();}void show(){System.out.println(super.i + " " + this.i);//super.i引用父类成员,this.i引用本类成员}}public class Main {public static void main(String[] args) {Student s1 = new Student(3);s1.show();}}


0 0