Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Answer #1

Open
wants to merge 11 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
.idea/**/workspace.xml
.idea/**/tasks.xml
.idea/dictionaries
.idea

# Sensitive or high-churn files:
.idea/**/dataSources/
Expand All @@ -31,6 +32,8 @@ cmake-build-debug/

## File-based project format:
*.iws
*.iml
*.ipr

## Plugin-specific files:

Expand Down
15 changes: 15 additions & 0 deletions ANSWER.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# Java面向对象编程

### 如何下载
+ 答案详情请点击上方 作业答案下载,来下载 `answer.zip` 压缩包文件

### 如何启动
+ 解压 `answer.zip`,用 IntelliJ IDE 打开
+ 运行测试
根目录下执行`./gradlew clean test`

### 重点讲解
+ 仔细阅读 `test` 文件夹里的每个题目的README.md,确定题目需求

### 注意事项
+ 建议每个题目按顺序来做,它们之间是递进关系
48 changes: 0 additions & 48 deletions README.md

This file was deleted.

12 changes: 0 additions & 12 deletions oo-basic-step-by-step-java.iml

This file was deleted.

19 changes: 0 additions & 19 deletions oo-basic-step-by-step-java_main.iml

This file was deleted.

26 changes: 0 additions & 26 deletions oo-basic-step-by-step-java_test.iml

This file was deleted.

3 changes: 3 additions & 0 deletions script.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#!/usr/bin/env bash

./gradlew test
8 changes: 0 additions & 8 deletions src/main/java/Library.java

This file was deleted.

8 changes: 3 additions & 5 deletions src/main/java/practice10/Teacher.java
Original file line number Diff line number Diff line change
@@ -1,20 +1,18 @@
package practice10;

import java.util.Collection;
import java.util.Collections;
import java.util.stream.Collectors;
import java.util.LinkedList;

import static java.util.stream.Collectors.joining;

