继承的小DEMO

不打扰是莪最后的温柔 提交于 2020-02-22 15:13:23
代码如下 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("走路!!!!!"); } }
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!