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

2012年(1008)

我的朋友

分类:

2012-08-01 11:00:47

原文地址:Java 构造过程 作者:luozhiyong131


点击(此处)折叠或打开

  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. }

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