在Java編程中,我們可以通過定義小時工和計件工的代碼來實現薪資計算。下面分別來介紹這兩種代碼。
//小時工代碼 public class HourlyWorker extends Employee { private double wage; //每小時工資 private double hours; //每周工作小時數 public HourlyWorker(String name, double wage, double hours) { super(name); this.wage = wage; this.hours = hours; } @Override public double calculatePay() { return wage * hours; } } //計件工代碼 public class PieceWorker extends Employee { private double wagePerPiece; //每個件的工資 private int pieces; //每周完成件數 public PieceWorker(String name, double wagePerPiece, int pieces) { super(name); this.wagePerPiece = wagePerPiece; this.pieces = pieces; } @Override public double calculatePay() { return wagePerPiece * pieces; } }
以上是兩種基礎的Java代碼,分別用于實現小時工和計件工的薪資計算。我們可以通過繼承Employee類、重寫calculatePay()方法來實現這個功能。具體實現過程中,可以根據需求添加更多屬性和方法,讓代碼更加完善。