日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > java >内容正文

java

Java-Super

發布時間:2024/9/27 java 25 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Java-Super 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

super 調用父類屬性

public class Person {// public > protected > default > private// public// protected --受保護的,可以繼承// default -- 不寫就是默認,可以繼承// private -- 父類私有,(private 不能繼承,不能直接訪問,可以通過get set間接訪問 )protected String name = "Person.wang"; } public class Student extends Person{private String name = "Student.wang";public void test(String name){System.out.println(name); // 王System.out.println(this.name); // Student.wangSystem.out.println(super.name); // Person.wang}} public class Application {public static void main(String[] args) {Student student = new Student();student.test("王");} }

super 調用父類方法

public class Person {protected String name = "Person.wang";public void print(){System.out.println("Person");} } public class Student extends Person{private String name = "Student.wang";public void print(){System.out.println("Student");}public void test1() {print(); // Studentthis.print(); // Studentsuper.print(); // Person} } public class Application {public static void main(String[] args) {Student student = new Student(); // student.test("王");student.test1();} }

private 私有的無法被繼承,使用super也不能調用

super與構造函數

public class Person {public Person() {System.out.println("Person無參執行了");} } public class Student extends Person{public Student() {// 隱藏代碼: 調用了父類的無參構造,super();寫不寫都行 // super(); // 調用父類構造器,必須要在子類構造器的第一行// this(""); // 調用構造器,必須要在第一行System.out.println("Student無參執行了");} } public class Application {public static void main(String[] args) {Student student = new Student();} }

super注意點:1. super 調用父類的構造方法,必須在構造方法的第一個2. super 必須只能出現在子類的方法或者構造方法中3. super和this 不能同時調用構造方法!因為他們都要寫在方法的第一行 VS this:代表的對象不同:this: 本身調用著的這個對象super: 代表父類對象的應用前提this: 沒有繼承也可以使用super: 只能在繼承條件才可以使用構造方法:this() ; 本類的構造super(); 父類的構造

https://www.bilibili.com/video/BV12J41137hu?p=69

總結

以上是生活随笔為你收集整理的Java-Super的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。