java基础五 面向对象--封装实例

员工部分

package com.company;
import javax.security.auth.Subject;
public class Person {
    private String name;
    private String number;
    private String sex;
    private String age;
    private Company companySubject;
    public Person(String name, String number, String sex, String age, Company companySubject) {
        this.name = name;
        this.number = number;
        this.sex = sex;
        this.age = age;
        this.setCompanySubject(companySubject);
    }
    public Company getCompanySubject() {
        return companySubject;
    }
    public void setCompanySubject(Company companySubject) {
        this.companySubject = companySubject;
    }
    public String getSex() {
        return sex;
    }
    public void setSex(String sex) {
        this.sex = sex;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getNumber() {
        return number;
    }
    public void setNumber(String number) {
        this.number = number;
    }
    public String getAge() {
        return age;
    }
    public void setAge(String age) {
        this.age = age;
    }
    public String introduction() {
        String str ="姓名:" + this.name + "\n工号:" + this.number + "\n性别" + this.sex + "\n职务" + this.getCompanySubject().getBumen() + this.getCompanySubject().getZhiwu();
        return str;
    }
}

公司部分

 package com.company;
import javax.security.auth.Subject;
public class Company {
    private String Bumen;
    private String Zhiwu;

    public Company(String bumen, String zhiwu) {
        this.setBumen(bumen);
        this.setZhiwu(zhiwu);
    }
    public String getBumen() {
        return Bumen;
    }
    public void setBumen(String bumen) {
        Bumen = bumen;
    }
    public String getZhiwu() {
        return Zhiwu;
    }
    public void setZhiwu(String zhiwu) {
        Zhiwu = zhiwu;
    }
}

测试部分

package com.company;
import javax.security.auth.Subject;
import com.company.Company;
import com.company.Person;
public class test {
    public static void main(String[] args) {
        Company company1 = new Company("人事部","经理");
        Person person1 = new Person("张明","S221","男","22",company1);
        String str = person1.introduction();
        System.out.println(str);
    }

}

随机浏览