extends在java中什么意思(Java中的关键字有哪些)
生活随笔
收集整理的這篇文章主要介紹了
extends在java中什么意思(Java中的关键字有哪些)
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
繼承是理解面向對象程序設計的關鍵。在Java中,通過關鍵字extends繼承一個已有的類,被繼承的類稱為父類(超類,基類),新的類稱為子類(派生類)。在Java中不允許多繼承。
class Animal{
void eat(){
System.out.println("Animal eat");
}
void sleep(){
System.out.println("Animal sleep");
}
void breathe(){
System.out.println("Animal breathe");
}
}
class Fish extends Animal{
}
public class TestNew {
public static void main(String[] args) {
// TODO Auto-generated method stub
Animal an = new Animal();
Fish fn = new Fish();
an.breathe();
fn.breathe();
}
}
登錄后復制
在eclipse執行得:Animal breathe!
Animal breathe!
.java文件中的每個類都會在文件夾bin下生成一個對應的.class文件。執行結果說明派生類繼承了父類的所有方法。
覆蓋
class Animal{
void eat(){
System.out.println("Animal eat");
}
void sleep(){
System.out.println("Animal sleep");
}
void breathe(){
System.out.println("Animal breathe");
}
}
class Fish extends Animal{
void breathe(){
System.out.println("Fish breathe");
}
}
public class TestNew {
public static void main(String[] args) {
// TODO Auto-generated method stub
Animal an = new Animal();
Fish fn = new Fish();
an.breathe();
fn.breathe();
}
}
登錄后復制
執行結果:
Animal breathe
Fish breathe
在子類中定義一個與父類同名,返回類型,參數類型均相同的一個方法,稱為方法的覆蓋。方法的覆蓋發生在子類與父類之間。另外,可用super提供對父類的訪問。
推薦教程: 《java教程》
以上就是extends在java中什么意思的詳細內容,更多請關注風君子博客其它相關文章!
總結
以上是生活随笔為你收集整理的extends在java中什么意思(Java中的关键字有哪些)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: dch驱动什么意思(提示GPU驱动程序过
- 下一篇: 苹果 Mac mini 好搭档:小米 R