标签:
* abstract:抽象的,可以用来修饰类、方法
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 }
标签:
原文地址:http://www.cnblogs.com/zhangfan94/p/4263288.html