内容回顾
在上一P中:d/4294,我们简单介绍了 Thomas Vesper 的学历、爱好以及家庭情况。
整个项目的结构看起来像这样:
|-- 自定义的项目名
|-- com.example.project1(主包)
|-- academy
|-- AwesomeUniversity.java
|-- DegreeOfAwesomeUniversity
|-- Field.java
|-- IDegree.java
|-- University
|-- person
|-- hobby
|-- IHobbiesOfTV.java
|-- student
|-- CollegeStudent.java
|-- CyberTV.java
|-- VirtualPerson.java
|-- Main.java
此外,值得注意的是,部分类中使用了“饱汉式”的单例设计模式,如 CyberTV 类。因为,某一赛博人类的具体实现,都是无一物二的。简单来说,如果类比成真实世界的人类,即便某一群人的身上会有相似之处,但他们也依然是独一无二的个体。
1. 将其构造函数被设为私有的
private CyberTV(String name, int age, String gender, String nickname, String studentId) {
super(name, age, gender, nickname, studentId);
workingExperience = new ArrayList<>();
}
2. 设计 CyberTV 实例
private static CyberTV instance;
public static CollegeStudent getInstance() {
if (instance == null) {
instance = new CyberTV("Thomas Vesper", 25,
"Male", "TV", "S114511114");
}
return instance;
}
此外,你也可能注意到,某些类中的某些属性仅实现了 getXXX() 方法,而该属性只能被构造函数改变一次。在实际的开发过程中,这是很常见的做法。类比到现实世界,可以想象成伴随人出生时,与生俱来的一些属性。如血型,这类后天无法被改变的属性。
在此,也可以引申出另一个有关于属性命名的问题。回顾 VirtualPerson 类中对性别属性的定义:
private String gender;
public String getGender(){
return this.gender;
}
public void setGender(String newValue){
this.gender = newValue;
}
为什么在这里将其命名为 gender 并保留 setGender(String newValue) 方法?
在英文语义中,sex 在绝大多数情况下仅表示二元性别(即生理性别,只有男和女两种选项);而 gender 更加倾向于表达自身所认同的性别,因此是可以随时改变的。
如果在设计类时,仅考虑定义二元性别,那么就应该符合上述的“与生俱来”的属性;也就应该删除 setSex(String newValue) 方法,并永远保证 sex 属性只能被包含该属性的类中的构造函数所赋值,且仅赋值一次。
回顾内容到此结束。
在本篇(本系列的第二P)中,我们将继续讲述这段小故事,并完成与之相关的练习题。
书接上回,TV的梦想是毕业后能够入职总部位于硅谷的Awesome IT公司。不过,即使他有名校的文凭,也没能成功入职。
思路:需要创建入职相关类:如需要一个Company抽象类表示公司,其中可能包括名称属性、公司类型属性、接收职位申请方法、办理入职方法等。
需要一个抽象类或接口来表示职业,本文选择创建 ICareer 接口。该接口中可能包含获取职业名称方法、描述职业方法以及获得要求的领域方法。
此外,TV已经从学校里毕业了,身份也需要有所转变,如,拥有了打工人身份。打工人可以做的事情可能包含:获取当前在职状态、找工作、转行、展示工作经验等。可以通过抽象类或接口进行实现,本文中通过接口实现。
TV feels unhappy,但后来他还是成功入职了一家火箭公司,职位是嵌入式开发工程师,负责开发火箭上的嵌入式电脑。
思路:可能需要空接口来表示他的职业为计算机类的职位,因为嵌入式开发属于计算机行业。
收到录用通知,被通知第二天去办理入职的TV非常happy。第一时间打电话给老妈,说自己得了 MVP ,成功从计算机行业转行到了航空业。
不料,老妈听到后不但没有表扬TV,还质问他为什么大学毕业了还找不到女朋友,怎么不找找自己问题。于是,该通话在一阵不愉快的交谈声中草草结束。
该段对话被牛棚中的赛博牛牛听到了,赛博牛牛当即对 TV 提出了质疑,说:“嵌入式开发岗位属于计算机行业,你怎么就转行了?”
TV听后,恼羞成怒:“这你都分不清,还在这里指点什么?”
不料,此举激怒了学识渊博的牛牛,并当即停止为TV家的农场进行产奶。牛牛说道:“想要让我恢复产奶,你就必须使用一段代码证明你已经进行了转行。当然,为了公平起见,我会叫来这个世界上最聪明的类人生物:Thinker(早年与赛博牛牛交好,成为好友的过程略),与我一起评判此事。”
思路:需要一些内容,对是否转行进行评判。
人物介绍:91 号 Thinker
据传,在赛博世界中有个疯老头科学家,曾创造出100个类人的人工智能生物。该项目命名为 Project "Thinker"。该项目共生产了100个这样的智慧生命体,老头科学家为找出最聪明的生命体,便开启了N轮辩论赛(使几个生命体在一起就某些话题展开分析与辩论的比赛),最后仅淘汰胜出了一个智慧生命体——91号(该编号是按生产顺序排布的),其他的则全部被消灭。
而赛博牛牛口中所说的 Thinker,便是这个91号,其具有超高智力和分析能力。后来成为赛博世界中令人仰慕的“人”,大家都亲切地称他为 No. 91 - Mr. T(编号 91,T先生)。据说,在赛博世界中也有个与Java语言类似的编程语言 JVAV,其中的 Object 类就是继承自这个91号的,可见其实力之强大。
思路:为 Thinker 新建赛博人类的实例。
这位 Thinker 有如下爱好:
- 爱微软:指不遗余力地购买与喜爱微软产品,并在遇到软黑时,与之全力火拼。
- 深度思考:在遇到对自己不利的辩词时,将持续输出看似有道理,实则车轱辘话的内容。
赛博牛牛的罢工行为,将导致TV家每小时至少损失 $1145.14 刀乐,TV需要想办法来弥补这一切。不过,根据赛博世界不成文的规矩,奶牛罢工时,不得直接将其杀死或食用其牛肉。因此,TV只能想办法哄奶牛开心,或编写一段强有力的证明代码。TV还想请求奶牛放他一马,不过,无论TV再说什么,赛博牛牛只会读出一个链接:
[baike.baidu.com/item/%E5%B5%8C%E5%85%A5%E5%BC%8F%E6%8A%80%E6%9C%AF/11025546](https://)
思路:创建抽象动物类,其中可能包括名称等属性,然后创建赛博奶牛类,使其继承自抽象动物类。
私下里,赛博牛牛电话联系了 No. 91 - Mr. T,反复叮嘱 T 先生在本次判决中,保持客观中立。于是,T 先生便毫不犹豫地答应了这一请求。
回到房中的TV感到筋疲力尽,不过他还是毅然决然地打开了电脑,启动了 IDEA,试图使用一些代码来证明他确实转行了。
先对身份转变相关类进行实现。
1. 创建可拉磨(可工作)接口
在 person 包中添加 working 包,并在其中创建 IWorkable 接口:
package com.example.project1.person.working;
import com.example.project1.career.ICareer;
import com.example.project1.company.Company;
public interface IWorkable {
/**
* The function is used to judge if a cyber person is currently in a position.
*
* @return The judging result, true if the answer is yes otherwise false.
*/
boolean isCurrentlyInAPosition();
/**
* The function is used to help a cyber person get a job from a company.
*
* @param targetCareer The career to expect to do.
* @param targetCompany The company to expect to work in.
* @return true if the cyber person succeeded in doing so, otherwise false.
*/
boolean tryGettingAJobFromACompany(ICareer targetCareer, Company targetCompany);
/**
* The function is used to get a job for a cyber person.
*
* @param career The instance of the object which implements the ICareer interface.
* To update the career info of the cyber person.
* @param company The instance of the object that extends the abstract Company class.
*/
void getAJob(ICareer career, Company company);
/**
* The function is used to get the current career of a cyber person.
*
* @return An instance of the object that implements the ICareer interface.
*/
ICareer getCurrentCareer();
/**
* The function is used to change the profession of a cyber person.
*
* @param newCareer A new instance of the object that implements the ICareer interface.
* @param company The instance of the object that extends the abstract Company class.
*/
void changeProfession(ICareer newCareer, Company company);
/**
* The function is used to get all the working experience of a cyber person.
*
* @return An instance of ArrayList with the generic type of the WorkingExperience class.
*/
WorkingExperience[] getWorkingExperience();
}
既然某赛博人类可以进行工作,那么其必会在日后成为某公司的员工(仅仅为理想情况下的假设)。因此需要一个抽象类或接口对员工进行描述。其中可能包含获取当前公司方法,以及离职方法等。
2. 创建员工接口
在 working 包中创建 IStaff 接口:
package com.example.project1.person.working;
import com.example.project1.company.Company;
public interface IStaff {
/**
* The function is used to get the current working company of a virtual person.
* @return An instance of the Company object.
*/
Company getWorkingCompany();
void resign(int workingYears);
}
然后,更新 IWorkable 接口,使其“继承”自 IStaff 接口:
public interface IWorkable extends IStaff {
. . .
}
既然有了工作,那么就需要某样东西来描述工作经验。工作经验可能包含获取所在公司属性、职业属性、工作年限属性等。
3. 新建工作经验类
在 working 包中新建 WorkingExperience 类
package com.example.project1.person.working;
import com.example.project1.career.ICareer;
import com.example.project1.company.Company;
public class WorkingExperience {
public WorkingExperience(Company workingCompany, ICareer career, int years) {
this.workingCompany = workingCompany;
this.career = career;
this.years = years;
}
private Company workingCompany;
public Company getWorkingCompany() {
return workingCompany;
}
private ICareer career;
public ICareer getCareer() {
return this.career;
}
private int years;
public int getYears() {
return years;
}
@Override
public String toString() {
return String.format("Used to work in %s", workingCompany.getName() + ",\n"
+ String.format("with %d year of working experience as a %s", years, career.getName() + "."));
}
}
完成上述内容创建后,需要更新 CyberTV 类,使其实现 IWorkable 接口。
更新后的 CyberTV 类如下所示:
package com.example.project1.person;
import com.example.project1.career.ICareer;
import com.example.project1.company.Company;
import com.example.project1.person.hobby.IHobbiesOfTV;
import com.example.project1.person.student.CollegeStudent;
import com.example.project1.person.working.IWorkable;
import com.example.project1.person.working.WorkingExperience;
import java.util.ArrayList;
public class CyberTV extends CollegeStudent implements IHobbiesOfTV, IWorkable {
private ArrayList<WorkingExperience> workingExperience;
private CyberTV(String name, int age, String gender, String nickname, String studentId) {
super(name, age, gender, nickname, studentId);
workingExperience = new ArrayList<>();
}
@Override
public void observeHandsOfAPerson(VirtualPerson luckyTarget) {
if ("Femboy".equals(luckyTarget.getGender()) ||
"Male".equals(luckyTarget.getGender())) {
System.out.println("TV: 看看手");
}
}
@Override
public void observeFaceOfAPerson(VirtualPerson luckyTarget) {
if ("Femboy".equals(luckyTarget.getGender()) ||
"Male".equals(luckyTarget.getGender())) {
System.out.println("TV: 看看脸");
}
}
@Override
public void tryGetTheFirstKissOfAPerson(VirtualPerson luckyTarget) {
if ("Femboy".equals(luckyTarget.getGender()) ||
"Male".equals(luckyTarget.getGender())) {
System.out.println("TV: 能不能把初吻给我");
}
}
@Override
public void shareShitContentToTheSocialNetworks() {
System.out.print("""
New content shared by TV:\s
牛x网:《我儿子28岁了,现在一提结婚就炸,怎么办?》
抽象网:《婚恋市场变成了xx模式,是谁的责任?》""");
}
@Override
public void seekingForAGirlToFillInLoveWith(VirtualPerson target) {
if ("Female".equals(target.getGender())) {
System.out.println("TV: 找对象");
}
}
@Override
public String toString() {
return super.toString();
}
private static CyberTV instance;
public static CollegeStudent getInstance() {
if (instance == null) {
instance = new CyberTV("Thomas Vesper", 25,
"Male", "TV", "S114511114");
}
return instance;
}
private ICareer currentCareer;
private Company currentWorkingCompany;
@Override
public boolean isCurrentlyInAPosition() {
if (currentCareer.equals(null)) {
return false;
}
return true;
}
@Override
public boolean tryGettingAJobFromACompany(ICareer targetCareer, Company targetCompany) {
return targetCompany.acceptANewPost(targetCareer, this);
}
@Override
public void getAJob(ICareer career, Company company) {
this.currentCareer = career;
this.currentWorkingCompany = company;
}
@Override
public ICareer getCurrentCareer() {
return this.currentCareer;
}
@Override
public void changeProfession(ICareer newCareer, Company company) {
WorkingExperience[] filtered = (WorkingExperience[]) workingExperience.stream().filter(exp ->
company.equals(exp.getWorkingCompany()) &&
newCareer.equals(exp.getCareer()))
.toArray();
if (filtered.length > 0) {
System.out.println("Can not change profession to the same career and the same company");
return;
}
this.currentCareer = newCareer;
this.currentWorkingCompany = company;
}
@Override
public WorkingExperience[] getWorkingExperience() {
return this.workingExperience.toArray(new WorkingExperience[0]);
}
@Override
public Company getWorkingCompany() {
return this.currentWorkingCompany;
}
@Override
public void resign(int workingYears) {
WorkingExperience exp = new WorkingExperience(this.currentWorkingCompany,
this.currentCareer, workingYears);
workingExperience.add(exp);
this.currentCareer = null;
this.currentWorkingCompany = null;
}
}
4. 创建 Company 抽象类
在主包下新建 company 包,在其中新建 Company 类:
package com.example.project1.company;
import java.util.ArrayList;
import com.example.project1.career.ICareer;
import com.example.project1.person.student.CollegeStudent;
import com.example.project1.person.working.IStaff;
public abstract class Company {
private String name;
private ArrayList<ICareer> positions;
public Company(String name) {
this.name = name;
positions = new ArrayList<>();
}
public String getName() {
return name;
}
/**
* The function is used to get the type of the company.
*
* @return A member of the enum type CompanyType.
*/
public abstract CompanyType getCompanyType();
/**
* The function is used to accept a new post from a cyber college student
* who wants to get a job from the company.
*
* @param career An instance of the object that implements the ICareer interface.
* In this situation, is the carrier which was applied by
* the cyber college student.
* @param applier An instance of the CollegeStudent class.
* In this situation, is the cyber college student who is looking
* for a job.
* @return true if the post was received successfully, otherwise false.
*/
public abstract boolean acceptANewPost(ICareer career, CollegeStudent applier);
/**
* The function is used to take a new staff in.
*
* @param staff An instance of the object that implements the IStaff interface.
*/
public abstract void admitNewStaff(IStaff staff);
}
注意:此时我们还未创建 CompanyType 枚举,你可以使用代码注释功能对与 CompanyType 有关的内容进行注释,以消除编译器警告。
5. 创建 CompanyType 枚举
在 company 包下新建 CompanyType 枚举,并在其中定义IT公司,销售公司与火箭公司 3个枚举成员:
package com.example.project1.company;
public enum CompanyType {
ITCompany,
SalesCompany,
RocketCompany
}
6. 创建职业接口
在主包下新建 career 包,并在其中创建 ICareer 接口:
package com.example.project1.career;
import com.example.project1.academy.Field;
public interface ICareer {
/**
* The function is used to get the name of the current career.
* @return The name of the career.
*/
String getName();
/**
* The function is used to describe the career.
*/
void describeTheCareer();
/**
* The function is used to get the Field of the career.
* @return A member of the enum type Field.
*/
Field getRequiredFiled();
}
7. 创建计算机科学相关职业接口
在 career 包中新建 computer_related 包,并在其中新建 IComputerScienceRelatedCareer 空接口,用于描述计算机科学相关职业,并使该接口“继承”自 ICareer 接口:
package com.example.project1.career.computer_related;
import com.example.project1.career.ICareer;
public interface IComputerScienceRelatedCareer extends ICareer {
}
8.创建嵌入式工程师职业类
在computer_related 包中新建 EmbeddedEngineerCareer 类,使其实现计算机科学相关职业接口:
package com.example.project1.career.computer_related;
import com.example.project1.academy.Field;
public class EmbeddedEngineerCareer implements IComputerScienceRelatedCareer {
private String name;
public EmbeddedEngineerCareer(String name) {
this.name = name;
}
@Override
public String getName() {
return this.name;
}
@Override
public void describeTheCareer() {
System.out.println("Embedded Engineer: An embedded systems or software engineer helps design, develop, and maintain embedded systems in products.\n" +
"The role often includes software development. But it also requires knowledge of entire embedded systems.\n" +
"From [qt.io]");
}
@Override
public Field getRequiredFiled() {
return Field.ComputerScience;
}
private static IComputerScienceRelatedCareer instance;
public static IComputerScienceRelatedCareer getInstance() {
if (instance == null) {
instance = new EmbeddedEngineerCareer("Embedded Engineer");
}
return instance;
}
}
9. 创建航空航天相关职业接口
在 career 包下新建 aviation_related 包,并在其中新建 IAviationIndustryRelatedCareer 空接口,用于描述航空航天相关职业,并使该接口“继承”自 ICareer 接口:
package com.example.project1.career.aviation_related;
import com.example.project1.career.ICareer;
public interface IAviationIndustryRelatedCareer extends ICareer {
}
10.创建火箭工程师职业类
在aviation_related 包中新建 RocketEngineerCareer 类,使其实现航空航天相关职业接口:
package com.example.project1.career.aviation_related;
import com.example.project1.academy.Field;
public class RocketEngineerCareer implements IAviationIndustryRelatedCareer {
private String name;
public RocketEngineerCareer(String name) {
this.name = name;
}
@Override
public String getName() {
return this.name;
}
@Override
public void describeTheCareer() {
System.out.println("A Rocket Propulsion Engineer plays a crucial role in the design, development, and testing of propulsion systems used in rockets and spacecraft.\n" +
"Their primary responsibility is to ensure that these systems operate efficiently and safely,\n" +
"contributing to the overall mission success of the organization. \n\n" +
"By applying principles of physics and engineering, these professionals help drive innovation in the aerospace industry,\n" +
"directly impacting company objectives such as advancing technology, meeting project deadlines, and maintaining safety standards.\n");
}
@Override
public Field getRequiredFiled() {
return Field.AeronauticalAndAstronauticalEngineering;
}
private static IAviationIndustryRelatedCareer instance;
public static IAviationIndustryRelatedCareer getInstance() {
if (instance == null) {
instance = new RocketEngineerCareer("Rocket Engineer");
}
return instance;
}
}
11. 新建牛X IT 公司类
接下来需要创建 TV 心心念念要进入的公司(类)。在 company 包中新建 AwesomeITCompany 类,使其继承自 Company 抽象类:
package com.example.project1.company;
import com.example.project1.career.ICareer;
import com.example.project1.career.computer_related.EmbeddedEngineerCareer;
import com.example.project1.person.student.CollegeStudent;
import com.example.project1.person.working.IStaff;
import java.util.ArrayList;
public class AwesomeITCompany extends Company {
private static ArrayList<ICareer> openPositions;
private static ArrayList<IStaff> staffs;
private AwesomeITCompany() {
super("Awesome IT Company");
openPositions = new ArrayList<>();
openPositions.add(EmbeddedEngineerCareer.getInstance());
staffs = new ArrayList<>();
}
@Override
public CompanyType getCompanyType() {
return CompanyType.ITCompany;
}
@Override
public boolean acceptANewPost(ICareer career, CollegeStudent applier) {
if ("TV".equals(applier.getNickname())) {
return false;
}
return true;
}
@Override
public void admitNewStaff(IStaff staff) {
if (AwesomeITCompany.class.equals(staff.getWorkingCompany().getClass())) {
staffs.add(staff);
}
}
private static Company instance;
public static Company getInstance() {
if (instance == null) {
instance = new AwesomeITCompany();
}
return instance;
}
}
12. 创建 Thinker 类并通过接口实现其爱好与深度思考功能
在 person 包下新建 Thinker 类:
package com.example.project1.person;
import com.example.project1.person.hobby.IHobbiesOfThinker;
import com.example.project1.person.intelligence.IThinkable;
public class Thinker extends VirtualPerson implements IThinkable, IHobbiesOfThinker {
private int id;
private Thinker(String name, int age, String gender, String nickname, int id) {
super(name, age, gender, nickname);
this.id = id;
}
public int getId() {
return this.id;
}
@Override
public void think(Object anything) {
if (anything instanceof CyberTV) {
System.out.println(getNickname()+ ": 不予置评");
}
}
private static Thinker instance;
public static Thinker getInstance(){
if (instance == null){
instance = new Thinker("No. 91 - Thinker", 22, "Male",
"No. 91 - Mr. T", 91);
}
return instance;
}
@Override
public void deepThink() {
System.out.println(getNickname() + "缺芯少魂,哪来的生态?");
System.out.println(getNickname() + "没有南向生态,何来的北向生态");
}
@Override
public void loveMicrosoft() {
System.out.println(getNickname() + "说微软Arm发展不好的,看不见Prism?");
System.out.println(getNickname() + "看不见2023年以来新增的一大堆原生Arm64软件?");
}
}
注意:此时还未创建 IThinkable 和 IHobbiesOfThinker 接口。
在 person 包下新建 intelligence 包,并在其中新建 IThinkable 接口:
package com.example.project1.person.intelligence;
public interface IThinkable {
/**
* The function is used to think anything.
* @param anything An instance of the Object class.
*/
void think(Object anything);
}
在 hobby 包下新建 IHobbiesOfThinker 接口:
package com.example.project1.person.hobby;
public interface IHobbiesOfThinker {
/**
* The function is used to show some deep thinks of a thinker.
*
* You know, the biggest hobby of the thinkers is that, they often
* fall into the zen status while thinking.
*/
void deepThink();
/**
* The function is used to show No. 91 Mr. T's love for Microsoft.
*/
void loveMicrosoft();
}
13. 建立赛博动物抽象类
在主包下,新建 farm 包,然后在 farm 包中新建 animals 包;在animals 包中新建 CyberAnimal 类:
package com.example.project1.farm.animals;
public abstract class CyberAnimal {
private String name;
public CyberAnimal(String name){
this.name = name;
}
/**
* The function is used to get the name of the cyber animal.
* @return
*/
public String getName(){
return this.name;
}
/**
* The function is used to make the cyber animal say something.
* @param words The words it wanna saying.
*/
public abstract void saySomething(String words);
/**
* Sometimes, the cyber animals have a habit of repeating words.
* @param wordsToRepeat The words to be repeated by the animal.
*/
public abstract void activateLanguageRepeaterMode(String wordsToRepeat);
}
14. 建立赛博奶牛类
在 animals 包下新建 CyberCow 类,及其可发现真相 ITruthFindable 接口:
package com.example.project1.farm.animals.cow;
import com.example.project1.farm.animals.CyberAnimal;
import com.example.project1.person.working.IWorkable;
import com.example.project1.person.working.WorkingExperience;
import com.example.project1.career.computer_related.IComputerScienceRelatedCareer;
import java.util.Arrays;
public class CyberCow extends CyberAnimal implements ITruthFindable {
public CyberCow() {
super("Cyber Cow");
}
private boolean isSatisfied;
public boolean getIsSatisfied() {
return this.isSatisfied;
}
public void setIsSatisfied(boolean newValue) {
this.isSatisfied = newValue;
}
public void produceMilk() {
if (isSatisfied) {
System.out.println("Producing milk . . .");
} else {
System.out.println(". . . ");
}
}
@Override
public void saySomething(String words) {
System.out.println("Cyber Cow: " + words);
}
@Override
public void activateLanguageRepeaterMode(String wordsToRepeat) {
for (int i = 0; i < 10; i++) {
System.out.println("Cyber Cow: " + wordsToRepeat);
}
}
@Override
public boolean ifACyberMaleHasChangedHisProfession(IWorkable workablePerson) {
WorkingExperience[] exp = workablePerson.getWorkingExperience();
long careerCount = Arrays.stream(exp).filter(e ->
!(e.getCareer() instanceof IComputerScienceRelatedCareer))
.count();
if (careerCount > 0) {
System.out.println("CyberCow: 转行✌️!");
isSatisfied = true;
return true;
}
System.out.println("CyberCow: 转了个寂寞");
isSatisfied = false;
return false;
}
@Override
public void tellMeWhyBabe(IWorkable workablePerson) {
WorkingExperience[] exp = workablePerson.getWorkingExperience();
long careerCount = Arrays.stream(exp).filter(e ->
!(e.getCareer() instanceof IComputerScienceRelatedCareer))
.count();
System.out.println("CyberCow: 在工作履历中发现了" + careerCount + "条非 IComputerScienceRelatedCareer 的工作经历。\n"
+ "因此" + (careerCount > 0 ? "经历了" : "未经历") + "转行");
}
}
package com.example.project1.farm.animals.cow;
import com.example.project1.person.working.IWorkable;
/**
* For cyber cows living in the cyber world, they have the ability to find out
* what career(s) a virtual person actually did.
*/
public interface ITruthFindable {
/**
* The function is used to find out if a workable cyber person once changed his profession.
* @param workablePerson An instance of the object that implements the IWorkable interface.
* That is, a cyber person who is able to work.
* @return The finding result, true if the cyber person once changed his profession,
* otherwise false.
*/
boolean ifACyberMaleHasChangedHisProfession(IWorkable workablePerson);
/**
* The function is used to describe the finding result.
* Look in my eyes! Tell me why babe, why?
* @param workablePerson An instance of the object that implements the IWorkable interface.
* That is, a cyber person who is able to work.
*/
void tellMeWhyBabe(IWorkable workablePerson);
}
最后,更新 Main 中的代码:
package com.example.project1;
import com.example.project1.career.computer_related.EmbeddedEngineerCareer;
import com.example.project1.company.AwesomeITCompany;
import com.example.project1.company.Company;
import com.example.project1.company.RocketCompany;
import com.example.project1.farm.animals.cow.CyberCow;
import com.example.project1.person.CyberTV;
import com.example.project1.person.Thinker;
import com.example.project1.person.student.CollegeStudent;
import com.example.project1.academy.AwesomeUniversity;
import com.example.project1.academy.DegreeOfAwesomeUniversity;
import com.example.project1.person.working.IStaff;
import com.example.project1.person.working.IWorkable;
public class Main {
public static void main(String[] args) {
CollegeStudent cyberTV = CyberTV.getInstance();
AwesomeUniversity awesomeUniversity = new AwesomeUniversity();
awesomeUniversity.conferADegree(cyberTV, DegreeOfAwesomeUniversity.ComputerSciencePhdDegree);
System.out.println(cyberTV);
Company awesomeITCompany = AwesomeITCompany.getInstance();
boolean isAcceptedByAwesomeITCompany = ((IWorkable) cyberTV)
.tryGettingAJobFromACompany(
EmbeddedEngineerCareer.getInstance(),
awesomeITCompany);
if (isAcceptedByAwesomeITCompany) {
System.out.println("Succeeded to get a job from " + awesomeITCompany.getName() + ".");
} else {
System.out.println("Failed to get a job from " + awesomeITCompany.getName() + ".");
}
Company rocketCompany = RocketCompany.getInstance();
boolean isAcceptedByRocketCompany = ((IWorkable) cyberTV)
.tryGettingAJobFromACompany(
EmbeddedEngineerCareer.getInstance(),
rocketCompany);
if (isAcceptedByRocketCompany) {
System.out.println("Succeeded to get a job from " + rocketCompany.getName() + ".");
((IWorkable)cyberTV).getAJob(EmbeddedEngineerCareer.getInstance(), rocketCompany);
} else {
System.out.println("Failed to get a job from " + rocketCompany.getName() + ".");
}
System.out.print("TV: ");
((IWorkable)cyberTV).getCurrentCareer().describeTheCareer();
((IStaff) cyberTV).resign(3);
Thinker thinker91 = Thinker.getInstance();
System.out.println(thinker91);
thinker91.deepThink();
thinker91.loveMicrosoft();
thinker91.think(cyberTV);
CyberCow cow = new CyberCow();
cow.ifACyberMaleHasChangedHisProfession(((IWorkable) cyberTV));
cow.tellMeWhyBabe(((IWorkable) cyberTV));
System.out.print("CyberCow: ");
cow.produceMilk();
}
}
程序的运行结果如下所示:
Name: Thomas Vesper, Age: 25, Gender: Male, Nickname: TV. Student Id: S114511114
Degree conferred by Awesome University, 4 years of academic system, in the field of ComputerScience.
Failed to get a job from Awesome IT Company.
Succeeded to get a job from Awesome Rocket Company.
TV: Embedded Engineer: An embedded systems or software engineer helps design, develop, and maintain embedded systems in products.
The role often includes software development. But it also requires knowledge of entire embedded systems.
From [qt.io]
Name: No. 91 - Thinker, Age: 22, Gender: Male, Nickname: No. 91 - Mr. T
No. 91 - Mr. T缺芯少魂,哪来的生态?
No. 91 - Mr. T没有南向生态,何来的北向生态
No. 91 - Mr. T说微软Arm发展不好的,看不见Prism?
No. 91 - Mr. T看不见2023年以来新增的一大堆原生Arm64软件?
No. 91 - Mr. T: 不予置评
CyberCow: 转了个寂寞
CyberCow: 在工作履历中发现了0条非 IComputerScienceRelatedCareer 的工作经历。
因此未经历转行
CyberCow: . . .