码迷,mamicode.com
首页 > 编程语言 > 详细

JAVA学习--Abstract使用方法

时间:2015-01-30 22:25:37      阅读:290      评论:0      收藏:0      [点我收藏+]

标签:

* abstract:抽象的,可以用来修饰类、方法
 

*
 * 1.abstract修饰类:抽象类
 * 1)不可被实例化
 * 2)抽象类有构造器 (凡是类都有构造器)
 * 3)抽象方法所在的类,一定是抽象类。
 * 4)抽象类中可以没有抽象方法。
 *
 * 2.abstract修饰方法:抽象方法
 * 1)格式:没有方法体,包括{}.如:public abstract void eat();
 * 2)抽象方法只保留方法的功能,而具体的执行,交给继承抽象类的子类,由子类重写此抽象方法。
 * 3)若子类继承抽象类,并重写了所有的抽象方法,则此类是一个"实体类",即可以实例化
 * 4)若子类继承抽象类,没有重写所有的抽象方法,意味着此类中仍有抽象方法,则此类必须声明为抽象的!
 *
 
 
 1 public class TestAbstract {
 2     public static void main(String[] args) {
 3         //Person p1 = new Person();
 4         //p1.eat();
 5        
 6         Student s = new Student();
 7         s.eat();
 8        
 9         Person p = new Student();//多态
10         p.eat();
11        
12     }
13 }
14 
15 abstract class Creator{
16     abstract void breath();
17 }
18 
19 abstract class Person extends Creator{
20     String name;
21     public abstract void eat();
22     public abstract void walk();
23    
24     public String getName() {
25         return name;
26     }
27     public void setName(String name) {
28         this.name = name;
29     }
30     public Person(){
31        
32     }
33     public Person(String name){
34         this.name = name;
35     }
36 }
37 class Student extends Person{
38     public void eat(){
39         System.out.println("学生吃饭");
40     }
41     public void walk(){
42         System.out.println("学生走路");
43     }
44     @Override
45     void breath() {
46         System.out.println("学生不应该呼吸雾霾的空气");
47     }
48 }
49 
50 abstract class Worker extends Person{
51     public void eat(){
52         System.out.println("工人吃饭");
53     }
54 //    public void walk(){
55 //        System.out.println("工人走路");
56 //    }
57 }
58 
59 class Animal{
60     //不是抽象方法!
61     public void sleep(){
62        
63     }
64 }

 

JAVA学习--Abstract使用方法

标签:

原文地址:http://www.cnblogs.com/zhangfan94/p/4263288.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!