Java使用另一个类作为属性

该博客内容涉及一个简单的Java程序,实现了一个员工信息管理系统的部分功能,包括雇员类(Employee)、日期类(MyDate)的定义。程序中创建了雇员对象,设置了姓名、年薪和受雇日期,并进行了合法性检查。主要涉及Java面向对象编程、日期处理和输入验证。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

package 雇员;

public class test02 {
    public static void main(String[] args){
        //MyDate m1 = new MyDate(2001,2,32);
        MyDate m1 = new MyDate();
        m1.setYear(2001);
        m1.setMonth(2);
        m1.setDay(28);
        Employee e1 = new Employee("小刘",500000,m1);

        System.out.println("姓名:"+e1.getName());
        System.out.println("年薪:"+e1.getSalry());
        System.out.println("受雇时间:"+e1.getHour().getYear()+"年"+e1.getHour().getMonth()+"月"+e1.getHour().getDay()+"天");
        
    }
}

package 雇员;

public class Employee {
    //姓名、年薪、受雇时间
    private String name;
    private double salry;
    private MyDate hour;

    public Employee(String name, double salry, MyDate hour) {
        this.name = name;
        this.salry = salry;
        this.hour = hour;
    }

    public Employee() {
    }


    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public double getSalry() {
        return salry;
    }

    public void setSalry(double salry) {
        this.salry = salry;
    }

    public MyDate getHour() {
        return hour;
    }

    public void setHour(MyDate hour) {
        this.hour = hour;
    }

}


package 雇员;

import java.util.Scanner;

//受雇时间
public class MyDate {
    private int year;
    private int month;
    private int day;
    Scanner scan = new Scanner(System.in);

    public MyDate() {
    }

    public int getYear() {
        return year;
    }

    public void setYear(int year) {
        if(year <= 0)
            System.out.println("请输入合法年份!");
        else
            this.year = year;
    }

    public int getMonth() {
        return month;
    }

    public void setMonth(int month) {
        if(month <= 0 && month >=13)
            System.out.println("请输入合法的月份!");
        else
            this.month = month;
    }

    public int getDay() {
        return day;
    }

    public void setDay(int day) {
        MyDate d1 = new MyDate();
        //确定是否为闰年,然后确定好最大天数
        int max_day = d1.isLeapYear(this.year);
        if(day > max_day && day <= 0)
            System.out.println("请输入有效的天数!");
        else
            this.day = day;

    }
    //判断闰年
    public int isLeapYear(int year){
        int t = 0;
        //输入年份,用于判断闰年!
        switch (this.month) {
            case 1:
            case 3:
            case 5:
            case 7:
            case 8:
            case 10:
            case 12:
                t += 31;
                break;
            //对于2月份需要判断是否为闰年
            case 2:
                if ((this.year % 4 == 0 && this.year % 100 != 0) || (this.year % 400 == 0)) {
                    t += 29;
                    break;
                } else {
                    t += 28;
                    break;
                }
            case 4:
            case 6:
            case 9:
            case 11:
                t +=30;
                break;
            default:
                System.out.println("请输入正确的年份和月份");
                break;
        }
        return t;
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值