很急很急 拜托了 java编程 子类和基类的问题。
发布网友
发布时间:2023-08-07 07:23
我来回答
共2个回答
热心网友
时间:2023-09-12 01:52
package ;
/**
* Person 类的子类
* @author Administrator
*
*/
public class Employee extends Person{
//每一个employee都有office, salary, and date hired(办公室,工资和录用日期)
String office;//办公室
int salary; // 工资
String hired;//录用日期
public String toString(){
String className = this.getClass().getCanonicalName();
System.out.println(className);
return className;
}
}
package ;
/**
* Employee的子类
* @author Administrator
*
*/
public class Faculty extends Employee{
public String toString(){
String className = this.getClass().getCanonicalName();
System.out.println(className);
return className;
}
}
package ;
/**
* 定义一个名为MyDate的类,包含字段年,月,日。每个faculty成员都有office hours and a rank
* @author Administrator
*
*/
public class MyDate {
private String year;
private String month;
private String day;
public String date(){
return this.year + "年" + this.month +"月" +this.day + "日";
}
public String toString(){
String className = this.getClass().getCanonicalName();
System.out.println(className);
return className;
}
}
package ;
/**
*Person 父类 有两个子类 Student和Employee
* @author Administrator
*
*/
public class Person {
private String name;
private int address;
private int phoneNumber;
private int emailAddress;
public String toString(){
String className = this.getClass().getCanonicalName();
System.out.println(className);
return className;
}
public static void main(String[] args) {
Person p = new Person();
p.toString();
}
}
package ;
/**
* Employee的子类
* @author Administrator
*
*/
public class Staff extends Employee{
private String title;
public String toString(){
String className = this.getClass().getCanonicalName();
System.out.println(className);
return className;
}
}
package ;
/**
* Person 类的子类
* @author Administrator
*
*/
public class Student extends Person {
private final static int freshman = 1;//定义常量 年级
private final static int sophmore = 2;
private final static int junior = 3;
private final static int senior = 4;
public void status(int i){ //定义年级
switch (i) {
case 1: System.out.println(freshman+"");
break;
case 2: System.out.println(sophmore+"");
break;
case 3: System.out.println(junior+"");
break;
case 4: System.out.println(senior+"");
break;
default:
break;
}
}
public String toString(){
String className = this.getClass().getCanonicalName();
System.out.println(className);
return className;
}
}
package ;
public class test {
public static void main(String[] args) {
//写一个测试程序创建一个人,学生,员工,教师和工作人员,并调用其的toString()方法。
Student s = new Student(); //创建一个学生
s.toString();
Employee e = new Employee(); //创建一个员工
e.toString();
Faculty f = new Faculty(); //创建一个老师
f.toString();
Staff sf= new Staff();
sf.toString();
}
}
水平就这样了 看看能不能用 不能用就算了 好累!!!!!
热心网友
时间:2023-09-12 01:52
10分啊 呵呵追问想多给的,可是没有财富值了。
追答哦