码迷,mamicode.com
首页 > 其他好文 > 详细

面向对象5(方法的重载、重写、继承)

时间:2014-11-27 18:08:42      阅读:147      评论:0      收藏:0      [点我收藏+]

标签:io   ar   sp   on   bs   as   new   nbsp   方法   

class Person{

private String name;

private String location;

 Person(String name) {  

 this.name = name;   location = "BeiJing";

  }

//方法的重载

    Person (String name ,String location){

  this.name = name;   this.location = location;

  }

   public String info(){

  return "name: " + name + "  location: " + location;

  } 

 }

class Teacher extends Person {

 private String captial;

   Teacher(String name, String captial){

  this(name, "BeiJing", captial);

  }

    Teacher(String n, String l, String captial){

  super(n,l);

  this.captial = captial;

  }

//重写

    public String info(){

  return super.info() + " captial: " + captial;

  }

 }

class Student extends Person {

 private String school;

 Student (String name, String school) {

  this(name, "BeiJing", school);

  }

    Student (String n, String l, String school){

  super (n,l);

  this.school = school;

  }

    public String info(){

  return super.info() + "  school: " + school;

  }

 }

  public class TestStudentAndTeacher {

 public static void main (String []args){

  Person p1 = new Person("A");

  Person p2 = new Person("B","Shanghai");

  Student s1 = new Student("C","s1");

  Student s2 = new Student("C","Shanghai","s2");

  Teacher t1 = new Teacher("C","profession");

  System.out.println(p1.info());

  System.out.println(p2.info());

  System.out.println(s1.info());

  System.out.println(s2.info());  

 System.out.println(t1.info());

  }

 }

面向对象5(方法的重载、重写、继承)

标签:io   ar   sp   on   bs   as   new   nbsp   方法   

原文地址:http://www.cnblogs.com/dingxiaoblog/p/4126804.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!