Java中的this、super、final關(guān)鍵字詳解
更新時間:2023年09月18日 10:03:01 作者:努力的小鳴人
這篇文章主要介紹了Java中的this、super、final關(guān)鍵字詳解,它在方法內(nèi)部使用,表示這個方法所屬對象的引用,它在構(gòu)造器內(nèi)部使用,表示該構(gòu)造器正在初始化的對象,this 可以調(diào)用類的屬性、方法和構(gòu)造器,需要的朋友可以參考下
一、this
- 介紹:在Java中
- 它在方法內(nèi)部使用,表示這個方法所屬對象的引用
- 它在構(gòu)造器內(nèi)部使用,表示該構(gòu)造器正在初始化的對象
- this 可以調(diào)用類的屬性、方法和構(gòu)造器
- 何時使用:當在方法內(nèi)需要用到調(diào)用該方法的對象時,就用this
- 具體的:我們可以用this來區(qū)分屬性和局部變量:比如:this.name = name;
- 使用this,調(diào)用屬性、方法:
class Person{ // 定義Person類 private String name ; private int age ; public Person(String name,int age){ this.name = name ; this.age = age ; } public void getInfo(){ System.out.println("姓名:" + name) ; this.speak(); } public void speak(){ System.out.println(“年齡:” + this.age); } }
- 使用this,調(diào)用當前對象:
class Person{ // 定義Person類 String name; Person(String name){ this.name = name; } public void getInfo(){ System.out.println("Person類 --> " + this.name); } public boolean compare(Person p){ return this.name==p.name; } } public class PersonTest{ public static void main(String args[]){ Person per1 = new Person("張三") ; Person per2 = new Person("李四") ; per1.getInfo() ; // 當前調(diào)用getInfo()方法的對象是per1 per2.getInfo() ; // 當前調(diào)用getInfo()方法的對象是per2 boolean b = per1.compare(per2); } }
- 使用this調(diào)用本類的構(gòu)造器:
class Person{ // 定義Person類 private String name ; private int age ; public Person(){ // 無參構(gòu)造器 System.out.println("新對象實例化") ; } public Person(String name){ this(); // 調(diào)用本類中的無參構(gòu)造器 this.name = name ; } public Person(String name,int age){ this(name) ; // 調(diào)用有一個參數(shù)的構(gòu)造器 this.age = age; } public String getInfo(){ return "姓名:" + name + ",年齡:" + age ; } }
二、super
- 使用super來調(diào)用父類中的指定操作:
- super可用于訪問父類中定義的屬性
- super可用于調(diào)用父類中定義的成員方法
- super可用于在子類構(gòu)造器中調(diào)用父類的構(gòu)造器
- super和this的用法相像,this代表本類對象的引用,super代表父類的內(nèi)存空間的標識
- super的追溯不僅限于直接父類
- 關(guān)鍵字super舉例:
class protected Person { String name = "張三"; protected int age; public String getInfo() { return "Name: " + name + "\nage: " + age; } } class Student extends Person { protected String name = "李四"; private String school = "New Oriental"; public String getSchool() { return school; } public String getInfo() { return super.getInfo() + "\nschool: " + school; } } public class StudentTest { public static void main(String[] args) { Student st = new Student(); System.out.println(st.getInfo()); } }
調(diào)用父類的構(gòu)造器
- 子類中所有的構(gòu)造器默認都會訪問父類中空參數(shù)的構(gòu)造器
- 當父類中沒有空參數(shù)的構(gòu)造器時,子類的構(gòu)造器必須通過this(參數(shù)列表)或者super(參數(shù)列表)語句指定調(diào)用本類或者父類中相應(yīng)的構(gòu)造器。同時,只能”二選一”,且必須放在構(gòu)造器的首行
- 如果子類構(gòu)造器中既未顯式調(diào)用父類或本類的構(gòu)造器,且父類中又沒有無參的構(gòu)造器,則編譯出錯
- 調(diào)用父類構(gòu)造器舉例:
public class Person { private String name; private int age; private Date birthDate; public Person(String name, int age, Date d) { this.name = name; this.age = age; this.birthDate = d; } public Person(String name, int age) { this(name, age, null); } public Person(String name, Date d) { this(name, 30, d); } public Person(String name) { this(name, 30); } } public class Student extends Person { private String school; public Student(String name, int age, String s) { super(name, age); school = s; } public Student(String name, String s) { super(name); school = s; } // 編譯出錯: no super(),系統(tǒng)將調(diào)用父類無參數(shù)的構(gòu)造器。 public Student(String s) { school = s; } }
this與super的區(qū)別
三、final
可使用關(guān)鍵字 final 來修飾,表示 最終的
- final標記的類不能被繼承
- final標記的方法不能被子類重寫
- final標記的變量(成員變量或局部變量)即稱為常量
- 關(guān)鍵字final應(yīng)用舉例:
public final class Test { public static int totalNumber = 5; public final int ID; public Test() { ID = ++totalNumber; // 可在構(gòu)造器中給final修飾的“變量”賦值 } public static void main(String[] args) { Test t = new Test(); System.out.println(t.ID); final int I = 10; final int J; J = 20; J = 30; // 非法 } }
總結(jié):this、super、final三個關(guān)鍵詞的靈活運用會很好地改變代碼量,了解其中細節(jié)也會減少平時那令人頭疼的bug哈,做個筆記經(jīng)??纯?/p>
到此這篇關(guān)于Java中的this、super、final關(guān)鍵字詳解的文章就介紹到這了,更多相關(guān)Java中的關(guān)鍵字內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
淺談Java的Synchronized鎖原理和優(yōu)化
這篇文章主要介紹了Java的Synchronized鎖原理和優(yōu)化,synchronized的作用是保證在同一時刻, 被修飾的代碼塊或方法只會有一個線程執(zhí)行,以達到保證并發(fā)安全的效果,需要的朋友可以參考下2023-05-05Spring Boot 啟動、停止、重啟、狀態(tài)腳本
今天給大家分享Spring Boot 項目腳本(啟動、停止、重啟、狀態(tài)),通過示例代碼給大家介紹的非常詳細,需要的朋友參考下吧2021-06-06