Chinaunix首页 | 论坛 | 博客
  • 博客访问: 48476
  • 博文数量: 31
  • 博客积分: 228
  • 博客等级: 二等列兵
  • 技术积分: 240
  • 用 户 组: 普通用户
  • 注册时间: 2012-04-09 23:57
文章分类

全部博文(31)

文章存档

2012年(31)

我的朋友
最近访客

分类:

2012-04-17 00:22:32

原文地址: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. }

阅读(474) | 评论(0) | 转发(0) |
0

上一篇:Java equsls方法

下一篇:Java 抽象类

给主人留下些什么吧!~~