日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

java 抽象类 final_final/抽象类/interface

發布時間:2025/3/8 编程问答 22 豆豆
生活随笔 收集整理的這篇文章主要介紹了 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 }

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的練習

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

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

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

總結

以上是生活随笔為你收集整理的java 抽象类 final_final/抽象类/interface的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。