听说你用 java new 了一个女朋友
程序员文章站
2022-03-04 18:40:10
...
前言
看到网上,好多人在秀自己 new 的 girl !!
本文做一个小练习:
读取 json 文件,封装、继承、接口等java 基础的回顾!!
项目结构
pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.feng</groupId>
<artifactId>girl_friend_demo</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<java.version>1.8</java.version>
<maven.compiler.source>${java.version}</maven.compiler.source>
<maven.compiler.target>${java.version}</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.54</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.10</version>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>1.7.29</version>
</dependency>
</dependencies>
</project>
girl.json
{
"小美": {
"age": 25,
"height": 164.32,
"weight": 47.34,
"meetCounts": 7,
"birthday": "1997-12-22",
"beautiful": true,
"hobbies": "看,看电影,玩游戏",
"infos": "不喜欢吃辣"
}
}
log4j.properties
log4j.rootLogger=debug, console
log4j.appender.console=org.apache.log4j.ConsoleAppender
log4j.appender.console.layout=org.apache.log4j.PatternLayout
log4j.appender.console.layout.ConversionPattern=%d{HH:mm:ss,SSS} %-5p %-60c %x - %m%n
AbstractPeople
package org.feng.entity;
import lombok.Builder;
import lombok.Data;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.time.LocalDate;
import java.util.Set;
/**
* Created by Feng on 2020/2/14 12:30
* CurrentProject's name is girl_friend_demo
* 人的抽象:抽取公共属性
* @author Feng
*/
@Data
public abstract class AbstractPeople {
private static final Logger LOGGER = LoggerFactory.getLogger(AbstractPeople.class);
/**名字*/
private String name;
/**年龄*/
private Integer age;
/**身高*/
private Double height;
/**体重*/
private Double weight;
/**生日*/
private LocalDate birthday;
/**爱好*/
private String[] hobbies;
/**其他信息*/
private String[] infos;
/**认识的男生*/
protected Set<Boy> boys;
/**认识的女生*/
protected Set<Girl> girls;
public AbstractPeople(String name){
this.name = name;
}
/**打电话*/
public void call(AbstractPeople people){
// TODO 打电话的业务
LOGGER.info("[{}] 给 [{}] 打了一个电话!", this.name, people.getName());
}
/**闲逛*/
public abstract void hangout(AbstractPeople people);
/**交朋友*/
public void makeFriend(AbstractPeople people){
if(people instanceof Boy){
boys.add((Boy)people);
}
if(people instanceof Girl){
girls.add((Girl)people);
}
LOGGER.info("[{}] 和 [{}] 交朋友了!", this.name, people.getName());
}
/**约会*/
public abstract void haveMeet(AbstractPeople people);
}
Boy
package org.feng.entity;
import lombok.EqualsAndHashCode;
import lombok.Getter;
import lombok.Setter;
import java.io.Serializable;
import java.util.HashSet;
import java.util.TreeSet;
/**
* Created by Feng on 2020/2/14 12:30
* CurrentProject's name is girl_friend_demo
* @author Feng
*/
@EqualsAndHashCode(callSuper = true)
@Getter
@Setter
public class Boy extends AbstractPeople implements Serializable, Comparable<Boy> {
private static final long serialVersionUID = 3244522984736192414L;
/**颜值指数:越高越好*/
private Integer coolCounts = 0;
public Boy(String name) {
super(name);
girls = new TreeSet<>();
boys = new HashSet<>();
}
@Override
public int compareTo(Boy boy) {
return (int)((this.coolCounts + boy.getHeight() - boy.getAge()) - (boy.coolCounts + boy.getHeight() - boy.getAge()));
}
@Override
public void hangout(AbstractPeople people) {
}
@Override
public void haveMeet(AbstractPeople people) {
}
}
Girl
package org.feng.entity;
import lombok.EqualsAndHashCode;
import lombok.Getter;
import lombok.Setter;
import lombok.ToString;
import java.io.Serializable;
import java.util.HashSet;
import java.util.TreeSet;
/**
* Created by Feng on 2020/2/14 12:31
* CurrentProject's name is girl_friend_demo
* @author Feng
*/
@ToString(callSuper = true)
@EqualsAndHashCode(callSuper = true)
@Setter
@Getter
public class Girl extends AbstractPeople implements Serializable, Comparable<Girl> {
private static final long serialVersionUID = -2528133621953391589L;
/**
* 是否漂亮
*/
private Boolean beautiful = true;
/**
* 约会次数
*/
private Integer meetCounts = 0;
/**认识的男生*/
public Girl(String name) {
super(name);
boys = new TreeSet<>();
girls = new HashSet<>();
}
@Override
public int compareTo(Girl girl) {
return (int)((this.meetCounts + this.getHeight()) - (girl.meetCounts + girl.getHeight()));
}
@Override
public void hangout(AbstractPeople people) {
}
@Override
public void haveMeet(AbstractPeople people) {
}
}
Client
import com.alibaba.fastjson.JSONObject;
import org.feng.entity.Boy;
import org.feng.entity.Girl;
import java.io.*;
import java.nio.charset.StandardCharsets;
import java.time.LocalDate;
/**
* Created by Feng on 2020/2/14 13:12
* CurrentProject's name is girl_friend_demo
* @author Feng
*/
public class Client {
/**
* 初始化数据:读取 json
*/
private static JSONObject init(){
String json = readJsonFile("D:\\jee-2019-7-idea-maven-workspace\\girl_friend_demo\\src\\main\\resources" +
"\\girl.json");
return JSONObject.parseObject(json);
}
public static void main(String[] args) {
JSONObject jsonObject = init();
JSONObject tempObject = JSONObject.parseObject(jsonObject.get("小美").toString());
Boy boy = new Boy("你家宝宝");
Girl girl = new Girl("小美");
girl.setHeight(tempObject.getDouble("height"));
girl.setWeight(tempObject.getDouble("weight"));
girl.setAge(tempObject.getInteger("age"));
girl.setHobbies(tempObject.getString("hobbies").split(","));
girl.setBirthday(LocalDate.parse(tempObject.getString("birthday")));
girl.setBeautiful(tempObject.getBoolean("beautiful"));
girl.setMeetCounts(tempObject.getInteger("meetCounts"));
girl.setInfos(tempObject.getString("infos").split(","));
System.out.println(girl);
boy.makeFriend(girl);
boy.call(girl);
boy.haveMeet(girl);
boy.hangout(girl);
}
private static String readJsonFile(String fileName) {
String jsonStr;
try {
File jsonFile = new File(fileName);
FileReader fileReader;
fileReader = new FileReader(jsonFile);
Reader reader = new InputStreamReader(new FileInputStream(jsonFile), StandardCharsets.UTF_8);
int ch = 0;
StringBuilder sb = new StringBuilder();
while ((ch = reader.read()) != -1) {
sb.append((char) ch);
}
fileReader.close();
reader.close();
jsonStr = sb.toString();
return jsonStr;
} catch (IOException e) {
e.printStackTrace();
return null;
}
}
}
运行结果
Girl(super=AbstractPeople(name=小美, age=25, height=164.32, weight=47.34, birthday=1997-12-22, hobbies=[看, 看电影, 玩游戏], infos=[不喜欢吃辣], boys=[], girls=[]), beautiful=true, meetCounts=7)
14:10:42,505 INFO org.feng.entity.AbstractPeople - [你家宝宝] 和 [小美] 交朋友了!
14:10:42,506 INFO org.feng.entity.AbstractPeople - [你家宝宝] 给 [小美] 打了一个电话!
Process finished with exit code 0
这就 new 好了,虽然有点 Low,但是,咦,我想说啥来着!!!???
上一篇: idea连接mysql数据库报java.lang.RuntimeException: com.mysql.cj.exceptions.InvalidConnectionAttributeExcepti
推荐阅读