请用java编写两个类:Person类和Student类,实现继承关系。

要求:(Person类包括属性name,age,包括方法getInfo()用来输出所有属性;Student类包括属性school,包括方法getInfo()用来输出所有属... 要求:
(Person类包括属性name,age,包括方法getInfo()用来输出所有属性;Student类包括属性school,包括方法getInfo()用来输出所有属性。)
展开
 我来答
qiaole123
2010-06-05 · TA获得超过3413个赞
知道大有可为答主
回答量:1408
采纳率:66%
帮助的人:965万
展开全部
package test;

class Persons {
private String name;
private int age;

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public int getAge() {
return age;
}

public void setAge(int age) {
this.age = age;
}

public void getInfo(String name, int age) {
System.out.println(name + ": " + age);
}
}

class Student extends Person {

Student(String nema, String dizhi) {
super(nema, dizhi);
}

public void getInfo(String school) {
System.out.println(school);
}

private String school;

public String getSchool() {
return school;
}

public void setSchool(String school) {
this.school = school;
}

}
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
百度网友460b538
推荐于2016-08-13 · TA获得超过696个赞
知道小有建树答主
回答量:707
采纳率:100%
帮助的人:913万
展开全部
public class Person {
private String name;
private int age;

public void getInfo() {
System.out.println("Name: " + name);
System.out.println("Age: " + age);
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public int getAge() {
return age;
}

public void setAge(int age) {
this.age = age;
}

}

==================================

public class Student extends Person {
private String school;

public void getInfo() {
super.getInfo();
System.out.println("School: " + school);
}

public static void main(String[] args) {
Student s = new Student();
s.setName("aaa");
s.setAge(28);
s.setSchool("bbb");
s.getInfo();
}

public String getSchool() {
return school;
}

public void setSchool(String school) {
this.school = school;
}

}
本回答被提问者采纳
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
大时代暑
2010-06-05 · TA获得超过2.5万个赞
知道大有可为答主
回答量:4111
采纳率:0%
帮助的人:4821万
展开全部
import java.util.*;
public class TestCollection {

public static void main(String[] args) {
Person p1 = new Person("Tom", "male", 18);
Person p2 = new Person("Tom", "male", 19);
Student t1 = new Student("Lucy", "female", "一中", 19);
Student t2 = new Student("Lucy", "male", "一中", 29);
Student t3 = new Student("Lucy", "female", "二中", 19);
Collection c = new HashSet();
c.add(p1);
c.add(p2);
c.add(t1);
c.add(t2);
c.add(t3);
System.out.println(c);
}

}

class Person {
private String name;
private String sex;
private int age;

public Person(){

}
public Person(String name, String sex){
this.name = name;
this.sex = sex;
}
public Person(String name, String sex, int age){
this(name, sex);
this.age = age;
}
public void setName(String name){
this.name = name;
}
public String getName(){
return name;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String toString(){
return name + " " + sex + " " + age;
}
public boolean equals(Object obj){
if(obj != null && obj instanceof Person){
Person per = (Person)obj;
if((this.getName().equals(per.getName())&& (this.getSex().equals(per.getSex())))){
return true;
}
}
return false;
}
public int hashCode(){
return 1;
}
}

class Student extends Person {
private String school;

public Student(String name, String sex, String school, int age){
super(name, sex, age);
this.school = school;
}
public String toString(){
return this.getName() + " " + this.getSex()+ " " + this.getAge() + " " + school;
}
public boolean equals(Object obj){
if(obj != null && obj instanceof Teacher){
Student tea = (Student)obj;
if(this.getName().equals(tea.getName()) && this.school.equals(tea.school)){
return true;
}
}
return false;
}
}
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
收起 更多回答(1)
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式