java 抽象類 final_final/抽象類/interface

lesson?Thirteen                          2018-05-10?02:10:43

final:

最終的,可以修飾類、屬性、方法

1.final修飾類:這個類就不能被繼承,如:String類,StringBuffer類,System類

1 class SubString extends String{

2 //The type SubString cannot subclass the final class String

3 }

4 final class A{

5

6 }

7 class B extends A{

8 //The type B cannot subclass the final class A

9 }

2.final修飾方法:不能被重寫 如:object的getclass();方法(獲取當前對象的類名)

1 class C{

2 public final void f() {

3 System.err.println("被final修飾的方法1");

4 }

5 }

6 class D extends C{

7 public void f() {

8 System.err.println("被final修飾的方法2");

9 //Cannot override the final method from C

10 }

11 }

3.final修飾屬性:此屬性就是一個常量,習慣常量用全大寫表示

3.1此常量不能默認初始化

3.2可以顯式賦值、代碼塊、構造器

1 class E{

2 final int INT = 123;

3

4 public void e() {

5 System.err.println(INT);

6 INT= 12;

7 //The final field E.INT cannot be assigned

8 }

9 }

abstract:

抽象的,可以修飾類、方法? ?即,抽象類,抽象方法?不能修飾屬性、構造器、private、final、static

1.1抽象類

1.1.1不可以被實例化

1.1.2抽象類有構造器(凡是類都有構造器)

1.1.3抽象方法所在的類,一定是抽象類

1.1.4抽象類中可以沒有抽象方法

1 abstract class eemployeee{

2 private String name;

3 private int id;

4 private double salary;

5 public String getName() {

6 return this.name;

7 }

8 public void setName(String name) {

9 this.name = name;

10 }

11 public int getId() {

12 return this.id;

13 }

14 public void setId(int id) {

15 this.id = id;

16 }

17 public double getSalary() {

18 return this.salary;

19 }

20 public void setSalary(double salary) {

21 this.salary = salary;

22 }

23

24 public abstract void work();

25 //這就是抽象方法,也可以沒有

26 }

1.2抽象方法

1.2.1格式:沒有方法體,包括{}。 如:public abstract void work();

1.2.2抽象方法只保留方法的功能,而具體的執行,交給繼承抽象類的子類,由子類重寫此抽象方法

1.2.3若子類繼承抽象類,并重寫了所以的抽象方法,則可以實例化,反之則不能

1 class Manager extends eemployeee{

2 private double bonus;

3 public double getBonus() {

4 return this.bonus;

5 }

6 public void setBonus(double bonus) {

7 this.bonus = bonus;

8 }

9 public void work(){

10 System.out.println("監督工作");

11

12 }

13 }

14

15 class CommonEmployee extends eemployeee{

16 public void work(){

17 System.out.println("工人在流水線工作");

18

19 }

20 }

interface:

接口,與類并行的概念

interface(接口)是與類并行的概念

1.接口可是看作是一個特殊的抽象類,是常量與抽象方法的集合。不能包含變量和一般的方法

2.接口是沒有構造器的

3.接口定義的就是一種功能,可以被類實現(implements)

如:class DD extends CC implements A

1 interface A {

2 //常量:所有的常量都用 public static final

3 int j =1;

4 public static final int i =0;

5 //抽象方法:所有的都用 public abstract修飾

6 void method1();

7 public abstract void method2();

8 }

4.實現接口的類,必須重寫其中所有的抽象方法,方可實例化,不然,此類仍為抽象類

1 interface A {

2 //常量:所有的常量都用 public static final

3 int j =1;

4 public static final int i =0;

5 //抽象方法:所有的都用 public abstract修飾

6 void method1();

7 public abstract void method2();

8 }

9

10

11

12 abstract class BB implements A {

13

14 }

5.類可以實現多個接口-----JAVA中的類是單繼承的

1 class CC extends DD implements A,B {

2 public void method1(){

3

4 }

5 public void method2(){

6

7 }

8 public void method3(){

9

10 }

11 }

6.接口與接口之間是繼承關系,而且可以多繼承

1 interface A {

2 //常量:所有的常量都用 public static final

3 int j =1;

4 public static final int i =0;

5 //抽象方法:所有的都用 public abstract修飾

6 void method1();

7 public abstract void method2();

8 }

