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

希望与广大网友互动??

点此进行留言吧!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值