項目三(2)——person與學生
阿新 • • 發佈:2017-10-11
學生類 turn static main 性別 str class string student
編程實現Person類,學生類的設計及其繼承關系
(同第六章課後第三題)
class Person{ private String name; private String addr; private String sex; private int age; public Person(){ System.out.println("聲明一個人類"); } public Person(String name,String addr){ this(); this.name=name; this.addr=addr; } public Person(String name,String addr,String sex,int age){ this(name,addr); this.sex=sex; this.age=age; } public String getName(){ return this.name; } public String getAddr(){ return this.addr; } public String getSex(){ return this.sex; } public int getAge(){ return this.age; } public void print(){ System.out.println("人類信息----->姓名:"+this.getName()+"; 地址:"+this.getAddr()+"; 性別:"+this.getSex()+"; 年齡:"+this.getAge()); } } class Student extends Person{ private float math; private float english; public Student(){ super(); System.out.println("此人是個學生"); } public Student(String name,String addr){ super(name,addr); } public Student(String name,String addr,String sex,int age,float math,float english){ super(name,addr,sex,age); this.math=math; this.english=english; } public void print(){ System.out.println("學生信息----->姓名:"+this.getName()+"; 地址:"+this.getAddr()+"; 性別:"+this.getSex()+"; 年齡:"+this.getAge()+"; 數學成績:"+this.math+"; 英語成績:"+this.english); } } public class Person01{ public static void main(String args[]){ Person per1=null; Person per2=null; per1=new Person("張三","河北保定","男",30); per2=new Student("張小三","河北保定","男",15,88.0f,77.0f); per1.print(); per2.print(); } }
項目三(2)——person與學生