编程语言
首页 > 编程语言> > JAVA 根据类构建数组(用类处理数组信息) 初学者

JAVA 根据类构建数组(用类处理数组信息) 初学者

作者:互联网

package com.wana;

import java.time.LocalDate;

/**
 * @author JackZhao
 * @create 2020-03-24 下午 12:47
 */
public class EmployeeTest {
    public static void main(String[] args) {

        Employee[] staff = new Employee[3];
        //构建employee数组
        staff[0] = new Employee("Carl Cracker", 75000, 1987, 12, 15);
        staff[1] = new Employee("Harry Hacker", 50000, 1989, 10, 1);
        staff[2] = new Employee("Tony Tester", 40000, 1990, 3, 15);
        //为数组填入对象

        for (Employee e : staff)
            e.raiseSalary(5);
        //像极了py的for语句实在好用,处理Employee数组staff。
        for (Employee e : staff)
            System.out.println("name=" + e.getName() + ",salary=" + e.getSalary() + ",hireDay="
                    + e.getHireDay());
}
}

class Employee
{
    private String name;
    private double salary;
    private LocalDate hireDay;

    public Employee(String n, double s, int year, int month, int day)//处理Employee数组填入的对象信息
    {
        name = n;
        salary = s;
        hireDay = LocalDate.of(year, month, day);
    }

    public String getName()
    {
        return name;
    }//返回名字的方法

    public double getSalary()
    {
        return salary;
    }//返回薪水的方法

    public LocalDate getHireDay()
    {
        return hireDay;
    }//返回入职时间的方法

    public void raiseSalary(double byPercent)//增长薪水的方法
    {
        double raise = salary * byPercent / 100;
        salary += raise;
    }
}
/*原来可以这样用:1.根据一个类构建一个数组;
                 2.再用类中的方法处理填入数组对象的信息。*/

 

标签:salary,用类,JAVA,double,数组,Employee,public,staff
来源: https://www.cnblogs.com/MR---Zhao/p/12558573.html