简体   繁体   English

Java中的继承与多态

[英]Inheritance & Polymorphism in Java

I have programmed for exercising the Inheritance & Polymorphism use in Java, but coming across some problems, please see the codes below: 我已经编写了在Java中使用继承和多态性的程序,但遇到一些问题,请参阅下面的代码:

Superclass: 超类:

public class Worker {

private String name;
private double salaryRate;

public Worker(String nm, double rate){
nm=name;
rate=salaryRate;
}

public void computePay(int hours){

double pay=hours*salaryRate;
System.out.println("The Salary for "+getName()+" is "+pay);

}

public String toString(){
return name+" "+salaryRate;
}

public String getName() {
return name;
}

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

public double getSalaryRate() {
return salaryRate;
}

public void setSalaryRate(double salaryRate) {
this.salaryRate = salaryRate;
}
}

one subclass: 一个子类:

   public class HourlyWorker extends Worker {

   public HourlyWorker(String nm, double rate) {
    super(nm, rate);

    }

   public void computePay(int hours){

if (hours>40){
    double extraPay=(hours-40)*1.5*getSalaryRate();
    double pay=40*getSalaryRate();
    double total=extraPay+pay;
    System.out.println("The salary for "+getName()+" is "+total);
}
else{
    super.computePay(hours);

}
    }
    }

Another subclass: 另一个子类:

 public class SalariedWorker extends Worker {

 public SalariedWorker(String nm, double rate){
super(nm,rate);
   }

 public void computePay(int hours){
double pay=40*getSalaryRate();
System.out.println("The salary for "+getName()+" is "+pay);

   }
   }

Main() method: Main()方法:

 public class Test {

public static void main(String[] args) {
    Worker a=new HourlyWorker("Tom",2.0);
    Worker b=new HourlyWorker("Lee",2.0);
    Worker c=new SalariedWorker("Pei",2.0);
    Worker d=new SalariedWorker("Joe",2.0);

    System.out.println(a+" "+b+" "+c+" "+" "+d);

    a.computePay(50);
    b.computePay(30);
    c.computePay(20);
    d.computePay(60);


}

    }

It is a bit long, thank you for your patient to read:) However, when they compile, it shows: 这有点长,感谢您的患者阅读:)但是,当他们编译时,它显示:

 null 0.0 null 0.0 null 0.0  null 0.0
 The salary for null is 0.0
 The Salary for null is 0.0
 The salary for null is 0.0
 The salary for null is 0.0

Please advise where goes wrong, thank you guys! 请告知哪里出错了,谢谢你们!

You assignments are reversed in the constructor. 您的分配在构造函数中是相反的。 You are not setting the instance attributes values using the input params and hence those attributes always have the default values.Change this 您没有使用输入参数设置实例属性值,因此这些属性始终具有默认值。更改此值

public Worker(String nm, double rate){
nm=name;
rate=salaryRate;
}

to

public Worker(String nm, double rate){
   this.name=nm;
   this.salaryRate=rate;
}

Note: Usage of this helps you to avoid shadowing problems as well when the name of the input params and class attributes are same. 注意:当输入参数和类属性的名称相同时,使用this可以帮助您避免阴影问题。

public Worker(String nm, double rate){
nm=name;
rate=salaryRate;
}

you are assigning values to local variables not to the instance variables. 您将值分配给局部变量而不是实例变量。 Therefore those variables are having their default values. 因此,这些变量具有默认值。 for string default value is null for double it is 0.0 for string默认值为null为double,为0.0

you should do 你应该做

public Worker(String nm, double rate){
name = nm;
salaryRate = rate;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM