多易javaSE基础 day7 课程日志 面向对象

今天练习了面向对象的相关内容.跟着core java学习的,感觉书上讲的很细很清楚.学起来体验很好.练习的代码如下:

package day06;

import java.time.LocalDate;

public class DemoClass1 {
    public static void main(String[] args) {

        Employee[] staff = new Employee[6];
        staff[0] = new Employee( "杨",15000,2020,12,30);
        staff[1] = new Employee( "刘",2020,11,30);
        staff[2] = new Employee( "欧",17000,2020,10,30);
        staff[3] = new Employee( "胡",18000,2020,9,30);
        staff[4] = new Employee( "崔",19000,2020,8,30);
        staff[5] = new Employee( "徐",20000,2020,7,30);

        for (int i = 0; i < staff.length; i++) {
            System.out.println(staff[i].getId());
        }
    }
}

class Employee{
    private static int nextId = 0;
    private final int id;
    private final String name;
    private int salary = 0;
    private final LocalDate inDay ;

    public Employee( String name, int salary, int year, int month, int day) {
        nextId++;
        this.id = nextId;
        this.name = name;
        this.salary = salary;
        this.inDay = LocalDate.of(year,month,day);
    }
    public Employee( String name, int year, int month, int day) {
        nextId++;
        this.id = nextId;
        this.name = name;
        this.inDay = LocalDate.of(year,month,day);
    }

    public String getName() {
        return name;
    }


    public int getSalary() {
        return salary;
    }

    public void setSalary(int salary) {
        this.salary = salary;
    }

    public int getId() {
        return id;
    }

    public LocalDate getInDay() {
        return inDay;
    }

}

记一下类和对象的知识点吧
类是对象的模板.对象的三个主要特性:行为,状态,标识.
对象的数据要保证私有,要通过方法去操作对象的数据.
一定要养成对数据进行初始化的习惯.
一个系统中,名词是类或对象,动词是方法.
静态方法调用格式:类名.方法名(参数列表);
静态方法无法访问和改变对象的状态.但可以访问静态字段.

©️2020 CSDN 皮肤主题: 1024 设计师: 上身试试 返回首页
实付0元
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值