9 interface B{

10 public abstract void method3();

11 }

12 interface C extends B,A{

13 public abstract void method4();

14 }

8f900a89c6347c561fdf2122f13be562.png

961ddebeb323a10fe0623af514929fc1.png

1 interface Runner{

2 public void Strat();

3 abstract void Run();

4 public abstract void Stop();

5

6 }

7

8 class Person implements Runner{

9

10 @Override

11 public void Strat() {

12 System.out.println("人在走路");

13 }

14

15 @Override

16 public void Run() {

17 System.out.println("人在跑");

18 }

19

20 @Override

21 public void Stop() {

22 System.out.println("人在休息");

23 }

24

25 public void Dance() {

26 System.out.println("人在跳舞");

27 }

28

29 }

30 class Car implements Runner{

31

32 @Override

33 public void Strat() {

34 System.out.println("汽車啟動");

35 }

36

37 @Override

38 public void Run() {

39 System.out.println("汽車在行駛");

40 }

41

42 @Override

43 public void Stop() {

44 System.out.println("汽車停了");

45 }

46

47 public void fillFuel(){

48 System.out.println("汽車在加油");

49 }

50 public void crack() {

51 System.out.println("撞車了");

52 }

53

54 }

55 class Bird implements Runner{

56

57 @Override

58 public void Strat() {

59 System.out.println("鳥在起飛");

60 }

61

62 @Override

63 public void Run() {

64 System.out.println("鳥在跑");

65 }

66

67 @Override

68 public void Stop() {

69 System.out.println("鳥停下了");

70 }

71

72 public void fly() {

73 System.out.println(" 鳥在飛 ");

74 }

75

76 }

interface的練習

8f900a89c6347c561fdf2122f13be562.png

961ddebeb323a10fe0623af514929fc1.png

1 public static void main(String[] args) {

2

3 interfaceTest1 i = new interfaceTest1();

4 Fly duck = new Duck();

5 i.test3(duck);

6

7 new interfaceTest1().test1(new Duck());

8 new interfaceTest1().test2(new Duck());

9 new interfaceTest1().test3(new Duck());

10 new interfaceTest1().test4(new Duck(), new Duck(), new Duck());

11 }

12

13 public void test1(Run duck){

14 duck.run();

15 // duck.fly();

16 // duck.swim();

17 }

18 public void test2(Swim duck){

19 // duck.fly();

20 duck.swim();

21 // duck.run();

22 }

23 public void test3(Fly duck){

24 // duck.run();

25 // duck.swim();

26 duck.fly();

27 }

28 public void test4(Run r, Swim s, Fly f){

29 r.run();

30 s.swim();

31 f.fly();

32 }

33

34

35

36

37

38

39

40 interface Run{

41 void run();

42 }

43 interface Swim{

44 void swim();

45 }

46 interface Fly{

47 void fly();

48 }

49

50 class Duck implements Run,Swim,Fly{

51

52 @Override

53 public void fly() {

54 System.out.println("flying");

55 }

56

57 @Override

58 public void swim() {

59 System.out.println("swimming");

60 }

61

62 @Override

63 public void run() {

64 System.out.println("running");

65 }

66

67 }

interface多態的運用1

8f900a89c6347c561fdf2122f13be562.png

961ddebeb323a10fe0623af514929fc1.png

1 public static void main(String[] args) {

2 Computer computer = new Computer();

3 computer.work(new Flash());

4 Printer p = new Printer();

5 computer.work(p);

6

7

8 USB phone = new USB(){

9

10 @Override

11 public void start() {

12 // TODO Auto-generated method stub

13 System.out.println("start working");

14 }

15

16 @Override

17 public void stop() {

18 System.out.println("stop working");

19 }

20

21 };

22

23

24 }

25

26

27

28

29

30

31 interface USB{

32 public static final int i = 10;

33 public abstract void start();

34 public abstract void stop();

35 }

36

37 class Flash implements USB {

38

39 @Override

40 public void start() {

41 System.out.println("閃存開始工作");

42 }

43

44 @Override

45 public void stop() {

46 System.out.println("閃存停止工作");

47 }

48

49 }

50

