Chinaunix首页 | 论坛 | 博客
  • 博客访问: 7723633
  • 博文数量: 961
  • 博客积分: 15795
  • 博客等级: 上将
  • 技术积分: 16612
  • 用 户 组: 普通用户
  • 注册时间: 2010-08-07 14:23
文章分类

全部博文(961)

文章存档

2016年(1)

2015年(61)

2014年(41)

2013年(51)

2012年(235)

2011年(391)

2010年(181)

分类: Java

2012-04-15 22:29:15


点击(此处)折叠或打开

  1. class Person {
  2.   private String name;
  3.   private String location;

  4.   Person(String name) {
  5.     this.name = name;
  6.     location = "beijing";
  7.   }
  8.   
  9.   Person(String name,String location) {
  10.     this.name = name;
  11.     this.location = location;
  12.   }
  13.   
  14.   public String info() {
  15.     return
  16.       "name: "+name+
  17.       " location: "+location;
  18.   }
  19. }

  20. class Teacher extends Person {
  21.     private String capital;
  22.     
  23.     Teacher(String name, String capital) {
  24.         this(name, "beijing", capital);
  25.     }
  26.     
  27.     Teacher(String n, String l, String capital) {
  28.         super(n, l);
  29.         this.capital = capital;
  30.     }
  31.     
  32.     public String info() {
  33.         return super.info() + "capital " + capital;
  34.     }
  35. }

  36. class Student extends Person {
  37.     private String school;
  38.     Student(String name, String school) {
  39.         this(name,"beijing", school);
  40.     }
  41.     Student(String n,String l,String school) {
  42.         super(n,l);
  43.         this.school = school;
  44.     }
  45.     public String info() {
  46.         return super.info()+
  47.      " school: "+school;
  48.     }
  49. }

  50. public class TestTeacher {
  51.     public static void main(String[] args) {
  52.         Person p1 = new Person("A");
  53.         Person p2 = new Person("B","shanghai");
  54.         Student s1 = new Student("C","S1");
  55.         Student s2 = new Student("C","shanhai","S2");
  56.         
  57.         System.out.println(p1.info());
  58.         System.out.println(p2.info());
  59.         System.out.println(s1.info());
  60.         System.out.println(s2.info());
  61.         
  62.         Teacher t1 = new Teacher("D", "Professor");
  63.         System.out.println(t1.info());
  64.     }
  65. }

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