更新時(shí)間:2022-06-20 09:55:28 來(lái)源:動(dòng)力節(jié)點(diǎn) 瀏覽1822次
動(dòng)力節(jié)點(diǎn)小編告訴大家,Java父類(lèi)和子類(lèi)是繼承關(guān)系,Java 支持繼承,這是一種 OOP 概念,其中一個(gè)類(lèi)獲取另一個(gè)類(lèi)的成員(方法和字段)。
您可以從另一個(gè)類(lèi)繼承一個(gè)類(lèi)的成員,使用 extends 關(guān)鍵字:
class A extends B{}
繼承其他屬性的類(lèi)稱(chēng)為子類(lèi)(派生類(lèi)、子類(lèi)),繼承屬性的類(lèi)稱(chēng)為父類(lèi)(基類(lèi)、超類(lèi))。
以下是演示繼承的示例。在這里,我們有兩個(gè)類(lèi),即 Sample 和 MyClass。其中 Sample 是父類(lèi),名為 MyClass 的類(lèi)是子類(lèi)。
class Sample{
public void display(){
// Java Arrays with Answers
System.out.println("Hello this is the method of the super class");
}
}
public class MyClass extends Sample {
public void greet(){
System.out.println("Hello this is the method of the sub class");
}
public static void main(String args[]){
MyClass obj = new MyClass();
obj.display();
obj.greet();
}
}
輸出
Hello this is the method of the super class
Hello this is the method of the sub class
以上就是關(guān)于“淺析Java父類(lèi)和子類(lèi)”的介紹,大家如果想了解更多相關(guān)知識(shí),可以關(guān)注一下動(dòng)力節(jié)點(diǎn)的Java教程,里面的課程內(nèi)容更加全面細(xì)致,相信對(duì)大家的學(xué)習(xí)會(huì)有所幫助。
相關(guān)閱讀
Java實(shí)驗(yàn)班
0基礎(chǔ) 0學(xué)費(fèi) 15天面授
Java就業(yè)班
有基礎(chǔ) 直達(dá)就業(yè)
Java夜校直播班
業(yè)余時(shí)間 高薪轉(zhuǎn)行
Java在職加薪班
工作1~3年,加薪神器
Java架構(gòu)師班
工作3~5年,晉升架構(gòu)
提交申請(qǐng)后,顧問(wèn)老師會(huì)電話(huà)與您溝通安排學(xué)習(xí)