Java基础笔记-抽象,继承,多态

抽象类: abstract修饰

抽象方法必须定义在抽象类中,抽象类不能创建对象.

在抽象方法中可以不定义抽象方法,作用是:让该类不能建立对象.

特点是:

1.定义在抽象类中

2.方法和类都用abstract修饰,在抽象类中的方法不写abstract也默认的是抽象方法.

3.不能用new来创建对象,调用抽象方法没意义.

4.抽象类中的方法被使用,必须由子类覆写其所有的抽象方法后,才能建立子类对象进行调用.

 如果子类只覆盖了部分的抽象方法.那么该子类还是一个抽象类.

5.抽象类不可以被实例化.

 

继承:(extends)

关键字:extends,一般用于类与类之间的所属关系.java中是单继承,多实现(implements).

让类与类之间产生关系,从而才有了多态的特性.

当两个类有了继承关系以后.就可以在子类中调用父类的方法和属性,一般使用 super 关键字.

其用法和this关键字类似.super调用的是父类的,而this是调用的方法本身的.

 

 1 abstract class Employee
 2 {
 3     private String name;
 4     private String id;
 5     private double pay;
 6     //构造函数
 7     Employee(String name,String id,double pay)
 8     {
 9         this.name = name;
10         this.id = id;
11         this.pay = pay;
12     }
13     public abstract void work(); //抽象类.
14     
15 }
16 
17 class NormalWorker extends Employee
18 {    
19     NormalWorker(String name,String id,double pay)
20     {
21         super(name,id,pay);
22     }
23     
24     public void work()
25     {
26         System.out.println("NormalWorker Work");
27     }
28     
29 }
30 
31 class Manager extends Employee
32 {
33     private double bonus;
34     
35     Manager(String name,String id,double pay,double bonus)
36     {
37         super(name,id,pay);
38         this.bonus = bonus;
39     }
40     public void work()
41     {
42         System.out.println("Manager Work");
43     }
44     
45 }
46 class AbstractTest
47 {
48     public static void main(String args[])
49     {
50         Manager M = new Manager("lisi","0910",10000,5000);
51         M.work();
52         
53     }
54 }
View Code

多态:
向上转型:将子类的对象转给父类的引用.

向下转向;将父类转换成子类类型.

一般使用的是向上转型,向下转型会出现不安全的问题,例如,猫是动物,但是我们不能说动物就是猫.

 1 /*
 2 多态:
 3 人:男人,女人
 4 动物:猫,狗
 5 猫 m =new 猫();
 6 动物 m = new 猫();
 7 特点:提高代码的拓展.
 8 */
 9 abstract class Animal
