| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 470 人关注过本帖
标题:关于子父类的调用问题
只看楼主 加入收藏
菜鸟高手菜鸟
Rank: 1
等 级:新手上路
帖 子:14
专家分:8
注 册:2012-11-18
结帖率:75%
收藏
已结贴  问题点数:10 回复次数:2 
关于子父类的调用问题
设计一个人员类(Person),以及从Person类派生出助教类(Assistant)、讲师类(Instructor)、副教授类(AssistantProfesson)和教授类(Professor)。四种类型教师的工资收入计算公式如下:

工资收入=基本工资+授课时数×每课时报酬

其中,助教基本工资为800元,每课时报酬25元,讲师基本工资为1000元,每课时报酬35元,副教授基本工资为1200元,每课时报酬40元,教授基本工资为1400元,每课时报酬50元。

将人员类定义为抽象类,具体定义如下:
abstract class Person {
    String name;
    double salary; //工资收入
  int hour; //课时
  abstract double countSalary(int hour);//计算工资收入
  abstract public String toString();//返回员工的姓名、职称、收入字符串
  abstract public boolean equals(Person p);//与另一员工比较职称及工资收入是否一样
}
请为四种类型教师自行设计合理的类,但必须继承自Person类,另编写一个测试类测试你所编写的类,要求输入两个员工的姓名,职称和授课时数后分别显示他们的姓名、职称、收入,并比较两人职称及工资收入是否一样。
abstract class Person{    //String title;
    String name;
    double salary; //工资收入
    int hour; //课时
    abstract double countSalary(int hour);//计算工资收入
    abstract public String toString();//返回员工的姓名、职称、收入字符串
    abstract public boolean equals(Person p);//与另一员工比较职称及工资收入是否一样
}
class Assistant extends Person{String title;
     double countSalary(int hour){
             salary=800+hour*25;
            return salary;
        }
        public String toString(){
            return "姓名 "+name+" 职称 "+title+" 收入 "+salary;
        }
        public boolean equals(Person p){
            boolean flag=false;
            if(this.title.equals(p.title)&&p.salary==this.salary)
            flag=true;
            return flag;
        }
        }
class Instructor extends Person{
    String title;
        double countSalary(int hour){
            salary=1000+hour*35;
            return salary;
        }
        public String toString(){
            return "姓名 "+name+" 职称 "+title+" 收入 "+salary;
        }
        public boolean equals(Person p){
            boolean flag=false;
            if(this.title.equals(p.title)&&p.salary==this.salary)
            flag=true;
            return flag;
        }
    }
class AssistantProfesson extends Person{
    String title;
        double countSalary(int hour){
            salary=1200+hour*40;
            return salary;
        }
        public String toString(){
            return "姓名 "+name+" 职称 "+title+" 收入 "+salary;
        }
        public boolean equals(Person p){
            boolean flag=false;
            if(this.title.equals(p.title)&&p.salary==this.salary)
            flag=true;
            return flag;
        }
    }
class Professor extends Person{
    String title;
        double countSalary(int hour){
            salary=1400+hour*50;
            return salary;
        }
        public String toString(){
            return "姓名 "+name+" 职称 "+title+" 收入 "+salary;
        }
        public boolean equals(Person p){
            boolean flag=false;
            if(this.title.equals(p.title)&&p.salary==this.salary)
            flag=true;
            return flag;
        }
}
public class Test4{
    public static void main(String []args){
        //Scanner reader=new Scanner(System.in);
        Assistant p1=new Assistant();
        p1.name="李强";
        p1.title="Assistant";
        p1.countSalary(10);
        System.out.println(p1.toString());
        
        
        Instructor p2=new Instructor();
        p2.name="陈晨";
        p2.title="Instructor";
        p2.countSalary(5);
        System.out.println(p2.toString());Person p22=p2;
        String out=p1.equals(p22) ? "两人职称及工资收入一样":"两人职称及工资收入不一样";
        System.out.println(out);
        }
}

C:\Users\lenovo\Desktop\9\Test4.java:20: 找不到符号
符号: 变量 title
位置: 类 Person
                        if(this.title.equals(p.title)&&p.salary==this.salary)
                                              ^
