有关java继承的题目

2025-01-19 17:09:05
推荐回答(5个)
回答1:

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

public Person(){
}

public Person(String name,String sex,int age){
setName(name);
setSex(sex);
setAge(age);
}

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

public void setSex(String sex){
this.sex=sex;
}

public void setAge(int age){
this.age=age;
}
public String getName(){
return this.name;
}
public String getSex(){
return this.sex;
}
public int getAge(){
return this.age;
}
public String toString(){
return null;
}
}
//学生类
class Student extends Person
{
//状态
private String state;
public Student(String name,String sex,int age,String state){
super(name,sex,age);
setState(state);
}
public void setState(String state){
this.state=state;
}
public String getState(){
return this.state;
}
public String toString(){
return "姓名:"+super.getName()+"\n性别:"+super.getSex()+
"\n年龄:"+super.getAge()+"\n年级:"+this.getState();
}
}
class Employee extends Person
{
//办公室
private String bureaux;
//工资
private double salary;
//受聘日期
private String datetime;
public Employee(String name,String sex,int age,String bureaux,
double salary,String datetime){
super(name,sex,age);
setBureaux(bureaux);
setSalary(salary);
setDatetime(datetime);
}
public void setBureaux(String bureaux){
this.bureaux=bureaux;
}
public void setSalary(double salary){
this.salary=salary;
}
public void setDatetime(String datetime){
this.datetime=datetime;
}
public String getBureaux(){
return this.bureaux;
}
public double getSalary(){
return this.salary;
}
public String getDatetime(){
return this.datetime;
}
public String toString(){
return "姓名:"+super.getName()+"\n性别:"+super.getSex()+
"\n年龄:"+super.getAge()+"\n办公室:"+this.getBureaux()+
"\n薪水:"+this.getSalary()+"\n受聘日期:"+this.getDatetime();
}
}
public class Person01
{
public static void main(String agrs[]){
Student s=new Student("张三","男",18,"一年级");
System.out.println(s.toString());
System.out.println("------------------------------");
Employee emp=new Employee("李四","女",26,"3楼",8000,"2009-3-4");
System.out.println(emp.toString());
}
}

可以编译,已经测试过,创建个Person01.java文件,粘贴后便可运行
toString方法必须返回String,不可以是void。看你写的代码错误和规范很多,好好加油吧!

回答2:

所谓的相关信息是什么信息啊

回答3:

class Student extends Person {
public void toString(){
System.out.println("this is student!");
}
}
class Employee extends Person {
public void toString(){
System.out.println("this is employee!");
}
}

回答4:

class Studnet extends Person{
private String grade="";
public void setGrade(String grade){
this.grade=grade;
}
public String getGrade(){
return this.grade;
}
public void toString(){
System.out.println("学生的年级是:"+this.grade);
System.out.println(xxx)//XXX是person中的信息

}
Employee类相似,只是多了三个字段.

回答5:

class Studnet extends Person{
private String xinxi="";
public void setXinxi(String xinxi){
this.xinxi=xinxi;
}
public String getXinxi(){
return this.xinxi;
}
public void toString(){
System.out.println("学生的信息是:"+xinxi);

}
/******************************************/
class Employee extends Person{
private String xinxi="";
public void setXinxi(String xinxi){
this.xinxi=xinxi;
}
public String getXinxi(){
return this.xinxi;
}
public void toString(){
System.out.println("雇员的信息是:"+xinxi);

}