例1:



package com.oop.demo05;

//定义一个Person父类
public class Person {
protected String name="小明";

}
/*
super关键字可访问父类的成员、成员方法和构造方法
*/

//定义Boy类继承Person类
class Boy extends Person{
private String name="小红";

public void test(String name){
System.out.println(name); //输出张三
System.out.println(this.name); //输出小红
System.out.println(super.name); //输出小明
}

}


package com.oop.demo05;

import com.oop.demo01.Student;
//定义一个测试类
public class App {
public static void main(String[] args) {
Boy boy = new Boy(); //实例化Boy对象
boy.test("张三");

}
}


输出:

Super关键字_JAVA

 

例2:



1 package com.oop.demo05;
2
3 //定义一个Person父类
4 public class Person {
5 protected String name="小明";
6 public void print(){
7 System.out.println("Person");
8 }
9 }
10 /*
11 super关键字可访问父类的成员、成员方法和构造方法
12 */
13
14 //定义Boy类继承Person类
15 class Boy extends Person{
16 //private String name="小红";
17 //定义一个print方法
18 public void print(){
19 System.out.println("Boy");
20 }
21
22 public void test1(){
23 print(); //this.print()=print(),this省略了,调用自己的方法
24 this.print(); //调用自己的方法
25 super.print();//调用父类的print方法
26
27 }
28
29
30 // public void test(String name){
31 // System.out.println(name); //输出张三
32 // System.out.println(this.name); //输出小红
33 // System.out.println(super.name); //输出小明
34 }
35
36
37
38 package com.oop.demo05;
39
40 import com.oop.demo01.Student;
41 //定义一个测试类
42 public class App {
43 public static void main(String[] args) {
44 Boy boy = new Boy(); //实例化Boy对象
45 //boy.test("张三");
46 boy.test1();
47
48
49 }
50 }


输出:

Super关键字_super关键字_02

 例3:无参构造方法的继承



1 package com.oop.demo05;
2
3 //定义一个Person父类
4 public class Person {
5 protected String name="小明";
6
7 public Person() {
8 System.out.println("调用父类Person的无参构造");
9 }
10
11 // public void print(){
12 // System.out.println("Person");
13 // }
14 }
15 /*
16 super关键字可访问父类的成员、成员方法和构造方法
17 */
18
19 //定义Boy类继承Person类
20 class Boy extends Person{
21 //private String name="小红";
22 //定义一个print方法
23
24
25 public Boy() {
26 super();
27 /*
28 super();原本是隐藏的,可不写
29 调用父类的构造器,必须放在子类构造器的第一行
30 */
31 System.out.println("调用了子类Boy的无参构造");
32 }
33
34 // public void print(){
35 // System.out.println("Boy");
36 // }
37
38 // public void test1(){
39 // print(); //this.print()=print(),this省略了,调用自己的方法
40 // this.print(); //调用自己的方法,输出Boy
41 // super.print();//调用父类的print方法
42 //
43 // }
44
45
46 // public void test(String name){
47 // System.out.println(name); //输出张三
48 // System.out.println(this.name); //输出小红
49 // System.out.println(super.name); //输出小明
50 }
51
52
53
54 package com.oop.demo05;
55
56 import com.oop.demo01.Student;
57 //定义一个测试类
58 public class App {
59 public static void main(String[] args) {
60 Boy boy = new Boy(); //实例化Boy对象
61 //boy.test("张三");
62 //boy.test1();
63
64
65 }
66 }


输出:

Super关键字_JAVA_03

super注意点:

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

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

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

和this的异同:

    代表的对象不同:

              this:本身调用者这个对象

              super:代表父类对象的应用

   前提:

           this:不用继承也可以使用

           super:只能在继承中使用

构造方法

           this():可调用本类的构造

           super():可调用父类的构造