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

继承中的构造方法

时间:2016-05-03 14:44:13      阅读:141      评论:0      收藏:0      [点我收藏+]

标签:extends super


package lc1;

public 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;
 }
}

public class Student extends Person{
 private String school;
 Student(String name,String school){
  this(name,school,"beijing");
 }
 Student(String n,String l,String school){
  super(n,l);
  this.school=school;
 }
 public String info(){
  return super.info()+"school:"+school;
 }
}

public class Test {

 public static void main(String[] args) {
  // TODO 自动生成的方法存根
  Person p1=new Person("A");
  Person p2=new Person("B","shanghai");
  Student s1=new Student("C","s1");
  Student s2=new Student("C","shanghai","s2");
  System.out.println(p1.info());
  System.out.println(p2.info());
  System.out.println(s1.info());
  System.out.println(s2.info());
 }

}


本文出自 “技术的秘密” 博客,请务必保留此出处http://liucongcong.blog.51cto.com/11527480/1769659

继承中的构造方法

标签:extends super

原文地址:http://liucongcong.blog.51cto.com/11527480/1769659

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