学习随笔——Java类和对象小例子

一、打印日历(预定义类)

import java.time.DayOfWeek;
import java.time.LocalDate;

public class CalendarTest {
    public static void main(String[] args) {
        LocalDate date = LocalDate.now();
        int month = date.getMonthValue();
        int today = date.getDayOfMonth();

        date = date.minusDays(today-1);     //将date设置为这一天的第一个月
        DayOfWeek weekday = date.getDayOfWeek();
        int value = weekday.getValue();     //1 = Monday,...7 = Sunday

        System.out.println("Mon Tue Wed Thu Fri Sat Sun");
        for(int i=1;i<value;i++)
            System.out.print("    ");   //空格数=3+1
        while(date.getMonthValue() == month){
            System.out.printf("%3d",date.getDayOfMonth());
            if(date.getDayOfMonth() == today)
                System.out.print("*");
            else
                System.out.print(" ");
            date = date.plusDays(1);
            if(date.getDayOfWeek().getValue() == 1)
                System.out.println();
        }
        if(date.getDayOfWeek().getValue() !=1)
            System.out.println();
    }

}

二、打印员工信息(用户自定义类)

import java.time.LocalDate;

public class EmployeeTest {
    public static void main(String[] args) {
        //在staff数组中填充三个Employee对象
        Employee[] staff = new Employee[3];

        staff[0] = new Employee("张三", 75000, 2015, 12, 15);
        staff[1] = new Employee("李四", 50000, 2016, 10, 1);
        staff[2] = new Employee("王五", 40000, 2017, 3, 15);

        //把每个人的薪水提高5%
        for(Employee e:staff)
            e.raiseSalary(5);

        //打印关于所有员工对象的信息
        for(Employee e:staff)
            System.out.println("姓名="+e.getName()+",薪水="+e.getSalary()+",受聘日期="+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){
        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 ralse = salary * byPercent / 100;
        salary += ralse;
    }
}

三、打印员工信息(静态方法)

public class StaticTest {
    public static void main(String[] args) {
        //在staff数组中填充三个Employee对象
        Employee[] staff = new Employee[3];

        staff[0] = new Employee("张三", 40000);
        staff[1] = new Employee("李四", 60000);
        staff[2] = new Employee("王五", 65000);

        //打印关于所有员工对象的信息
        for(Employee e : staff){
            e.setId();
            System.out.println("姓名="+e.getName()+",id="+e.getId()+",薪水="+e.getSalary());
        }

        int n = Employee.getNextId();   //调用静态方法
        System.out.println("下一个可用的id="+n);
    }

}

class Employee{
    private static int nextId = 1;

    private String name;
    private double salary;
    private int id;

    public Employee(String n,double s){
        name = n;
        salary = s;
        id = 0;
    }

    public String getName(){
        return name;
    }

    public double getSalary(){
        return salary;
    }

    public int getId(){
        return id;
    }

    public void setId(){
        id = nextId;    //设置id下一个可用id
        nextId++;
    }

    public static int getNextId(){
        return nextId;
    }   
}

你可能感兴趣的:(JAVA)