user.ts
import Msg from "./msg";
import Reply from "./reply";
/**
* 用户
*/
export default class User {
// 编号
id: number;
// 姓名
name: string;
// 关注(好友)
friends: User[];
// 粉丝
fans: Array<User>;
// 所有消息(状态)
msgs: Array<Msg>;
/**
* 用户构造方法
* @param id 用户编号
*
* @param name 用户昵称
*/
constructor(id: number, name: string) {
this.id = id;
this.name = name;
//给数组赋值(初始化)
this.friends = new Array();
this.fans = new Array();
this.msgs = new Array();
}
//业务逻辑(领域)
// ------------------------------------
showInfo(): void {
console.log(`用户:${this.name}`);
console.log(`关注:${this.friends.length}`);
console.log(`粉丝:${this.fans.length}`);
console.log(`消息:${this.msgs.length}`);
console.log(`----------------------------`);
}
/**
* 关注(添加好友)
* @param other 新关注的好友
*
*/
// 关注
addFriend(other: User) {
// if (!this.friends.includes(other)) {
// 当前用户的好友多了other
this.friends.push(other);
// other的粉丝列表多了一个this
other.fans.push(this);
// }
}
// 取消关注
removeFriend(other: User) {
// if(this.friends.includes(other)){
// splice
// 获得other的索引
// for (let i = 0; i < this.friends.length; i++) {
// if (this.friends[i] === other) {
// this.friends.splice(i, 1);
// break;
// }
// }
this.friends.splice(this.friends.indexOf(other), 1);
// 从other粉丝列表删除当前用户
other.fans.splice(other.fans.indexOf(this), 1);
// }
}
/**
* 发状态,返回了一个消息对象
* 方法内部封装了Msg对象的创建过程
* @param content 状态的内容
*/
// 发状态
sendMsg(content: string): Msg {
// 创建消息,消息获得了作者
let m = new Msg(content, this);
// 用户获得了消息
this.msgs.push(m);
// 存储
m.id = Math.random();
return m;
}
/**
* 给特定的消息写评论
* @param msg
*
* @param content
*/
// 回复
addReply(msg: Msg, content: string): Reply {
// 创建回复
let reply = new Reply(this, content);
// 回复与消息关联
msg.replyList.push(reply);
return reply;
}
}
msg.ts
import User from "./user";
import Reply from "./reply";
export default class Msg {
/**
*
* 编号
*/
id:number;
// 消息的作者
author:User;
// 内容
content:string;
/**
* 所有的回复或者评论
*/
replyList:Array<Reply>;
/**
*
* @param content
*
* @param author
*/
constructor(content:string,author?:User){
this.author=author;
this.content=content;
this.replyList=new Array();
}
/**
*
* 重写
*/
toString():string{
return `msg{id:${this.id}, author:${this.author},content:${this.content},评论:${this.replyList}}`;
}
}
reply.ts
import User from "./user";
/**
* 状态的回复
*/
export default class Reply {
/**
*
*
*/
id:number;
// 回复是消息的一部分,消息包含了它,它不独立存在
// 所以不写msg:Msg;
/**
* 内容
*/
content:string;
/**
*
* 作者
*/
author:User;
constructor(author:User,content:string) {
this.author=author;
this.content=content;
}
}
app.ts
import User from "./user";
import Msg from "./msg";
class App {
static run(){
// 创建用户
let u1=new User(1,"alice");
let u2=new User(2,"bob");
let u3=new User(3,"jack");
let u4=new User(4,"rose");
// 彼此关注,取消关注
u1.addFriend(u4);
u2.addFriend(u4);
u3.addFriend(u4);
u4.addFriend(u1);
// (用户)发状态
// 1.
// u4.sendMsg(new Msg());
// 2.是否返回一个消息对象
let m1= u4.sendMsg("hello");
console.log(m1.toString());
let m2=u4.sendMsg("再见");
console.log(m2.toString());
console.log(u1.sendMsg("大家好!!!").toString());
u2.sendMsg("233");
u2.sendMsg("666");
u2.sendMsg("11");
u2.sendMsg("1");
// 显示用户信息
u1.showInfo();
u2.showInfo();
u3.showInfo();
u4.showInfo();
u1.removeFriend(u4);
u1.showInfo();
u2.showInfo();
u3.showInfo();
u4.showInfo();
// u2的消息
// for (const m of u2.msgs) {
// console.log(m.toString());
// }
// 函数的声明
let m9:Msg=u1.sendMsg("这是一条状态");
// 面向对象分析,设计,编程
// u2给m9写一个回复
let m9r1= u2.addReply(m9,"沙发");
let m9r2=u3.addReply(m9,"哈哈");
}
}
App.run();
代码运行结果为:
D:\TSCode\1>tsc app.ts
D:\TSCode\1>node app.js
msg{id:0.08835191018696431, author:[object Object],content:hello,评论:}
msg{id:0.7333447892424974, author:[object Object],content:再见,评论:}
msg{id:0.8182929141215902, author:[object Object],content:大家好!!!,评论:}
用户:alice
关注:1
粉丝:1
消息:1
----------------------------
用户:bob
关注:1
粉丝:0
消息:4
----------------------------
用户:jack
关注:1
粉丝:0
消息:0
----------------------------
用户:rose
关注:1
粉丝:3
消息:2
----------------------------
用户:alice
关注:0
粉丝:1
消息:1
----------------------------
用户:bob
关注:1
粉丝:0
消息:4
----------------------------
用户:jack
关注:1
粉丝:0
消息:0
----------------------------
用户:rose
关注:1
粉丝:2
消息:2
----------------------------