好得很程序员自学网

<tfoot draggable='sEl'></tfoot>

Java基础巩固抽象类与接口详解

抽象类与接口

1、抽象类

1.1、什么是抽象类

如果一个类中没有包含足够的信息来描绘一个具体的对象,这样的类就是抽象类

1.2、抽象类的用法

一个类如果被 abstract 修饰称为抽象类,抽象类中被 abstract 修饰的方法称为抽象方法,抽象方法不用给出具体的实现体。

?

1

2

3

4

5

6

7

8

9

10

11

abstract class Shape{

    /* public String name;

     public int age;

     public void func(){

         System.out.println("ada");

     }

     public static void func2(){

         System.out.println("akjdhkjasd");

     }*/

     public abstract void draw(); //抽象方法

}

注意:抽象类也是类,内部可以包含普通方法和属性,甚至构造方法

1.3、抽象类特点(限制条件)

1、使用abstract 修饰的类,就是抽象类

2、使用abstract 修饰的方法,就是抽象方法

3、抽象类不能够进行实例化new 抽象类();

4、里面的成员变量和成员方法都是和普通类是一样的,只不过就是不能进行实例化了

5、当一个普通的类,继承这个抽象类之后,那么这个普通类,必须重写这个抽象类当中所有的抽象方法。

6、抽象类存在的最大的意义就是为了被继承

7、抽象类也可以发生向上转型进一步发生多态

8、当一个抽象类A继承了抽象类B,此时抽象类A可以不重写抽象类B当中的方法

9、当一个普通的类C继承了第8条的A,此时就得重写所有的抽象方法

10、 final不能修饰抽象方法和抽象类

11、抽象方法也不能是private的

12、抽象类当中不一-定有抽象方法,但是如果这个方法是抽象方法,呢么这个类- -定是抽象类

代码示例:

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

abstract class Shape{

    /* public String name;

     public int age;

     public void func(){

         System.out.println("ada");

     }

     public static void func2(){

         System.out.println("akjdhkjasd");

     }*/

     public abstract void draw(); //抽象方法

}

abstract class A extends Shape{  //当一个抽象类A继承抽象类Shape时,可以不重写抽象类Shape中的方法

     public abstract void func();

}

class B extends A{  //出来混迟早得还,父类没有重写  你全写

     @Override

     public void draw() {

     }

     @Override

     public void func() {

     }

}

class Circle extends Shape {  //普通类继承了抽象类(内有抽象方法),这里报错,  必须重写Shap类当中的抽象个方法

     @Override

     public void draw() {

         System.out.println( "○" );

     }

}

class Rect extends Shape {

     @Override

     public void draw() {

         System.out.println( "♦" );

     }

}

class Triangle extends Shape {

     @Override

     public void draw() {

         System.out.println( "▲" );

     }

}

public class Testdemo {

     public static void drawMap(Shape shape){

         shape.draw();

     }

     public static void main(String[] args) {

         //Shape shape = new Shape();    //1、抽象类不能进行实例化

         Circle circle = new Circle();

         Rect rect = new Rect();

         Triangle triangle = new Triangle();

         drawMap(circle);

         drawMap(rect);

         drawMap(triangle);

     }

     //输出结果:

     //就是按顺序输出三种图形

}

2、接口

2.1、什么是接口

接口就是公共的行为规范标准,大家在实现时,只要符合规范标准,就可以通用。在Java中,接口可以看成是:多个类的公共规范,是一种引用数据类型。

2.2、接口的用法

接口的定义格式与定义类的格式基本相同,将class关键字换成 interface 关键字,就定义了一个接口。

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

interface IShape{

    /* //成员变量   默认静态常量  必须初始化 (写不写都一样)

     public static final int a = 10;

 

     //成员方法

     //public abstract void func();//接口里面的方法是默认抽象方法

     default void func(){

         System.out.println("默认的方法!");//如果接口当中的方法 需要一个具体的实现  必须用default来修饰

     }

     public static void staticFunc(){

         System.out.println("静态方法!");//接口当中的静态方法可以有具体的实现

     }*/

     void draw();

}

2.3、如何使用接口

接口不能直接使用,必须要有一个"实现类"来"实现"该接口,实现接口中的所有抽象方法。

?

1

2

3

4

5

6

class Circle implements IShape {   //一个普通的类可以通过implemengts来实现这个接口  另外:重写抽象方法

     @Override

     public void draw() {

         System.out.println( "○" );

     }

}

注意:

子类和父类之间是extends继承关系,类与接口之间是 implements 实现关系。

2.4、接口的特点(限制条件)

1、接口使用关键字interface来修饰

2、接口当中的成员方法,只能是抽象方法。所有的方法默认都是public abstract3、接口当中的成员变量,默认是public static final

4、接口当中的方法,如果要实现,需要用default来修饰

5、接口当中的静态的方法,可以有具体的实现

6、接口不能进行实例化。new接口

7、-一个普通的类可以通过implements来实现这个接口

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

interface IShape{

    /* //成员变量   默认静态常量  必须初始化 (写不写都一样)

     public static final int a = 10;

     //成员方法

     //public abstract void func();//接口里面的方法是默认抽象方法

     default void func(){

         System.out.println("默认的方法!");//如果接口当中的方法 需要一个具体的实现  必须用default来修饰

     }

     public static void staticFunc(){

         System.out.println("静态方法!");//接口当中的静态方法可以有具体的实现

     }*/

     void draw();

}

class Circle implements IShape {   //一个普通的类可以通过implemengts来实现这个接口  另外:重写抽象方法

     @Override

     public void draw() {

         System.out.println( "○" );

     }

}

class Rect implements IShape {

     @Override

     public void draw() {

         System.out.println( "♦" );

     }

}

class Triangle implements IShape {

