Java 怎样在主函数中创建经理和普通员工的对象,赋值姓名等信息,并调用work函数。初学者,大神帮帮忙!
答案:2 悬赏:80 手机版
解决时间 2021-02-08 08:10
- 提问者网友:我是我
- 2021-02-07 11:42
public class AbstractTest {
public static void main(String[]args){
}
abstract class Employee//员工基类
{
private String name;
private String num;
private float salary;
Employee(String name,String num,float salary){
this.name=name;
this.num=num;
this.salary=salary;
}
public abstract void work();
}
class Manager extends Employee//经理类
{
private float bonus;
Manager(String name,String num,float salary,float bonus){
super(name,num,salary);
this.bonus=bonus;
}
public void work(){
System.out.println("Manager's work");
}
}
class GenStaff extends Employee//普通员工类
{
GenStaff(String name,String num,float salary){
super(name,num,salary);
}
public void work(){
System.out.println("General staff's work");
}
}
}
最佳答案
- 五星知识达人网友:詩光轨車
- 2021-02-07 13:08
public static void main(String[] args)
{
Manager mr=new Manager("张三","10001",135.1,100);
mr.work();
GenStaff gs=new GenStaff("张三","10001",135.1,100);
gs.work();
}
{
Manager mr=new Manager("张三","10001",135.1,100);
mr.work();
GenStaff gs=new GenStaff("张三","10001",135.1,100);
gs.work();
}
全部回答
- 1楼网友:北方的南先生
- 2021-02-07 14:10
完整代码如下,验证可执行
class AbstractTest {
public static void main(String[]args){
AbstractTest aa=new AbstractTest();
Manager mr=aa.new Manager("张三","10001",135.1f,100);
mr.work();
GenStaff gs=aa.new GenStaff("张三","10001",135.1f);
gs.work();
}
abstract class Employee//员工基类
{
private String name;
private String num;
private float salary;
Employee(String name,String num,float salary){
this.name=name;
this.num=num;
this.salary=salary;
}
public abstract void work();
}
class Manager extends Employee//经理类
{
private float bonus;
Manager(String name,String num,float salary,float bonus){
super(name,num,salary);
this.bonus=bonus;
}
public void work(){
System.out.println("Manager's work");
}
}
class GenStaff extends Employee//普通员工类
{
GenStaff(String name,String num,float salary){
super(name,num,salary);
}
public void work(){
System.out.println("General staff's work");
}
}
}
另外:建议父类的Employee的字段不要设为private,设为protected比较合适
我要举报
如以上问答信息为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
推荐资讯