请用java编写两个类:Person类和Student类,实现继承关系。
要求:(Person类包括属性name,age,包括方法getInfo()用来输出所有属性;Student类包括属性school,包括方法getInfo()用来输出所有属...
要求:
(Person类包括属性name,age,包括方法getInfo()用来输出所有属性;Student类包括属性school,包括方法getInfo()用来输出所有属性。) 展开
(Person类包括属性name,age,包括方法getInfo()用来输出所有属性;Student类包括属性school,包括方法getInfo()用来输出所有属性。) 展开
3个回答
展开全部
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;
}
}
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;
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
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;
}
}
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;
}
}
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
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;
}
}
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;
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询