     @Override

     public void draw() {

         System.out.println( "▲" );

     }

}

   /*  @Override

     public void func() {

         System.out.println("adiashgd");

     }*/

public class Test {

         public static void drawMap(IShape shape){

             shape.draw();

         }

         public static void main(String[] args) {

             //Shape shape = new Shape();    //1、抽象类不能进行实例化

             Circle circle = new Circle();

             Rect rect = new Rect();

             Triangle triangle = new Triangle();

             drawMap(circle);

             drawMap(rect);

             drawMap(triangle);

         }

         //IShape iShape = new IShape();  //接口不能实例化

         /*IShape iShape = new A();

         iShape.draw();

         iShape.func();

         IShape.staticFunc();*/

}

2.5、如何实现多个接口

代码示例:

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

class Animal {

     public String name;

     public Animal(String name) {

         this .name = name;

     }

     public void eat(){

         System.out.println( "吃饭饭!" );

     }

     // 飞  跑  游泳    布恩那个把这些操作写在这里

}

interface IFlying{

     void fly();

}

interface IRunning{

     void run();

}

interface ISwimming{

     void swim();

}

class Cat extends Animal implements IRunning{

     public Cat(String name) {

         super (name);

     }

     @Override

     public void run() {

         System.out.println( this .name+ "is cat 正在跑!" );

     }

}

//狗  继承了Animal实现了 跑和游泳接口    先继承后实现   需要重写两接口的方法

class Dog extends Animal implements IRunning,ISwimming{

     public Dog(String name){

         super (name);

     }

     @Override

     public void run() {

         System.out.println( this .name+ "is dog 正在跑!" );

     }

     @Override

     public void swim() {

         System.out.println( this .name+ "正在游泳!" );

     }

}

class Robot implements IRunning{

     @Override

     public void run() {

         System.out.println( "我是机器人,我在跑!" );

     }

}

class Duck extends Animal implements IRunning,ISwimming,IFlying{

     public Duck(String name) {

         super (name);

     }

     @Override

     public void fly() {

         System.out.println( this .name+ "鸭子正在飞!" );

     }

     @Override

     public void run() {

         System.out.println( this .name+ "鸭子正在跑!" );

     }

     @Override

     public void swim() {

         System.out.println( this .name+ "鸭子正在游泳!" );

     }

}

public class TestDemo1 {

     public static void walk(IRunning iRunning){

         iRunning.run();

     }

     public static void fly(IFlying iFlying){

         iFlying.fly();

     }

     public static void swim(ISwimming iSwimming){

         iSwimming.swim();

     }

     public static void main(String[] args) {

         //swim(new Dog("小狗"));

         Duck duck = new Duck( "小鸭子" );

         swim(duck);

         walk(duck);

         fly(duck);

     }

     public static void main2(String[] args) {

         walk( new Cat( "mimi" ));

         walk( new Dog( "mimi2" ));

         walk( new Robot());

     }

     public static void main1(String[] args) {

         Animal animal = new Cat( "mimi" );

         Animal animal2 = new Dog( "mimi2" );

         IRunning iRunning = new Cat( "mimi" );

         IRunning iRunning2 = new Dog( "mimi2" );

         ISwimming iSwimming2 = new Dog( "mimi2" );

     }

}

2.6、接口于接口之间的继承关系

类和类之间是单继承的,一个类可以实现多个接口,接口与接口之间可以多继承。即:用接口可以达到多继承的目的。接口可以继承一个接口, 达到复用的效果. 使用 extends 关键字.

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

interface C extends A,B{

     void funcC();

}

class AA implements C{    //类和接口之间是implements  那么接口与接口之间呢----extends[扩展]

     @Override

     public void funcC() {

         System.out.println( "funcC" );

     }

     @Override

     public void funcA() {

     }

     @Override

     public void funcB() {

     }

}

2.7、如何使用接口

在 sort 方法中会自动调用 compareTo 方法. compareTo 的参数是 Object , 其实传入的就是 Student 类型的对象.然后比较当前对象和参数对象的大小关系(按分数来算).

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

import java.util.Arrays;

class Student implements Comparable<Student>{

     public String name;

     public int age;

     public double score;

     public Student(String name, int age, double score) {

         this .name = name;

         this .age = age;

         this .score = score;

     }

     @Override

     public String toString() {

         return "Student{" +

                 "name='" + name + '\ '' +

                 ", age=" + age +

                 ", score=" + score +

                 '}' ;

     }

     @Override

     public int compareTo(Student o) {

       /*  if (this.age > o.age){

             return 1;

         }else if (this.age == o.age){

             return 0;

         }else {

             return -1;

         }*/

         //return this.name测试数据pareTo(o.name);//姓名比较

         //return this.age-o.age;//年龄比较

         return ( int ) ( this .score-o.score); //分数比较

     }

}

public class Test {

     public static void main(String[] args) {

         Student student1 = new Student( "zhangsan" , 98 , 78.9 );

         Student student2 = new Student( "lisi" , 38 , 18.9 );

         System.out.println(student1测试数据pareTo(student2));

     }

     public static void main1(String[] args) {

         Student[] students = new Student[ 3 ];

         students[ 0 ] = new Student( "zhangsan" , 98 , 78.9 );

         students[ 1 ] = new Student( "lisi" , 38 , 18.9 );

         students[ 2 ] = new Student( "abc" , 78 , 88.9 );

         Arrays.sort(students);

         System.out.println(Arrays.toString(students));

     }

}

到此这篇关于Java基础巩固抽象类与接口详解的文章就介绍到这了,更多相关Java抽象类与接口内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!

原文链接:https://blog.csdn.net/ks_xfx/article/details/124565216

查看更多关于Java基础巩固抽象类与接口详解的详细内容...

  阅读:11次