|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
学校需要构建综合系统,使用者包含多种角色。
角色Role分两类:学生Student和雇员Employee;
雇员又分为教员Faculty和职员Staff。
每个角色都有姓名、年龄。
学生有学号、班级。
一个雇员有工号、入职日期。
教员有职称。职员有职位称号。
请以如下Main类为基础,构建各个角色类,将代码补充完整。
- public class Main {
- public static void main(String[] args) {
- Faculty fac = new Faculty("张三",32,"33006","2019","10","25","讲师");
- Student stu=new Student("李四",19,"20201103","202011");
- Staff sta = new Staff("王五",27,"32011","2015","06","17","教务员");
- fac.show();
- sta.show();
- stu.show();
- }
- }
复制代码 输入样例:无
输出样例:我是张三,年龄32岁。工号是33006,2019年10月25日入职。是一名教师,讲师职称。我是王五,年龄27岁。工号是32011,2015年6月17日入职。是一名教务员。我是李四,年龄19岁。学号是20201103,来自202011班。
我的代码:
- public class test7301 {
-
- public static void main(String[] args) {
- Faculty fac = new Faculty("张三",32,"33006","2019","10","25","讲师");
- //Student stu=new Student("李四",19,"20201103","202011");
- //Staff sta = new Staff("王五",27,"32011","2015","06","17","教务员");
- fac.show();
- //sta.show();
- // stu.show();
- }
- }
- class per{
- String name;//姓名
- int age;//年龄
- per(String name, int age) {
- this.name = name;
- this.age = age;
- }
-
-
- }
- class Student extends per{
- String sno;//学号
- String cla;//班级
- public Student(String sno, String cla) {
- super(name,age);//Cannot refer to an instance field name while explicitly invoking a constructor报错
- this.sno = sno;
- this.cla = cla;
- }
- void show() {
- System.out.print("我是"+name+",年龄"+"age"+"岁。学号是"+sno+",来自"+cla+"班。");
- }
- }
- class Employee extends per{
- public String no;//工号
- public String yy;//入职年
- public String mm;//月
- public String dd;//日
-
- public Employee(String no, String yy, String mm, String dd) {
- super(name,age);
- this.no = no;
- this.yy = yy;
- this.mm = mm;
- this.dd = dd;
- }
- void show() {
- //System.out.print("我是"+name+",年龄"+"age"+"岁。学号是"+no+",来自"+cla+"班。");
- }
-
-
- }
- class Staff extends Employee{
- String zwch;//职位称号
-
- public Staff(String zwch) {
- super(name,age,no,yy,mm,dd);
- this.zwch = zwch;
- }
- void show() {
- //System.out.print("我是"+name+",年龄"+"age"+"岁。学号是"+no+",来自"+cla+"班。");
- }
-
- }
- class Faculty extends Employee{
- String zc;//职称
-
- public Faculty(String name,int age,String no,String yy,String mm,String dd,String zc) {
- //super();
- this.zc = zc;
- }
- void show() {
- System.out.print("我是"+name+",年龄"+age+"岁。工号是"+no+","+yy+"年"+mm+"月"+dd+"日入职。是一名教师,"+zwch+"职称。");
- }
-
-
- }
复制代码 super(name,age)调用父类构造函数为什么一直报错
你传的构造函数里就没有name和age参数,下面那两个子类也是同样错误
|
|