Java高级编程
掌握
类的定义;
对象的定义;
类的构造方法;
方法的重载
实现步骤:
新建Worker类
为Worker类添加四个属性
为Worker类添加带参的构造方法,不带参的构造方法不再自动创建
创建不带参数的构造方法
为Worker类添加getTax()方法
为Worker类添加getSalary()方法
在主方法中实例化对象测试并输出姓名,工资和所得税
package hh;
public class Worker {
private String ID;
private String name;
private double basic;
private double bonus;
public Worker() {
name="jack";
basic=1900;
bonus=100;
ID="07160615";
}
public Worker(String ID, String name, double basic, double bonus) {
super();
this.ID = ID;
this.name = name;
this.basic = basic;
this.bonus = bonus;
}
public String getID() {
return ID;
}
public void setID(String iD) {
ID = iD;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public double getBasic() {
return basic;
}
public void setBasic(double basic) {
this.basic = basic;
}
public double getBonus() {
return bonus;
}
public void setBonus(double bonus) {
this.bonus = bonus;
}
public double getTax(){
if (this.basic+this.bonus<=1600){
return 0;
}else{
return (this.basic+this.bonus-1600)*0.15;
}
}
public double getSalary(){
if (this.basic+this.bonus<=1600){
return this.basic+this.bonus;
}else{
return this.basic+this.bonus-this.getTax();
}
}
void print(){
System.out.println("工人姓名:"+this.name);
System.out.println("所得税:"+this.getTax());
System.out.println("实际工资:"+this.getSalary());
}
public static void main(String[] args) {
Worker jack=new Worker("07160615", "jack", 1400, 100);
jack.print();
}
}
验结果及分析(运行结果截图)