A simple understanding of the basic concepts of object-oriented - April 21, 2018 (experience notes)

Data table and simple Java class mapping

    Now suppose there is the following relational table, and now it is required to implement the following data association operations:
        a department has multiple employees;

        An employee has one or zero leaders;

program relationship

    In this program, it can be found that there is the following reference relationship definition between the Emp and Dept classes:
        an employee belongs to a department, and the department information should be saved in the employee, so a dept attribute is defined in the Emp class, and if there is a department, it is set An instantiated object of the Dept class, otherwise set to null;
        a department has multiple employees, if you want to describe more than this concept, you should use the object array to complete. So add an Emp class object array (Emp emps []) to the Dept class;
        an employee has a leader, and the leader information is the employee information, and the leader's self-association (Emp mgr) should be added to the Emp class
        ; Two simple Java classes can completely describe the structure definition of the data table, and then set and obtain the data according to the structure. The following information output is required:
        You can query his corresponding leadership information and department information according to an employee;

        All employees and the leadership information of each employee can be retrieved according to a department;

PS: (In the middle, in order to save trouble, use a systematic construction method; the red ones are all very classic data links)

package com.smxy;

import java.util.Arrays;

class Dept{
	private int id;
	private String name;
	private String loc;
	private Emp emps[];
	public Dept(int id, String name, String loc) {
		// TODO Auto-generated constructor stub
		this.id=id;
		this.name=name;
		this.loc=loc;
	}
	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getLoc() {
		return loc;
	}
	public void setLoc(String loc) {
		this.loc = loc;
	}
	public Emp[] getEmps() {
		return emps;
	}
	public void setEmps(Emp[] emps) {
		this.emps = emps;
	}
	@Override
	public String toString() {
		return "Dept [id=" + id + ", name=" + name + ", loc=" + loc +  "]";
	}
	
	
}
class Emp{
	private int id;
	private String ename;
	private String job;
	private double sal;
	private double comm;
	private Dept dept;
	private Emp mar;
	public Emp(int id, String ename, String job, double sal, double comm) {
		super();
		this.id = id;
		this.ename = ename;
		this.job = job;
		this.sal = sal;
		this.comm = comm;
	}
	
	public Emp getMar() {
		return mar;
	}

	public void setMar(Emp mar) {
		this.mar = mar;
	}

	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	public String getEname() {
		return ename;
	}
	public void setEname(String ename) {
		this.ename = ename;
	}
	public String getJob() {
		return job;
	}
	public void setJob(String job) {
		this.job = job;
	}
	public double getSal() {
		return sal;
	}
	public void setSal(double sal) {
		this.sal = sal;
	}
	public double getComm() {
		return comm;
	}
	public void setComm(double comm) {
		this.comm = comm;
	}
	public Dept getDept() {
		return dept;
	}
	public void setDept(Dept dept) {
		this.dept = dept;
	}
	@Override
	public String toString() {
		return "Emp [id=" + id + ", ename=" + ename + ", job=" + job + ", sal="
				+ sal + ", comm=" + comm + "]";
	}
	
}
public class Lei {
    public static void main(String args[]) {
    	Dept dept = new Dept(10,"ABC","beijing");
    	Emp ea = new Emp(1,"Li Hua","Employee",800.0,100.0);
    	Emp eb = new Emp(2,"Zhang San","Manager",12800.0,10000.0);
    	Emp ec = new Emp(3,"王五","CEO",18800.0,100000.0);
    	ea.setMar(eb);//Li Hua's boss Zhang San
    	eb.setMar(ec);//Zhang San's boss Wang Wu
    	System.out.println(ea.getMar().toString());
    	System.out.println(ea.getMar().getMar().toString());
    	System.out.println("--------------------------------");
    	dept.setEmps(new Emp[]{ea,eb,ec});
    	for(int i=0;i<dept.getEmps().length;i++){
    		System.out.println(dept.getEmps()[i].toString());
    		if(dept.getEmps()[i].getMar()!=null){
    			System.out.println("\t"+dept.getEmps()[i].getMar().toString());
    		}
    	}
    }
}

    

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324619921&siteId=291194637