1 super注意点:
2 1. super调用父类的构造方法,必须在构造方法的第一个
3 2. super 必须只能出现在子类的方法或者构造方法中!
4 3.super和this不能同时调用构造方法!
5 Vs this:
6 代表的对象不同:
7 this:本身调用者这个对象
8 super:代表父类对象的应用
9
10 前提
11 this:没哟继承也可以使用
12 super:只能在继承条件才可以使用
13 构造方法
14 this() ;本类的构造,
15 super():父类的构造!
1 package com.oop;
2
3 import com.oop.demo5.Student;
4
5 public class Application {
6 public static void main(String[] args) {
7 Student student = new Student( );
8 //student. test("小明" ) ;
9 //student.test1();
10 }
11 }
1 package com.oop.demo5;
2 //学生 is 人:派生类,子类
3 //子类继承了父类,就会拥有父类的全部方法!
4
5 public class Student extends Person {
6 public Student() {
7 System.out.println("Student无参执行了");
8 }
9
10 private String name ="qinjiang" ;
11
12 public void print() {
13 System.out.println("Student");
14 }
15 public void test1(){
16 print();
17 this.print();
18 super . print();
19 }
20
21 public void test(String name ){
22 System. out. println(name);
23 System. out. println(this.name);
24 System. out . println(super.name) ;
25 }
26 }
1 package com.oop.demo5;
2 //在Java中, 所有的类,都默认直接或者间接继承object
3 //Person人: 父类
4 //ctrl+H 展开继承关系树
5 public class Person {
6 public Person() {
7 //隐藏代码:调用了父类的无参构造
8 super();//调用父类的构造器,必须要在子类构造器的第一行
9 System.out.println("Person无参执行了");
10 }
11
12 protected String name = "kuangshen" ;
13 //私有的东西无法被继承!
14 public void print(){
15 System . out . println("Person");
16 }
17 }