Super详解(重点)
生活随笔
收集整理的這篇文章主要介紹了
Super详解(重点)
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
Super詳解(重點)
super注意點;
1. super調用父類的方法,必須在構造方法的第一個2. super必須只能出現在子類的方法或者構造方法中!3. super和this 不能同時調用構造方法!Vs this:
? 代表的對象不同:
? this:本身調用者這個對象
? super:代表父類對象的應用
? 前提
? this:沒有繼承也可以使用
? super:只能在繼承條件下才可以使用
? 構造方法
? this();本類的構造
? super(); 父類的構造
package com.oop;import com.oop.demo05.Student;//一個項目應該這存在一個main方法 public class Application {public static void main(String[] args) {Student student= new Student();// student.test("我不會");//student.test1();} } package com.oop.demo05;//java中,所有的類,都默認直接或者繼承object //Person 人:父類或基類 public class Person {public Person() {System.out.println("Person無參執行");}protected String name="你好";//私有的東西無法被繼承!public void print(){System.out.println("Person");}} package com.oop.demo05;//Student 學生 is 人:派生類或子類 //子類繼承了父類,會擁有父類的全部方法; public class Student extends Person{public Student() {//隱藏代碼:調用了父類的無參構造super();//調用父類的構造器,必須要在子類構造器的第一行。System.out.println("Student無參執行");}private String name="不知道";public void print() {System.out.println("Student");}public void test1(){print();//Studentthis.print();//Studentsuper.print();//Person}public void test(String name){System.out.println(name);//我不會System.out.println(this.name);//不知道System.out.println(super.name);//你好}}總結
以上是生活随笔為你收集整理的Super详解(重点)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: pip 超时解决
- 下一篇: 动态修改网页icon图标