009抽象接口多态

内容:抽象、接口、多态

#########################################################################################################

1、抽象 Abstract

方法抽象了,那么类也得抽象
抽象类不能通过new对象
只有子类覆盖了所有的抽象方法后,子类才能通过new创建对象,如果没有覆盖所有的抽象方法,那么子类还是一个抽象类
抽象关键字abstract和final,private,static不能共存

 1 class ChouXiang 
 2 {
 3     public static void main(String[] args) 
 4     {
 5         Programmer p = new Programmer("jiang","ji357",7600);
 6         Manager m =new Manager("x","g009",12000,60000);
 7         p.work();
 8         m.work();
 9         p.show();
10         m.show();
11     }
12 }
13 
14 abstract class Employee
15 {
16     private String name;
17     private String id;
18     private double salary;
19 
20     Employee(String name,String id,double salary)
21     {
22         this.name = name;
23         this.id = id;
24         this.salary = salary;
25     }
26     abstract void work();
27     void show()
28     {
29         System.out.println("name:"+name+", id:"+id+", salary:"+salary);
30     }
31 }
32 
33 class Programmer extends Employee
34 {
35     Programmer(String name,String id,double salary)
36     {
37         super(name,id,salary);
38     }
39     void work()
40     {
41         System.out.println("code");
42     }
43 }
44 
45 class Manager extends Employee
46 {
47     private double bonus;
48     Manager(String name,String id,double salary,double bonus)
49     {
50         super(name,id,salary);
51         this.bonus = bonus;
52     }
53     void work()
54     {
55         System.out.println("manage");
56     }
57 }
eg1

###########################################################################################################

2、接口interface
接口的成员有两种:1、全局变量,2、抽象方法

interface Inter{
    public static final int PI = 3.141592653589793 ;
    public abstract void show1();
    public abstract void show2();
}

class De implements Inter{
    public void show1(){}
    public void show2(){}
 1 class  JieKouD
 2 {
 3     public static void main(String[] args) 
 4     {
 5         SubInter s = new SubInter();
 6         s.show1();
 7         s.show2();
 8     }
 9 }
10 
11 interface InterA
12 {
13     public abstract void show1();
14 }
15 
16 interface InterB
17 {
18     public abstract void show2();
19 }
20 
21 class SubInter implements InterA,InterB
22 {
23     public void show1()
24     {
25         System.out.println("show1");
26     }
27     public void show2()
28     {
29         System.out.println("show2");
30     }
31 }
View Code

############################################################################################################

3、多态
多态:也就是继承过后,类型的问题,比如有 Zi类 和 Fu类, Fu  f = new Zi();   这种那么 f 对象不能使用Zi类的特有方法,当通过强制转换过成Zi类型就可以使用子类的方法了。

类用户描述事物的共性基本功能
接口用于定义事物的额外功能

 

 

 

 



posted @ 2018-03-21 21:11  Alos403  阅读(239)  评论(0编辑  收藏  举报