java 用户自定义类 Employee类

package 第3章;
import java.time.*;
public class 第3章a
{
    public static void main(String[] args)
    {
        Employee[] men=new Employee[3];
        men[0]=new Employee("hello",20000,2001,1,2);
        men[1]=new Employee("well",30000,2002,2,3);
        men[2]=new Employee("good",10000,2003,3,4);
        for(Employee a: men)
        {
            a.ChangeSalary(5);
        }
        for(Employee b:men)
        {
            System.out.println("name= "+b.getName()+",salary= "+b.getSalary()+",hireTime= "+b.getHireTime());
        }
    }          
}
class Employee
{
    private String name;
    private double salary;
    private LocalDate hireTime;
    public Employee(String n,double s ,int year,int month,int day)
    {
        name=n;
        salary=s;
        hireTime=LocalDate.of(year, month, day);
    }
    public String getName()
    {
        return name;
    }
    public double getSalary()
    {
        return salary;
    }
    public LocalDate getHireTime()
    {
        return hireTime;
    }
    public void ChangeSalary(double number)
    {
        double Changing=salary*number/100;
        salary+=Changing;
    }
}
run:
name= hello,salary= 21000.0,hireTime= 2001-01-02
name= well,salary= 31500.0,hireTime= 2002-02-03
name= good,salary= 10500.0,hireTime= 2003-03-04
成功构建 (总时间: 0 秒)

Again

package 第3章;
import java.time.*;

public class 第3章a
{
    public static void main(String[] args)
    {
       Employee[] man=new Employee[2];
       man[0]=new Employee("hello",200000,2003,2,8);
       man[1]=new Employee("well",300000,2005,1,29);
       for(Employee a:man)
            System.out.println("name is "+a.getName()+", salary is "+a.getSalary()+", hireTime is "+a.getHireTime());
       for(Employee b:man)
       {
           b.increaseSalary(5);
       }
       for(Employee c:man)
            System.out.println(c.getSalary());
    }
}

class Employee
{
    private double salary;
    private String name;
    private LocalDate hireTime;
    public Employee(String n,double s,int year,int month,int day)
    {
        name=n;
        salary=s;
        hireTime=LocalDate.of(year, month, day);

    }
    public String getName()
    {
        return name;
    }
    public double getSalary()
    {
        return salary;
    }

    public LocalDate getHireTime()
    {
        return hireTime;
    }
    public void increaseSalary(double number)
    {
        double increase=salary*number/100;
        salary+=increase;
    }
}
run:
name is hello, salary is 200000.0, hireTime is 2003-02-08
name is well, salary is 300000.0, hireTime is 2005-01-29
210000.0
315000.0
成功构建 (总时间: 0 秒)
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值