欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

多易javaSE基础 day7 课程日志

程序员文章站 2022-04-16 16:25:18
今天练习了面向对象的相关内容.跟着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( "杨"...

今天练习了面向对象的相关内容.跟着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;
    }

}

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

本文地址:https://blog.csdn.net/weixin_44855583/article/details/107689260

相关标签: 多易 java