Chinaunix首页 | 论坛 | 博客
  • 博客访问: 631684
  • 博文数量: 1008
  • 博客积分: 10
  • 博客等级: 民兵
  • 技术积分: 5175
  • 用 户 组: 普通用户
  • 注册时间: 2012-07-31 09:44
文章分类
文章存档

2012年(1008)

我的朋友

分类:

2012-08-01 11:00:57

原文地址:Java 方法重写 作者:luozhiyong131


点击(此处)折叠或打开

  1. class Person {
  2.     private String name;
  3.     private int age;
  4.     public void setName(String name){this.name=name;}
  5.     public void setAge(int age) {this.age=age;}
  6.     public String getName(){return name;}
  7.     public int getAge(){return age;}
  8.     public String getInfo() {
  9.           return "Name: "+ name + "\n" +"age: "+ age;
  10.   }
  11. }

  12. class Student extends Person {
  13.     private String school;
  14.     public String getSchool() {return school;}
  15.     public void setSchool(String school)
  16.     {this.school =school;}
  17.     public String getInfo() {
  18.       return "Name: "+ getName() + "\nage: "+ getAge()
  19.                     + "\nschool: "+ school;
  20.         }
  21. }

  22. public class TestOverWrite {
  23. public static void main(String arg[]){
  24.         Student student = new Student();
  25.         Person person = new Person();
  26.         person.setName("none");
  27.         person.setAge(1000);
  28.         student.setName("John");
  29.         student.setAge(18);
  30.         student.setSchool("SCH");
  31.         System.out.println(person.getInfo());
  32.         System.out.println(student.getInfo());
  33.     }
  34. }

阅读(99) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~