51 class Printer implements USB{

52

53 @Override

54 public void start() {

55 System.out.println("打印機開始工作");

56 }

57

58 @Override

59 public void stop() {

60 System.out.println("打印機停止工作");

61 }

62

63 }

interface多態的運用2

如您對本文有疑問或者有任何想說的,請點擊進行留言回復,萬千網友為您解惑!

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/534459.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/534459.shtml
英文地址,請注明出處:http://en.pswp.cn/news/534459.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

java char i=2+#039;2#039;;_P039 二維數組的字符按列存放到字符串中 ★★

所屬年份:2010.9;2011.9;2012.3請編寫函數fun,該函數的功能是:將M行N列的二維數組中的字符數據,按列的順序依次放到一個字符串中。例如,若二維數組中的數據為W W W WS S S SH H H H則字符串中的內容應是:WSHWSHWSHWSH。#include#define M 3#d…

java io中斷_JDK源碼閱讀:InterruptibleChannel 與可中斷 IO

來源:木杉的博客 ,imushan.com/2018/08/01/java/language/JDK源碼閱讀-InterruptibleChannel與可中斷IO/Java傳統IO是不支持中斷的,所以如果代碼在read/write等操作阻塞的話,是無法被中斷的。這就無法和Thead的interrupt模型配合使…

java值棧_Struts2學習筆記-Value Stack(值棧)和OGNL表達式

只是本人的Struts2學習筆記,關于Value Stack(值棧)和OGNL表達式,把我知道的都說出來,希望對大家有用。一,值棧的作用記錄處理當前請求的action的數據。二,小例子有兩個action:Action1和Action2Action1有兩個…

php項目實戰流程_一個完整的php流程管理實例代碼分享

1. 添加新流程頁面:請選擇流程節點:session_start();include("../DBDA.class.php");$db new DBDA();$suser "select * from users";$auser $db->Query($suser);foreach($auser as $v){echo " {$v[2]} ";}?>$att…

php cdata,PHPcdata處理(詳細介紹)_PHP教程

PHPcdata處理(詳細介紹)_PHP教程當時在網上找了一個CDATA的轉換器, 修改之后, 將CDATA標簽給過濾掉。如下代碼如下:// States://// out// // // // // // // // in// ]// ]]//// (Yes, the states a represented by strings.)//$state out;$a s…

PHP 與go 通訊,Golang和php通信

