常见的关系有:依赖、聚合、继承
使用构造器constructor构造新实例(构造并初始化对象)
import java.time.*;
public class EmployeeTest {
public static void main(String[] args) {
System.out.println("Hello" );
Employee[] staff = new Employee[3];
staff[0] = new Employee("A", 200, 1991, 1, 1);
staff[1] = new Employee("B", 300, 1992, 2, 2);
staff[2] = new Employee("C",400,1993,3,3);
for (Employee e:staff)
e.raiseSalary(5);
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){
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 byPersent){
double raise = salary * byPersent/100;
salary += raise;
}
}
注:开始还用eclipse试了一下,发现我还是喜欢IDEA哈哈,然后找了一下idea创建java文件时自动添加作者和创建时间的方法,参考了这篇 https://blog.csdn.net/qq_39098813/article/details/80731698
staff[0] = new Employee("A", 200, 1991, 1, 1);
这里的Employee,是调用的构造器(调用构造函数?存疑)
final关键字只是表示存储在这个变量中的对象引用不会指向其他的对象,但是这个对象本身是可以更改的
如果将域定义为static,每个类中只有一个这样的域,静态域属于类,不属于任何独立的对象
静态方法是一种不能像对象实施操作的方法,例如:Math.pow(x,a),运算时不使用任何Math对象,即没有隐式的参数
这节没看懂啊????讲啥呢
import static java.lang.System.*;
/**
* @Author:Wconquer
* @Date:2019/8/14 15:32
*/
public class PackageTest {
public static void main(String[] args) {
out.println("Hello,World!");
}
}
真实没看懂