super关键字




- 表示超类或父类
  • super只能在子类中使用,可以访问父类中的实例变量、实例方法、还可以访问父类的构造方法


    访问父类的构造方法

    class Person{
    	//构造方法
    	public Person(){
    
    	}
    	public Person(String name,char sex,int age){
    		this.name = name;
    		this.sex = sex;
    		this.age = age;
    	}
    	//属性
    	String name;
    	char sex;
    	int age;
    	}
    }
    //编写学生子类继承人的父类
    class Student extends Person{
    	//构造方法
    	public Student(){
    
    	}
    	public Student(String name,char sex,int age,int id){
    		/*this.name = name; 
    		this.sex = sex;
    		this.age = age;*/
    
    		//以上三条语句,也可以编写为如下:
    		/*super.name = name; 
    		super.sex = sex;
    		super.age = age; */
    		//以上三条语句,也可以编写为调用父类的带三个参数构造方法完成,使用super关键字
    		//super();  //调用父类无参构造方法
    		super(name,sex,age);
    		this.id = id;
    		
    	}
    	//编写独有属性
    	int id;
    	//编写独有方法:学习
    	}
    



posted @ 2020-01-09 10:28  火车没有火  阅读(146)  评论(0编辑  收藏  举报