不同語言之間的通信方式有很多種,這里我介紹一種最簡單通信方式,json-rpc。Golang自帶json-rpc包,使用起來十分簡單,示例如下,提供一個簡單echo server。package mainimport ("fmt""net""net…

php 接口日志,PHP 開發 APP 接口--錯誤日志接口

APP 上線以后可能遇到的問題:① APP 強退② 數據加載失敗③ APP 潛在問題錯誤日志需要記錄的內容數據表 error_log 字段:idapp_id:app 類別 iddid:客戶端設備號version_id:版本號version_mini:小版本號erro…

php 空模塊,tp5.1配置空模塊,空方法

config/app.php//默認的空模塊名empty_module>index,controller/Error.php<?php namespace app\index\controller;use Env;use think\Controller;class Error extends Controller {//Db::connect(db_ck)//全局MISS路由 在route.php里面設置找不到控制器默認處理//Route:…

centos7php自啟動,centos7系統下nginx安裝并配置開機自啟動操作

這篇文章主要介紹了centos7系統下nginx安裝并配置開機自啟動操作方法,非常不錯&#xff0c;具有參考借鑒價值&#xff0c;需要的朋友可以參考下這篇文章主要介紹了centos7系統下nginx安裝并配置開機自啟動操作方法,非常不錯&#xff0c;具有參考借鑒價值&#xff0c;需要的朋友…

時鐘php,php+js液晶時鐘

php代碼$size_small5;//液晶寬度$size_big25;//液晶長度$distance10;//間距$color_back"#DDDDDD";$color_dark"#CCCCCC";$color_light"#000000";$number0;?>Timer|www.ibtf.net|www.bitefu.netfunction swapcolor(obj,onoff)//改變顏色{if (…

r和matlab學哪個,初學者求教‘r*’是什么意思啊

該樓層疑似違規已被系統折疊 隱藏此樓查看此樓PLOT(X,Y,S) where S is a character string made from one elementfrom any or all the following 3 columns:b blue . point - solidg green o circle : dottedr red x x-mark -. dashdotc cyan plus -- dashedm magenta * star…

php swoole 心跳,聊聊swoole的心跳

來自&#xff1a;桶哥的一篇關于swoole的心跳的文章&#xff0c;作為Swoole顧問(顧得上就問,是為「顧問」)得推一下這篇文章&#xff0c;最后只留下一配置&#xff0c;其實我也不是太明白原理&#xff0c;我在想如果是局域網里還需要心跳&#xff1f;—————————————…

mysql 查詢 投影,MySql-連接查詢

連接查詢Chloe 友好支持多表連接查詢&#xff0c;一切都可以用 lambda 表達式操作&#xff0c;返回類型可以是自定義類型&#xff0c;也可以是匿名類型。強類型開發&#xff0c;編譯可見錯誤&#xff0c;容錯率高。1.建立連接&#xff1a;var user_city_province context.Quer…

php 遞歸欄目名疊加,thinkPHP實現遞歸循環欄目并按照樹形結構無限極輸出的方法,thinkphp遞歸...

thinkPHP實現遞歸循環欄目并按照樹形結構無限極輸出的方法&#xff0c;thinkphp遞歸本文實例講述了thinkPHP實現遞歸循環欄目并按照樹形結構無限極輸出的方法。分享給大家供大家參考&#xff0c;具體如下&#xff1a;這里使用thinkphp遞歸循環欄目按照樹形結構無限極輸出&#…

php cannot call constructor,安裝ECshop普遍問題的解決方法

安裝時的問題&#xff1a;1.Strict Standards: Non-static method cls_image::gd_version() should not be called statically in /usr/local/httpd2/htdocs/upload/install/includes/lib_installer.php on line 31解決&#xff1a;找到install/includes/lib_installer.php中的…

wind試用版 matlab,免費產品試用 - MATLAB Simulink

請選擇其一AlabamaAlaska美屬薩摩亞APO/FPO AAAPO/FPO AEAPO/FPO APArizonaArkansasCaliforniaCaroline IslandsColoradoConnecticutDelawareDistrict of ColumbiaFlorida格魯吉亞關島HawaiiIdahoIllinoisIndianaIowaKansasKentuckyLouisianaMaineMariana Islands馬紹爾群島Mar…

php yii2 sns,GitHub - yggphpcoder/iisns: 基于 yii2 的 sns 社區系統,一站式解決社區建站...

iisns - 地球村入口iiSNS 是基于 yii2 的 SNS 社區系統&#xff0c;一站式解決社區建站。可以寫文章&#xff0c;做記錄&#xff0c;上傳圖片&#xff0c;論壇聊天等。還可以用來做內容管理系統(CMS)。iiSNS 是一個免費的開源項目&#xff0c;在 MIT 許可證下授權發布。特點與功…

php mvc 商城,基于MVC框架的小型網上商城設計

2&#xff0e;本人對課題任務書提出的任務要求及實現預期目標的可行性分析基于MVC框架的小型網上商城實現的功能&#xff1a;商品的瀏覽、查詢、購買&#xff0c;會員注冊以及會員訂單的查詢等&#xff0c;方便商場活動&#xff0c;該系統基本實現了網上商城的應有功能。該系統…

php 做更新進度條,PHP exec()后更新Bootstrap進度條

我使用PHP來運行一個python腳本&#xff0c;并且在腳本執行后需要更新一個進度條。進度條更新后&#xff0c;將執行另一個腳本&#xff0c;依此類推。這里是我的代碼如此的票價。我試圖用JavaScript來實現。它沒有解決Button Textif (isset($_POST[turn])){exec("sudo pyt…

zblog php和asp功能,ZBlog是否適合PHP或ASP?我們該如何選擇?

我最近玩了zblog一段時間&#xff0c;對于大多數第一次聯系zblog的博客&#xff0c;他們會問zblog是否適合PHP或ASP&#xff1f;我們該如何選擇&#xff1f;事實上&#xff0c;我真的不明白這個問題。我個人更喜歡PHP。今天我將整理出來并對PHP版本和ASP版本進行比較&#xff0…