自定义类型赋值,自定义数组使用
自定义类型赋值就是创建对象,将地址值赋值给变量
例如,自定义一个Employee,有一个变量 Employee employee,要给employee赋值就
new Employee();
部门类 员工类 部门类里面有员工 |
public class Department {
private String name;
private int age;
private Employee employee;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public Employee getEmployee() {
return employee;
}
public void setEmployee(Employee employee) {
this.employee = employee;
}
public Department() {
// 第2个执行
System.out.println("这里是部门");
}
}
class Employee{
private String name;
private int age;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public Employee(String name, int age) {
this.name = name;
this.age = age;
}
public Employee(){
// 第1个执行
System.out.println("我是员工");
}
}
class TestDE{
public static void main(String[] args) {
Employee employee = new Employee();
employee.setAge(21);
employee.setName("欣欣");
Department department = new Department();
department.setAge(22);
department.setName("西西");
// 给部门员工赋值
department.setEmployee(employee);
String name = department.getName();
int age = department.getAge();
Employee employee1 = department.getEmployee();
// 输出employee1是地址值
System.out.println(name + " " + age + " " + employee1);
// 可以通过对象再获得对象内部的属性
System.out.println(name + " " + age + " " + employee1.getName() + " " + employee1.getAge());
}
}
老师类 学生类 老师带了很多学生 把学生存倒数组中 Student[] stus = new Student[n]; // 数组长度为n |
public class Teacher {
private String name;
private int age;
// 方法一
private Student[] stu;
// 方法二
Student[] stu1 = new Student[3];
public Teacher() {
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public Student[] getStu() {
return stu;
}
public void setStu(Student[] stu) {
this.stu = stu;
}
}
class Student {
private String name;
private int age;
public Student() {
}
public Student(String name, int age) {
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
class TestTS{
public static void main(String[] args) {
Teacher teacher = new Teacher();
teacher.setAge(26);
teacher.setName("阿西吧");
// 方法一
Student[] students = new Student[2];
students[0] = new Student("小瓜",21);
students[1] = new Student("小咪",19);
teacher.setStu(students);
System.out.println(teacher.getName() + " " + teacher.getAge() + " " + teacher.getStu());
Student[] stu1 = teacher.getStu();
for (int i = 0; i < stu1.length; i++) {
System.out.println(stu1[i]); // 得到学生的地址
System.out.println(stu1[i].getAge() + " " + stu1[i].getName()); // 得到学生的属性值
}
// 方法二
System.out.println("=================");
teacher.stu1[0] = new Student("阿鸡", 14);
System.out.println(teacher.stu1[0].getName());
System.out.println(teacher.stu1[0].getAge());
}
}
还没有评论,来说两句吧...