java个人微博东雷_用Java面向对象思想实现一个微博的功能(未完)

大概功能:

1.用户,用户有关注的朋友,被关注的是粉丝,关注和被关注有交互。

2.发送状态消息的功能,状态消息有回复功能

3.消息,回复,关注,粉丝的打印

4............等

第一次真正的感受都面对对象编程的魅力,对This引用有了进一步的认识,想到以前学习C++时的面对对象,C++中里面也有个个This指针,Java真的是继承了C++的诸多优点。

class User {

//成员变量

String name;

String password;

Msg[] msgList;

Reply[] replyList;

int msgSize = 0;

User[] friendsList;

int friendNumber;

User[] fansList;

int fansNumber = 0;

//构造方法

User(String name) {

this.name = name;

password = "123456";

msgList = new Msg[10];

replyList = new Reply[10];

friendsList = new User[10];

fansList = new User[10];

}

//成员函数

void sendMsg(String content) {

//这里的this是当前对象的,谁调用了这个方法

Msg msg = new Msg(content,this);//User u1,u1.sendMsg,则当前对象就是u1,this这里等于u1.name。

msgList[msgSize] = msg;

msgSize++;

}

void displayMsgList() {

for(int i = msgSize - 1; i >= 0; i--) {

Msg m = msgList[i];

m.display();

}

}

void display() {

System.out.println(this.name);

}

void addFriend(User u) {

friendsList[friendNumber++] = u;

u.fansList[u.fansNumber++] = this;//this 就是一个对象,类似User u1的u1

}

void displayFriends() {

System.out.printf("%s关注的朋友有\n",this.name);

for(int i = 0; i < friendNumber;i++) {

User uu = friendsList[i];

uu.display();

}

System.out.println();

}

void displayFans() {

System.out.printf("%s的粉丝有\n",this.name);

for(int i = 0; i < fansNumber; i++) {

User uu = fansList[i];

uu.display();

}

System.out.println();

}

}

class Msg {

String content;

User author;

Reply[] replyList;

int replySize;

Msg(String content,User author) {

this.content = content;

this.author = author;

replyList = new Reply[10];

replySize = 0;

}

void display() {

System.out.printf("作者: %s\t, 消息: %s\n",author.name,content);

}

}

class Reply {

String content;

User author;

Msg msg;

}

class App_weibo {

public static void main(String[] args) {

User u1 = new User("Bob");

User u2 = new User("Alice");

User u3 = new User("路人甲");

User u4 = new User("Jack");

User u5 = new User("Jim");

u1.sendMsg("今天晚上有cf");

u1.sendMsg("cf很闲的");

u2.sendMsg("草泥马,凌晨叫晚上?");

u2.sendMsg("我就是听了你的话才变成秃头的!");

u3.sendMsg("大...大佬?!....萌新瑟瑟发抖");

System.out.println(u1.msgSize);

System.out.println(u2.msgSize);

System.out.println(u3.msgSize);

System.out.println();

u1.displayMsgList();

u2.displayMsgList();

u3.displayMsgList();

System.out.println();

u1.addFriend(u2);

u1.addFriend(u3);

u2.addFriend(u4);

u2.addFriend(u5);

u3.addFriend(u5);

u1.displayFriends();

u3.displayFans();

u3.displayFriends();

u5.displayFans();

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值