10 {
11     abstract void eat();
12 }
13 
14 class Cat extends Animal
15 {
16     public void eat()
17     {
18         System.out.println("吃鱼");
19     }
20     public void catchMouse()
21     {
22         System.out.println("抓老鼠");
23     }
24 }
25 
26 class Dog extends Animal
27 {
28     public void eat()
29     {
30         System.out.println("啃骨头");
31     }
32     public void kanJia()
33     {
34         System.out.println("看家");
35     }
36 }
37 
38 class Pig extends Animal
39 {
40     public void eat()
41     {
42         System.out.println("饲料");
43     }
44     public void swim()
45     {
46         System.out.println("游泳");
47     }
48 }
49 
50 
51 
52 class DuoTaiDemo
53 {
54     public static void  main(String args[])
55     {
56         /*
57         Cat c = new Cat();
58         c.eat();
59         Dog d = new Dog();
60         d.eat();
61         Pig p = new Pig();
62         p.eat();
63         */
64         /*
65         function(new Cat());
66         function(new Dog());
67         function(new Pig());
68         */
69         //Animal c = new Cat(); //向上转型 
70         function(new Cat());
71         function(new Dog());
72         function(new Pig());
73     }
74     /*
75     public static void function(Cat c)
76     {
77         c.eat();
78     }
79     public static void function(Dog d)
80     {
81         d.eat();
82     }
83     public static void function(Pig p)
84     {
85         p.eat();
86     }
87     */
88     public static void function(Animal a)
89     {
90         a.eat();
91     }
92 }
View Code
 1 abstract class Animal
 2 {
 3     abstract void eat();
 4 }
 5 
 6 class Cat extends Animal
 7 {
 8     public void eat()
 9     {
10         System.out.println("吃鱼");
11     }
12     public void catchMouse()
13     {
14         System.out.println("抓老鼠");
15     }
16 }
17 
18 class Dog extends Animal
19 {
20     public void eat()
21     {
22         System.out.println("啃骨头");
23     }
24     public void kanJia()
25     {
26         System.out.println("看家");
27     }
28 }
29 
30 class Pig extends Animal
31 {
32     public void eat()
33     {
34         System.out.println("饲料");
35     }
36     public void swim()
37     {
38         System.out.println("游泳");
39     }
40 }
41 
42 
43 
44 class DuoTaiDemo2
45 {
46     public static void  main(String args[])
47     {
48         /*
49         Animal a = new Cat(); //向上转型 
50         a.eat();
51         Cat c = (Cat)a; //向下转型,将父类的引用转成子类类型.
52         c.catchMouse();
53         */
54         function(new Cat());
55         function(new Dog());
56         function(new Pig());
57     }
58     
59     public static void function(Animal a)
60     {
61         a.eat();
62         if(a instanceof Cat)
63         {
64             Cat c = (Cat)a;
65             c.catchMouse();
66         }
67         else if(a instanceof Dog)
68         {
69             Dog d = (Dog)a;
70             d.kanJia();
71         }
72         else if(a instanceof Pig)
73         {
74             Pig p = (Pig)a;
75             p.swim();
76         }
77         
78     }
79 }
View Code
 1 /*
 2 练习: 
 3 基础班学生:学习,睡觉.
 4 高级班学生:学习,睡觉.
 5 */
 6 abstract class Student
 7 {
 8     public abstract void study();
 9     public void sleep()
10     {
11         System.out.println("躺着睡.");
12     }
13     
14 }
15 
16 class DoStudent
17 {
18     public void doSomething(Student stu)
19     {
20         stu.study();
21         stu.sleep();
22     }
23 }
24 
25 class BaseStudent extends Student
26 {
27     public void study()
28     {
29         System.out.println("base study");
30     }
31     public void sleep()
32     {
33         System.out.println("站着睡");
34     }
35 }
36 
37 class AdvStudent extends Student
38 {
39     public void study()
40     {
41         System.out.println("adv study");
42     }
43 }
44 
45 
46 
47 
48 
49 class DuoTaiTest
50 {
51     public static void main(String args[])
52     {
53         /*
54         BaseStudent bs = new BaseStudent();
55         bs.study();
56         bs.sleep();
57         AdvStudent as = new AdvStudent();
58         as.study();
59         as.sleep();
60         */
61         DoStudent ds = new DoStudent();
62         ds.doSomething(new BaseStudent());
63         ds.doSomething(new AdvStudent());
64     }
65 }
View Code

多态中成员的特点:
1.成员函数的特点; 在编译时,参阅引用型变量所属的类中是否有调用方法,有的话编译会通过,没有的话会编译失败

         在运行时,参阅对象所属的类中属否有调用方法.

面试中可能遇见的问题.问调用显示结果.

   成员函数(非静态的)在多态调用时,编译看左边,运行看右边.

   静态函数,无论编译还是运行,都看左边

 

看了一下之前写的那篇笔记,感觉都没什么能写的了,不知道写些什么,越往后学感觉都是得理解的,越学越难的说,最近又没什么状态,只好慢慢来,慢慢学习了.

posted @ 2015-05-19 17:28  曉峯  阅读(377)  评论(0编辑  收藏  举报