一、概述
1.目的
本文主要通过对博客信息的管理,展现java中对类的使用情况。
2.要求
通过面向对象以及类的使用,定义类对象实现:按照用户的需求实现查看各个内容的信息。
二、代码实现
1.Blogger(博主类)
创建Blogger类,并添加属性和Getter、Setter方法,添加一个show()方法用来展示博主的基本信息。
代码如下:
package work5_2022_09_20;
import java.text.SimpleDateFormat;
import java.util.Date;
public class Blogger {
private String bloggerId;//博主Id,唯一标识
private String userName;//博主用户名
private String password;//博主账户的密码
private String address;//博主地址
private String email;//博主邮箱
private Date daeTime;//博主注册日期
public String getPassword() {
return password;
}
public Blogger setPassword(String password) {
this.password = password;
return this;
}
public Date getDaeTime() {
return daeTime;
}
public Blogger setDaeTime(Date daeTime) {
this.daeTime = daeTime;
return this;
}
public String getBloggerId() {
return bloggerId;
}
public Blogger setBloggerId(String bloggerId) {
this.bloggerId = bloggerId;
return this;
}
public String getUserName() {
return userName;
}
public Blogger setUserName(String userName) {
this.userName = userName;
return this;
}
public String getAddress() {
return address;
}
public Blogger setAddress(String address) {
this.address = address;
return this;
}
public String getEmail() {
return email;
}
public Blogger setEmail(String email) {
this.email = email;
return this;
}
//博主个人基本信息展示
public void show() {
System.out.println("用户名:\t\t" + this.userName +
"\n博主个人地址:\t" + this.address +
"\n博主邮箱:\t" + this.email +
"\n注册日期:\t" + getDateTime());
}
private String getDateTime() {
SimpleDateFormat bartDateFormat = new SimpleDateFormat("EEEE-MMMM-dd-yyyy");
return bartDateFormat.format(this.daeTime);
}
}
在上述代码中,Setter方法的返回值设为了当前自定义类的类型,主要是为了方便属性注入时不用反复使用对象名.set方法,而是可以直接使用对象名.set方法1.set方法2,以此例推。注入Date属性时,Date的值为一个时间戳,需要通过getDateTime()里面的方法转换为正常的时间显示,如:星期二-九月-20-2022。
2.Article(文章类)
文章类相比于博主类多了两个自定义对象属性,分别为为Blogger和Module两个类,分别代表发表此博客文章的博主以及当前博客文章的所属模块类别。
代码如下:
package work5_2022_09_20;
import java.text.SimpleDateFormat;
import java.util.Date;
public class Article {
private String articleId;//文章Id,唯一标识
private Blogger blogger;//博客作者
private String articleName;//文章标题
private String commentCount;//文章内容
private Date dateTime;//发表时间
private Module moudule;//文章所属类别模块
public Blogger getBlogger() {
return blogger;
}
public Article setBlogger(Blogger blogger) {
this.blogger = blogger;
return this;
}
public String getArticleId() {
return articleId;
}
public Article setArticleId(String articleId) {
this.articleId = articleId;
return this;
}
public String getArticleName() {
return articleName;
}
public Article setArticleName(String articleName) {
this.articleName = articleName;
return this;
}
public String getCommentCount() {
return commentCount;
}
public Article setCommentCount(String commentCount) {
this.commentCount = commentCount;
return this;
}
public Date getDateTime() {
return dateTime;
}
public Article setDateTime(Date dateTime) {
this.dateTime = dateTime;
return this;
}
public Module getMoudule() {
return moudule;
}
public Article setMoudule(Module moudule) {
this.moudule = moudule;
return this;
}
private String getDateTimes() {
SimpleDateFormat bartDateFormat = new SimpleDateFormat("EEEE-MMMM-dd-yyyy");
return bartDateFormat.format(this.dateTime);
}
public void show() {
System.out.println( "文章标题:\t" + this.articleName
+ "\n文章作者:\t" + this.blogger.getUserName()
+ "\n文章发表时间:\t" + getDateTimes()
+ "\n文章内容:\t" + this.commentCount
+"\n文章所属模块:\t" + this.moudule.getModuleName());
}
}
3.Module(模块类)
代码如下:
package work5_2022_09_20;
public class Module {
private String moduleId;//模块Id
private String moduleName;//模块名称
private String moduleDepict;//模块描述
public String getModuleId() {
return moduleId;
}
public Module setModuleId(String moduleId) {
this.moduleId = moduleId;
return this;
}
public String getModuleName() {
return moduleName;
}
public Module setModuleName(String moduleName) {
this.moduleName = moduleName;
return this;
}
public String getModuleDepict() {
return moduleDepict;
}
public Module setModuleDepict(String moduleDepict) {
this.moduleDepict = moduleDepict;
return this;
}
public void show(){
System.out.println("模块的名称:\t" + this.moduleName + "\n模块描述:\t" + this.moduleDepict);
}
}
4.Comment(评论类)
代码如下:
package work5_2022_09_20;
import java.text.SimpleDateFormat;
import java.util.Date;
public class Comment {
private String commentId;//评论Id
private Article article;//评论文章
private String commentContent;//评论内容
private Date commentDateTime;//评论时间
private Blogger User;//评论用户
public Article getArticle() {
return article;
}
public Comment setArticle(Article article) {
this.article = article;
return this;
}
public String getCommentId() {
return commentId;
}
public Comment setCommentId(String commentId) {
this.commentId = commentId;
return this;
}
public String getCommentContent() {
return commentContent;
}
public Comment setCommentContent(String commentContent) {
this.commentContent = commentContent;
return this;
}
public Date getCommentDateTime() {
return commentDateTime;
}
public Comment setCommentDateTime(Date commentDateTime) {
this.commentDateTime = commentDateTime;
return this;
}
public Blogger getUser() {
return User;
}
public Comment setUser(Blogger user) {
User = user;
return this;
}
public void show() {
System.out.println("评论的文章标题:\t" + this.article.getArticleName()
+ "\n评论用户:\t" + this.User.getUserName()
+ "\n评论时间:\t" + getDateTime()
+ "\n评论内容:\t" + this.getCommentContent());
}
private String getDateTime() {
SimpleDateFormat bartDateFormat = new SimpleDateFormat("EEEE-MMMM-dd-yyyy");
return bartDateFormat.format(this.commentDateTime);
}
}
5.Test(测试类)
测试类中,分别创建Blogger、Article、Module以及Comment类的实例对象,并添加属性值。blogger对象表示注册的一个用户,module对象表示添加的一个分类模块,article对象表示当前注册的用户发表的一篇文章,其分类为module,comment表示当前blogger对象对文章对象article发表的评论。
代码如下:
package work5_2022_09_20;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Scanner;
public class Test {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
Blogger blogger = new Blogger();//创建一个博主的对象
//注册一个用户
blogger.setBloggerId("111111")
.setUserName("jay")
.setPassword("abc123456")
.setEmail("123456@qq.com")
.setAddress("湖南")
.setDaeTime(new Date());
//创建一个模块对象
Module module = new Module();
//添加一个java类的模块
module.setModuleId("1")
.setModuleName("java专栏")
.setModuleDepict("这里都是java的博客文章");
//发表一篇博客
Article article = new Article();
article.setArticleId("11111")
.setArticleName("java类的调用")
.setCommentCount("这是一篇关于java的文章")
.setDateTime(new Date())
.setMoudule(module)
.setBlogger(blogger);
//添加一条评论
Comment comment = new Comment();
comment.setCommentId("11")
.setArticle(article)
.setCommentContent("这是一条java评论")
.setUser(blogger)
.setCommentDateTime(new Date());
while(true) {
int choice = mean();//菜单选项展示,并获取选项
System.out.println("---------------------------------------------------------------\n");
switch (choice) {
case 1: module.show();break;
case 2: article.show();break;
case 3: blogger.show();break;
case 4: comment.show();break;
case 5: return;
default: System.out.println("输入错误!");break;
}
System.out.println("---------------------------------------------------------------\n");
System.out.print("返回上一层请安任意键返回,否则按n退出:");
String ch = input.next();
if(ch.equals("n")) {
return;
}
}
}
private static int mean() {
Scanner input = new Scanner(System.in);
System.out.println("*********************************这是分界线*********************************");
System.out.println("1.模块详情\t\n2.文章详情\t\n3.作者详情\t\n4.评论详情\t\n5.退出");
System.out.println("*********************************这是分界线*********************************");
System.out.print("请输入你的选择:");
int choice = input.nextInt();
return choice;
}
}