zoukankan      html  css  js  c++  java
  • 第十二节:类的定义

    标题图

    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();
    }
    
    }

    验结果及分析(运行结果截图)

    图片
    小礼物走一走 or 点赞

    图片

  • 相关阅读:
    linux进阶1
    linux中使用mysql数据库
    计算机是如何启动的
    宿主机mount虚拟机镜像文件
    SHA1算法原理
    KVM初始化过程
    关于内存对齐
    thread_info&内核栈
    gradlew compileDebug --stacktrace -info
    Android listview子控件的的点击事件(转)
  • 原文地址:https://www.cnblogs.com/dashucoding/p/11932587.html
Copyright © 2011-2022 走看看