首页 > 其他 > 详细

super详解

时间:2021-07-21 09:30:11      阅读:26      评论:0      收藏:0      [点我收藏+]

Super详解

super的注意点:

  1. super调用父类的构造方法,必须在第一个

  2. super必须只能出现在子类的方法或者构造方法中

  3. super和this不能同时调用构造方法

this:

  1. 代表的对象不同:

    this:本身调用者这个对象

    super:代表父类对象的应用

    前提:

    this:没有继承也可以使用

    super :只能在继承条件的时候才可以使用

    构造方法:

    this():本类的构造

    super():父类的构造

package oop.Test03;
//学生是人
//子类继承了父类的全部方法!
public class Student extends Person{
   public Student(){
       //隐藏代码:调用了父类的无参构造
       super();//调用父类的构造器,必须要在子类构造器的第一行
      /* this("hello");*/
       System.out.println("Student 无参构造执行了");
  }
   private String name="顾";
?
   //ctrl+H//查看树形
   public void test(String name){
       System.out.println(name);
       System.out.println(this.name);
       System.out.println(super.name);
  }
   public void print(){
       System.out.println("student");
  }
   public void test1(){
       print();
       this.print();
       super.print();
?
  }
}
?
package oop.Test03;
/**
* @author 顾文杰
*/ //人
//java中所有的类都直接或者间接继承object类;
public class Person {
   public Person() {
       System.out.println("person无参执行了");
  }
?
   protected String name="guwenjie";
   public int money=100000000;
   public void say(){
       System.out.println("说了一句话");
  }
   public void print(){
       System.out.println("Person");
  }
}
?
package oop.Test03;
?
public class Application {
   public static void main(String[] args) {
       Student student=new Student();
       /*student.say();
       student.test("梦瑶");
       student.test1();
       System.out.println(student.money);*/
  }
?
}
?

 

super详解

原文:https://www.cnblogs.com/gwj0424/p/15037642.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!