public class Teacher extends Person {
private Collection<Klass> classes;
private LinkedList<Klass> classes = new LinkedList<Klass>();

public Teacher(int id, String name, int age) {
super(id, name, age);
this.classes = Collections.emptySet();
}

public Teacher(int id, String name, int age, Collection<Klass> classes) {
public Teacher(int id, String name, int age, LinkedList<Klass> classes) {
super(id, name, age);
this.classes = classes;
}
Expand Down
8 changes: 4 additions & 4 deletions src/main/java/practice11/Teacher.java
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
package practice11;

import java.util.Collection;
import java.util.Collections;
import java.util.LinkedList;

import static java.util.stream.Collectors.joining;

public class Teacher extends Person implements JoinListener, AssignLeaderListener {
private Collection<Klass> classes;
private LinkedList<Klass> classes = new LinkedList<Klass>();


public Teacher(int id, String name, int age) {
super(id, name, age);
this.classes = Collections.emptySet();
}

public Teacher(int id, String name, int age, Collection<Klass> classes) {
public Teacher(int id, String name, int age, LinkedList<Klass> classes) {
super(id, name, age);
this.classes = classes;
this.classes.forEach(c -> {
Expand Down
12 changes: 0 additions & 12 deletions src/test/java/LibraryTest.java

This file was deleted.

2 changes: 1 addition & 1 deletion src/test/java/practice02/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ introduce方法返回一个字符串形如:

>My name is Tom. I am 21 years old.

再写一个Student类继承Person类,除了name,age属性,还有要有class属性。也有一个introduce方法,
再写一个Student类继承Person类,除了name,age属性,还有要有klass属性。也有一个introduce方法,
introduce方法返回一个字符串形如:

>I am a Student. I am at Class 2.
Expand Down
2 changes: 1 addition & 1 deletion src/test/java/practice03/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ introduce方法返回一个字符串形如:

>My name is Tom. I am 21 years old.

再写一个Student类继承Person类,除了name,age属性,还有要有class属性。也有一个introduce方法,
再写一个Student类继承Person类,除了name,age属性,还有要有klass属性。也有一个introduce方法,
introduce方法返回一个字符串形如:

>I am a Student. I am at Class 2.
Expand Down
2 changes: 1 addition & 1 deletion src/test/java/practice04/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ introduce方法返回一个字符串形如:

>My name is Tom. I am 21 years old.

再写一个Student类继承Person类,除了name,age属性,还有要有class属性。也有一个introduce方法,
再写一个Student类继承Person类,除了name,age属性,还有要有klass属性。也有一个introduce方法,
introduce方法返回一个字符串形如:

>My name is Tom. I am 21 years old. I am a Student. I am at Class 2.
Expand Down
2 changes: 1 addition & 1 deletion src/test/java/practice05/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ introduce方法返回一个字符串形如:

>My name is Tom. I am 21 years old.

再写一个Student类继承Person类,除了name,age属性,还有要有class属性。也有一个introduce方法,
再写一个Student类继承Person类,除了name,age属性,还有要有klass属性。也有一个introduce方法,
introduce方法返回一个字符串形如:

>My name is Tom. I am 21 years old. I am a Student. I am at Class 2.
Expand Down
6 changes: 3 additions & 3 deletions src/test/java/practice06/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,17 @@ introduce方法返回一个字符串形如:

>My name is Tom. I am 21 years old.

再写一个Student类继承Person类,除了name,age属性,还有要有class属性。也有一个introduce方法,
再写一个Student类继承Person类,除了name,age属性,还有要有klass属性。也有一个introduce方法,
introduce方法返回一个字符串形如:

>My name is Tom. I am 21 years old. I am a Student. I am at Class 2.

再写一个Teacher类继承Person类,除了name,age属性,也有class属性。也有一个introduce方法,
再写一个Teacher类继承Person类,除了name,age属性,也有klass属性。也有一个introduce方法,
introduce方法返回一个字符串形如:

>My name is Tom. I am 21 years old. I am a Teacher. I teach Class 2.

如果class为空,就会返回:
如果klass为空,就会返回:

>My name is Tom. I am 21 years old. I am a Teacher. I teach No Class.

Expand Down
8 changes: 4 additions & 4 deletions src/test/java/practice07/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,19 @@ introduce方法返回一个字符串形如:

>My name is Tom. I am 21 years old.

再写一个Student类继承Person类,除了name,age属性,还有要有class属性。也有一个introduce方法,
再写一个Student类继承Person类,除了name,age属性,还有要有klass属性。也有一个introduce方法,
introduce方法返回一个字符串形如:

>My name is Tom. I am 21 years old. I am a Student. I am at Class 2.

但是Student的class属性不是一个数字,而是一个对象,写一个Class类,有number属性。Student构造的时候把Class的一个实例传给Student,参见测试用例。
但是Student的klass属性不是一个数字,而是一个对象,写一个Klass类,有number属性。Student构造的时候把Klass的一个实例传给Student,参见测试用例。

再写一个Teacher类继承Person类,除了name,age属性,还有class属性。也有一个introduce方法,
再写一个Teacher类继承Person类,除了name,age属性,还有klass属性。也有一个introduce方法,
introduce方法返回一个字符串形如:

>My name is Tom. I am 21 years old. I am a Teacher. I teach Class 2.

如果class为空,就会返回:
如果klass为空,就会返回:

>My name is Tom. I am 21 years old. I am a Teacher. I teach No Class.

Expand Down
14 changes: 7 additions & 7 deletions src/test/java/practice08/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,27 +3,27 @@ introduce方法返回一个字符串形如:

>My name is Tom. I am 21 years old.

再写一个Student类继承Person类,除了id,name,age属性,还有要有class属性。也有一个introduce方法,
再写一个Student类继承Person类,除了id,name,age属性,还有要有Klass属性。也有一个introduce方法,
introduce方法返回一个字符串形如:

>My name is Tom. I am 21 years old. I am a Student. I am at Class 2.

但是Student的class属性不是一个数字,而是一个对象,写一个Class类,有number属性还有一个leader属性。但是leader属性不在构造器里。
但是Student的Klass属性不是一个数字,而是一个对象,写一个Klass类,有number属性还有一个leader属性。但是leader属性不在构造器里。

Student构造的时候把Class的一个实例传给Student,参见测试用例。
Class有一个assignLeader方法,接收一个Student实例。意为将一名学生设置为该Class的班长
如果Class的Leader是Tom,那么Tom调用introduce的方法就要返回:
Student构造的时候把Klass的一个实例传给Student,参见测试用例。
Klass有一个assignLeader方法,接收一个Student实例。意为将一名学生设置为该Klass的班长
如果Klass的Leader是Tom,那么Tom调用introduce的方法就要返回:

>My name is Tom. I am 21 years old. I am a Student. I am Leader of Class 2.

如果没有就继续返回旧的字符串。

再写一个Teacher类继承Person类,除了id,name,age属性,也有class属性。也有一个introduce方法,
再写一个Teacher类继承Person类,除了id,name,age属性,也有Klass属性。也有一个introduce方法,
introduce方法返回一个字符串形如:

>My name is Tom. I am 21 years old. I am a Teacher. I teach Class 2.

如果class为空,就会返回:
如果Klass为空,就会返回:

>My name is Tom. I am 21 years old. I am a Teacher. I teach No Class.

Expand Down
16 changes: 8 additions & 8 deletions src/test/java/practice09/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,34 +3,34 @@ introduce方法返回一个字符串形如:

>My name is Tom. I am 21 years old.

再写一个Student类继承Person类,除了id,name,age属性,还有要有class属性。也有一个introduce方法,
再写一个Student类继承Person类,除了id,name,age属性,还有要有Klass属性。也有一个introduce方法,
introduce方法返回一个字符串形如:

>My name is Tom. I am 21 years old. I am a Student. I am at Class 2.

但是Student的class属性不是一个数字,而是一个对象,写一个Class类,有number属性还有一个leader属性。但是leader属性不在构造器里。
但是Student的Klass属性不是一个数字,而是一个对象,写一个Klass类,有number属性还有一个leader属性。但是leader属性不在构造器里。

Student构造的时候把Class的一个实例传给Student,参见测试用例。
Class有一个assignLeader方法,接收一个Student实例。意为将一名学生设置为该Class的班长
如果Class的Leader是Tom,那么Tom调用introduce的方法就要返回:
Student构造的时候把Klass的一个实例传给Student,参见测试用例。
Klass有一个assignLeader方法,接收一个Student实例。意为将一名学生设置为该Klass的班长
如果Klass的Leader是Tom,那么Tom调用introduce的方法就要返回:

>My name is Tom. I am 21 years old. I am a Student. I am Leader of Class 2.

如果没有就继续返回旧的字符串。

Class还有一个appendMember方法,接受一个Student实例。意味将一名学生加入该班级。
Klass还有一个appendMember方法,接受一个Student实例。意味将一名学生加入该班级。
如果学生没有加入该班级,那么在调用assignLeader方法的时候,不会assign成功,会打印一句话:

>It is not one of us.

相应的调用Student的introduce方法也只会返回旧的字符串。

再写一个Teacher类继承Person类,除了id,name,age属性,也有class属性。也有一个introduce方法,
再写一个Teacher类继承Person类,除了id,name,age属性,也有Klass属性。也有一个introduce方法,
introduce方法返回一个字符串形如:

>My name is Tom. I am 21 years old. I am a Teacher. I teach Class 2.

如果class为空,就会返回:
如果Klass为空,就会返回:

>My name is Tom. I am 21 years old. I am a Teacher. I teach No Class.

Expand Down
Loading