菜鸟的博客

纵有疾风起,人生不言弃。

导航

2023.7.23 周日:继承

 1 //例:public class Students extends Person{}
 2 //关键字extends
 3 //在Java中所有的类都会直接或者间接继承object类
 4 //在Java中只有单继承没有多继承
 5 /////////////////////super
 6 //main
 7 public class Main {
 8     public static void main(String[] args){
 9     Student s1 = new Student();
10     s1.test("不良帅");
11     }
12 }
13 //父类
14 public class Person {
15    protected String name = "父类名字";
16 }
17 //子类
18 public class Student extends Person {
19    private String name = "子类名字";
20    public void test(String name) {
21        System.out.println(name);
22        System.out.println(this.name);
23        System.out.println(super.name);
24    }
25 }
26 //输出结果 
27 /*
28 不良帅
29 子类名字
30 父类名字
31 */
32 /////////////////////
33 /*
34 super注意点
35 1.super调用父类的构造必须在第一行
36 2.super只能出现在子类的方法或者构造方法中
37 3.super和this不能同时调用构造方法
38 VS  this
39 代表的对象不同
40 this:本身调用者这个对象
41 super:代表父类对象的应用
42 前提
43 this:没有继承就可以使用
44 super:只能在继承条件下才可以使用
45 构造方法
46 this():本类的构造
47 super():父类的构造
48 */

 

posted on 2023-07-23 18:49  hhmzd233  阅读(13)  评论(0)    收藏  举报