C:\Users\lenovo\Desktop\9\Test4.java:36: 找不到符号
符号: 变量 title
位置: 类 Person
                        if(this.title.equals(p.title)&&p.salary==this.salary)
                                              ^
C:\Users\lenovo\Desktop\9\Test4.java:52: 找不到符号
符号: 变量 title
位置: 类 Person
                        if(this.title.equals(p.title)&&p.salary==this.salary)
                                              ^
C:\Users\lenovo\Desktop\9\Test4.java:68: 找不到符号
符号: 变量 title
位置: 类 Person
                        if(this.title.equals(p.title)&&p.salary==this.salary)
                                              ^
4 错误

处理已完成。

如何才能不用在Person类中定义String title;在子类的方法中调用
public boolean equals(Person p){
            boolean flag=false;
            if(this.title.equals(p.title)&&p.salary==this.salary)
            flag=true;
            return flag;
        }
搜索更多相关主题的帖子: abstract double 副教授 讲师 教师 
2014-04-09 10:15
qwe885167759
Rank: 4
等 级:业余侠客
威 望:5
帖 子:148
专家分:259
注 册:2013-3-12
收藏
得分:5 
没有定义p

我本楚狂人,凤歌笑孔丘
2014-04-13 21:35
xl881221
Rank: 3Rank: 3
等 级:论坛游侠
威 望:2
帖 子:30
专家分:177
注 册:2014-3-23
收藏
得分:5 
把子类里的title提取到父类里就行了,还有没必要Person p22=p2;

abstract class Person{   
    String title;
    String name;
    double salary; //工资收入
    int hour; //课时
    abstract double countSalary(int hour);//计算工资收入
    abstract public String toString();//返回员工的姓名、职称、收入字符串
    abstract public boolean equals(Person p);//与另一员工比较职称及工资收入是否一样
}
class Assistant extends Person{
     double countSalary(int hour){
             salary=800+hour*25;
            return salary;
        }
        public String toString(){
            return "姓名 "+name+" 职称 "+title+" 收入 "+salary;
        }
        public boolean equals(Person p){
            boolean flag=false;
            if(this.title.equals(p.title)&&p.salary==this.salary)
            flag=true;
            return flag;
        }
        }
class Instructor extends Person{
        double countSalary(int hour){
            salary=1000+hour*35;
            return salary;
        }
        public String toString(){
            return "姓名 "+name+" 职称 "+title+" 收入 "+salary;
        }
        public boolean equals(Person p){
            boolean flag=false;
            if(this.title.equals(p.title)&&p.salary==this.salary)
            flag=true;
            return flag;
        }
    }
class AssistantProfesson extends Person{
        double countSalary(int hour){
            salary=1200+hour*40;
            return salary;
        }
        public String toString(){
            return "姓名 "+name+" 职称 "+title+" 收入 "+salary;
        }
        public boolean equals(Person p){
            boolean flag=false;
            if(this.title.equals(p.title)&&p.salary==this.salary)
            flag=true;
            return flag;
        }
    }
class Professor extends Person{
        double countSalary(int hour){
            salary=1400+hour*50;
            return salary;
        }
        public String toString(){
            return "姓名 "+name+" 职称 "+title+" 收入 "+salary;
        }
        public boolean equals(Person p){
            boolean flag=false;
            if(this.title.equals(p.title)&&p.salary==this.salary)
            flag=true;
            return flag;
        }
}
public class Test4{
    public static void main(String []args){
        //Scanner reader=new Scanner(System.in);
        Assistant p1=new Assistant();
        p1.name="李强";
        p1.title="Assistant";
        p1.countSalary(10);
        System.out.println(p1.toString());
        
        
        Instructor p2=new Instructor();
        p2.name="陈晨";
        p2.title="Instructor";
        p2.countSalary(5);
        System.out.println(p2.toString());
        String out=p1.equals(p2) ? "两人职称及工资收入一样":"两人职称及工资收入不一样";
        System.out.println(out);
        }
}
2014-04-18 12:17
快速回复:关于子父类的调用问题
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.014499 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved