标签:private package public return
package ctgu.java.java; 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("走路"); } }
package ctgu.java.java; public class Student extends Person { // private String name; // private int age; private String schoolName; public Student(){ } public Student(String name, int age){ // this.name = name ; // this.age = 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("我是一个学生"); } } package ctgu.java.java; public class Worker extends Person{ public static void main(String[] args) { Student s = new Student(); s.eat(); Worker w = new Worker(); w.eat(); } }
package ctgu.java.java; public class Graduate extends Student { public void show(){ System.out.println("我是一个研究生"); } }
package ctgu.java.java; public class TestExtends { public static void main(String[] args) { Student s = new Student(); s.eat(); Worker w = new Worker(); w.eat(); Person p = new Person(); p.eat(); s.setAge(22); s.setName("李"); System.out.println(s.getName()+":"+ s.getAge()); Graduate g = new Graduate(); g.eat(); g.info(); g.show(); } }
标签:private package public return
原文地址:http://9815936.blog.51cto.com/9805936/1605435