JAVA的extends使用方法
生活随笔
收集整理的這篇文章主要介紹了
JAVA的extends使用方法
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
???? ?理解繼承是理解面向對象程序設計的關鍵。在Java中,通過keywordextends繼承一個已有的類,被繼承的類稱為父類(超類,基類),新的類稱為子類(派生類)。在Java中不同意多繼承。
(1)繼承
在eclipse運行得:
Animal breathe!
Animal breathe!
.java文件里的每一個類都會在目錄bin下生成一個相應的.class文件。運行結果說明派生類繼承了父類的全部方法。
(2)覆蓋
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 stubAnimal an = new Animal();Fish fn = new Fish();an.breathe();fn.breathe();} }運行結果:
Animal breathe
Fish breathe
在子類中定義一個與父類同名,返回類型,參數類型均同樣的一個方法,稱為方法的覆蓋。方法的覆蓋發生在子類與父類之間。另外,可用super提供對父類的訪問。
?
參考原文:http://tieba.baidu.com/f?kz=295170500
參考原文:http://zhidao.baidu.com/question/25517733.html
總結
以上是生活随笔為你收集整理的JAVA的extends使用方法的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Ubuntu 11.10不得不知的快捷键
- 下一篇: 【CSS3 DEMO】扑克正反面翻牌效果