zoukankan      html  css  js  c++  java
  • Java关键字——super

    使用super关键字可以从子类中调用父类中的构造方法、普通方法和属性

    与this调用构造方法的要求一样,语句必须放在子类构造方法的首行

    this和super都可以调用构造方法,但是两者不能同时出现,调用构造的时候都必须放在构造方法的首行

    //=================================================
    // File Name       :	extends_demo
    //------------------------------------------------------------------------------
    // Author          :	Common
    
    
    // 类名:person
    // 属性:
    // 方法:
    class person{
    	private String name;
    	private int age;
    	
    	public person(String name,int age){	//构造方法
    		this.name = name;
    		this.age = age;
    	}
    	
    	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;
    	}
    	
    	//方法:print
    //	void print(){	//定义一个默认访问权限的方法
    //		System.out.println("Person---->void print()");
    //	}
    	
    	//方法:getInfo()
    	public String getInfo(){
    		return "姓名"+this.name+"年龄"+this.age;
    	}
    }
    
    //类名:student
    //属性:
    //方法:
    class student extends person{
    
    	private String school;		//新定义的属性school
    	
    	public student(String name, int age,String school) {
    		super(name, age);		//指定调用父类中的构造方法
    		this.school = school;		
    		// TODO 自动生成的构造函数存根
    	}
    	
    	public String getSchool() {
    		return school;
    	}
    
    	public void setSchool(String school) {
    		this.school = school;
    	}
    	
    	//方法:print
    //	public void print(){	//覆写父类中的方法,扩大了权限
    //		super.print();			//调用父类中的print()方法
    //		System.out.println("student---->void print()");
    //	}
    	
    	//方法:getInfo	
    	public String getInfo(){											//覆写父类中的方法
    		return super.getInfo()+"学校"+this.school;		//扩充父类中的方法
    	}
    }
    
    
    public class extends_demo {
    
    	public static void main(String[] args) {
    		// TODO 自动生成的方法存根
    		student person_1 = new student("李四",18,"清华大学");
    //		person_1.setName("张三");
    //		person_1.setAge(10);
    //		person_1.setSchool("涵三中");
    //		System.out.println("姓名:"+person_1.getName()+"
    "+"年龄:"+person_1.getAge()+"
    "+"学校:"+person_1.getSchool());
    //		new student("张三",11,"三中").print();
    		System.out.println(person_1.getInfo());		//打印信息,调用覆写过的方法
    		person person_2 = new person("张三",18);
    		System.out.println(person_2.getInfo());
    	}
    
    }
    

    this和super的区别

    区别        this                         super

    1.属性访问    访问本类中的属性,如果本类中没有此属性      访问父类中的属性

             ,则从父类中继续查找

    2.方法      访问本类中的方法,如果本类中没有此方法     直接访问父类中的方法

             ,则从父类中继续查找

    3.调用构造    调用本类构造,必须放在构造方法的首行      调用父类构造,必须放在子类构造方法的首行

    4.特殊      表示当前对象                  无此概念

  • 相关阅读:
    git-guide
    jenkinsfile 庖丁解牛
    pipline预发的'拉取代码'的stage
    jenkinsfile的credentialsId
    jenkins配置ssh插件
    Rancher
    福至心灵篇-python
    生成器和迭代器思维导图(待补充)
    怎么样才能使内心平静?
    Java中的语法糖
  • 原文地址:https://www.cnblogs.com/tonglin0325/p/5235757.html
Copyright © 2011-2022 走看看