Person.java:
public class Person {
private String name;
private String sex;
private int age;
public Person(){};
public Person(String name,String sex,int age)
{
this.name=name;
this.sex=sex;
this.age=age;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
}
Student.java:
import java.util.*;
public class Student extends Person {
private long number;
private int phi;
private int eng;
private int comp;
private int[] num;
public Student(){};
public Student(String name,String sex,int age,long number,int phi,int eng,int comp)
{
super(name,sex,age);
this.number=number;
this.phi=phi;
this.eng=eng;
this.comp=comp;
}
public int getComp() {
return comp;
}
public void setComp(int comp) {
this.comp = comp;
}
public int getEng() {
return eng;
}
public void setEng(int eng) {
this.eng = eng;
}
public long getNumber() {
return number;
}
public void setNumber(long number) {
this.number = number;
}
public int getPhi() {
return phi;
}
public void setPhi(int phi) {
this.phi = phi;
}
//求平均分数
public double avg()
{
return (getPhi()+getEng()+getComp())/3;
}
public int max()
{
sort();
return num[num.length-1];
}
public int min()
{
sort();
return num[0];
}
private void sort()
{
num=new int[]{getPhi(),getEng(),getComp()};
Arrays.sort(num);
}
public String toString()
{
return "学号:"+getNumber()+" 名字:"+getName()+" 性别:"+getSex()+" 平均分:"+avg()+" 最高分:"+max()+" 最低分:"+min();
}
}
测试类:
Test.java:
public class Test {
public static void main(String[] args) {
Student s=new Student("张三","男",20,11203,80,70,90);
System.out.println(s);
}
}
“天天”的代码正确啦,但是貌似把楼主的意思理解错了,题目的意思应该是Student类中的toString()方法用来输出该Student的信息,这是本人的理解,如果楼主是这个意思的话,应该将“天天”的构造函数部分的代码放到toString()函数中,也就是
public void toString() {
System.out.println("子类的toString()");
System.out.println("学号:" + this.getNumber());
System.out.println("姓名:" + super.name);
System.out.println("性别:" + super.getSex());
System.out.println("平均分:" + this.aver());
System.out.println("最高分:" + this.max());
System.out.println("最低分信息:" + this.min());
}
若是只要返回信息不用输出的话,就是这样了:
public String toString() {
String info=“”;
info="学号:" + this.getNumber()+"\n"+"姓名:" + super.name+"\n"+"性别:" +super.getSex()+"\n"+"平均分:" + this.aver())+"\n"+"最高分:" + this.max()+"\n"+"最低分信息:" + this.min();
return info;
}
public class Person{
public String name;
public char sex;
public int age;
}
public class Student extends Person{
public long number;
public int phi;
public int eng;
public int comp;
public double aver(){
return (phi+eng+comp)/3;
}
public int max(){
return phi >eng ? (phi>comp?phi:comp):(eng>comp?eng:comp);
}
public int min(){
return phi
public String toString(){
return "学号:"+number+" 姓名:"+name+" 性别:"+sex+"平均分:"+aver()+" 最高分:"+max()+" 最低分信息"+min();
}
}
public class Person {
protected String name = "wxk";
protected char sex = '男';
protected int age = 20;
public Person() {
System.out.println("父类的构造器");
}
public String toString(){
System.out.println("父类的toString()");
return "";
}
/**
* @return the name
*/
public String getName() {
return name;
}
/**
* @param name
* the name to set
*/
public void setName(String name) {
this.name = name;
}
/**
* @return the sex
*/
public char getSex() {
return sex;
}
/**
* @param sex
* the sex to set
*/
public void setSex(char sex) {
this.sex = sex;
}
/**
* @return the age
*/
public int getAge() {
return age;
}
/**
* @param age
* the age to set
*/
public void setAge(int age) {
this.age = age;
}
}