继承的小DEMO

代码如下 Student.java package four.com; public class Student extends Person { // private String name; // private int age; public Student(){} public Student(String name,int age){ this.setName(name); this.setAge(age); } // public String getName() { // return name; // } // public void setName(String name) { // this.name = name; // } // public int getAge() { // return age; // } // public void setAge(int age) { // this.age = age; // } // public void eat(){ // System.out.println("吃饭!!!!"); // } // public void walk(){ // System.out.println("走路!!!!!"); // } // public void info(){ eat(); System.out.println("我是好人"); } } Worker.java package four.com; public class Worker extends Person { // private String name; // private int age; // // public String getName() { // return name; // } // public void setName(String name) { // this.name = name; // } // public int getAge() { // return age; // } // public void setAge(int age) { // this.age = age; // } // public void eat(){ // System.out.println("吃饭!!!!"); // } // public void walk(){ // System.out.println("走路!!!!!"); // } } TestStudent.java package four.com; public class TestStudent { public static void main(String[] args) { Student s = new Student(); s.eat(); s.info(); Worker ww = new Worker(); ww.eat(); Person p = new Person(); p.eat(); } } Person.java package four.com; public class Person { private String name; private int age; public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public void eat(){ System.out.println("吃饭!!!!"); } public void walk(){ System.out.println("走路!!!!!"); } }
posted @ 2016-08-03 13:59  caocx  阅读(174)  评论(0编辑  收藏  举报