面向对象之单元测试练习题
# 《面向对象上》单元练习
# 1. 编写一个学生类,提供name
,age
,gender
,phone
,address
,email
成员变量,且为每个成员变量提供setter
、getter
方法。为学生类提供默认的构造器和带所有成员变量的构造器。为学生类提供方法,用于描绘吃、喝、玩、睡等行为。
# 代码示例
package com.abc.part4.chapterexercises;
/**
* @author mi
*/
public class Student {
private String name;
private int age;
private char gender;
private String phone;
private String address;
private String email;
public Student() {
System.out.println("学生类默认无参构造器");
}
public Student(String name, int age, char gender, String phone, String address, String email) {
this.name = name;
this.age = age;
this.gender = gender;
this.phone = phone;
this.address = address;
this.email = email;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public char getGender() {
return gender;
}
public void setGender(char gender) {
this.gender = gender;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public void eat() {
System.out.println("学生吃饭");
}
public void play() {
System.out.println("学生玩耍");
}
public void drink() {
System.out.println("学生喝水");
}
public void sleep() {
System.out.println("学生睡觉");
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
package com.abc.part4.chapterexercises;
/**
* @author mi
*/
public class StudentTest {
public static void main(String[] args) {
Student student = new Student("小花花", 21, '女', "1008611", "北京市海淀区", "10086@qq.com");
//我叫小花花, 今年21, 我的性别是女, 我的电话是1008611, 我的居住地址是北京市海淀区, 我的邮箱是10086@qq.com
System.out.println("我叫" + student.getName() + ", 今年" + student.getAge() + ", 我的性别是" + student.getGender() + ", 我的电话是" + student.getPhone() + ", 我的居住地址是" + student.getAddress() + ", 我的邮箱是" + student.getEmail());
/**
* 学生吃饭
* 学生喝水
* 学生玩耍
* 学生睡觉
*/
student.eat();
student.drink();
student.play();
student.sleep();
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# 2. 利用第1题定义的Student
类,定义一个Student[]
数组保存多个Student
对象作为通讯录数据。程序可通过name
,email
,address
查询,如果找不到数据,则进行友好提示。
# 代码示例
package com.abc.part4.chapterexercises;
/**
* @author mi
*/
public class Student {
private String name;
private int age;
private char gender;
private String phone;
private String address;
private String email;
public Student() {
System.out.println("学生类默认无参构造器");
}
public Student(String name, int age, char gender, String phone, String address, String email) {
this.name = name;
this.age = age;
this.gender = gender;
this.phone = phone;
this.address = address;
this.email = email;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public char getGender() {
return gender;
}
public void setGender(char gender) {
this.gender = gender;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public void eat() {
System.out.println("学生吃饭");
}
public void play() {
System.out.println("学生玩耍");
}
public void drink() {
System.out.println("学生喝水");
}
public void sleep() {
System.out.println("学生睡觉");
}
/**
* 重写toString()方法
*
* @return 对象的所有属性
*/
@Override
public String toString() {
return "Student{" + "name=" + name + ", age=" + age + ", gender=" + gender + ", phone=" + phone + ", address=" + address + ", email=" + email + "}";
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
package com.abc.part4.chapterexercises;
import java.util.Scanner;
/**
* @author mi
*/
public class StudentTest {
static Student[] arr = {new Student("小花花", 21, '女', "1008611", "北京市大兴区", "1008611@qq.com"),
new Student("大花花", 25, '女', "1008612", "北京市海淀区", "1008612@qq.com"),
new Student("小刚", 21, '男', "1008613", "北京市朝阳区", "1008613@qq.com")};
static Scanner input = new Scanner(System.in);
static String name = "name";
static String email = "email";
static String address = "address";
public static String select(String type) {
System.out.println("请输入您要查询的" + type);
String data = input.nextLine();
String noneData = "无法查询到关于" + data + "的相关通讯录信息。";
String dataReturn = null;
for (Student stu : arr) {
if (type.equals(name)) {
if (data.equals(stu.getName())) {
dataReturn = stu.toString();
break;
} else {
dataReturn = noneData;
}
} else if (type.equals(email)) {
if (data.equals(stu.getEmail())) {
dataReturn = stu.toString();
break;
} else {
dataReturn = noneData;
}
} else if (type.equals(address)) {
if (data.equals(stu.getAddress())) {
dataReturn = stu.toString();
break;
} else {
dataReturn = noneData;
}
}
}
return dataReturn;
}
public static void main(String[] args) {
while (true) {
System.out.println("***********************分割线***********************\n请输入您要查询的人,可以键入以下数字序号进行查询\n1.name\n2.email\n3.address");
String strNum = input.nextLine();
int num = Integer.parseInt(strNum);
switch (num) {
case 1: {
System.out.println(select("name"));
break;
}
case 2: {
System.out.println(select("email"));
break;
}
case 3: {
System.out.println(select("address"));
break;
}
default: {
System.out.println("输入数字序号有误!");
}
}
}
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# 3. 定义交通工具、汽车、火车、飞机这些类,注意它们的继承关系,为这些类提供超过3个不同的构造器,并通过初始化块提取构造器中的通用代码。
# 代码示例
package com.abc.part4.chapterexercises;
/**
* @Auther: ABC
* @Date: 2020/5/16 12:06
* @Description:
*/
public class Transportation {
private String transportationType;
private int transportationLength;
private String transportationColor;
static {
System.out.println("我是交通工具,作用是方便人们的出行。");
}
public Transportation(String transportationType, String transportationColor, int transportationLength) {
this.transportationType = transportationType;
this.transportationColor = transportationColor;
this.transportationLength = transportationLength;
}
public String getTransportationType() {
return transportationType;
}
public void setTransportationType(String transportationType) {
this.transportationType = transportationType;
}
public int getTransportationLength() {
return transportationLength;
}
public void setTransportationLength(int transportationLength) {
this.transportationLength = transportationLength;
}
public String getTransportationColor() {
return transportationColor;
}
public void setTransportationColor(String transportationColor) {
this.transportationColor = transportationColor;
}
@Override
public String toString() {
return "我是" + this.getTransportationType() + ", 我的颜色是" + this.getTransportationColor() + ", 我的长度是" +
this.getTransportationLength() + "米";
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
package com.abc.part4.chapterexercises;
/**
* @Auther: ABC
* @Date: 2020/5/16 12:17
* @Description:
*/
public class Car extends Transportation {
private String runInHighway;
public String getRunInHighway() {
return runInHighway;
}
public void setRunInHighway(String runInHighway) {
this.runInHighway = runInHighway;
}
public Car(String transportationType, String transportationColor, int transportationLength, String runInHighway) {
super(transportationType, transportationColor, transportationLength);
this.runInHighway = runInHighway;
}
@Override
public String toString() {
return super.toString() + ", 我可以在" + runInHighway + "上跑。";
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
package com.abc.part4.chapterexercises;
/**
* @Auther: ABC
* @Date: 2020/5/16 12:31
* @Description:
*/
public class Train extends Transportation {
private String runInHighway;
public String getRunInHighway() {
return runInHighway;
}
public void setRunInHighway(String runInHighway) {
this.runInHighway = runInHighway;
}
public Train(String transportationType, String transportationColor, int transportationLength, String runInHighway) {
super(transportationType, transportationColor, transportationLength);
this.runInHighway = runInHighway;
}
@Override
public String toString() {
return super.toString() + ", 我可以在" + runInHighway + "上跑。";
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
package com.abc.part4.chapterexercises;
/**
* @Auther: ABC
* @Date: 2020/5/16 12:34
* @Description:
*/
public class TransportationTest {
public static void main(String[] args) {
Car car = new Car("汽车", "白色", 5, "公路");
System.out.println(car.toString());
Train train = new Train("火车", "黑色", 1000, "铁轨");
System.out.println(train.toString());
/**
* 输出:
* 我是交通工具,作用是方便人们的出行。
* 我是汽车, 我的颜色是白色, 我的长度是5米, 我可以在公路上跑。
* 我是火车, 我的颜色是黑色, 我的长度是1000米, 我可以在铁轨上跑。
*/
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
编辑 (opens new window)
上次更新: 2021/08/10, 00:23:06
- 01
- Python实现对字符串的加解密02-25
- 02
- Python3对大文件中指定字符进行排序再写入到新的文件10-24
- 03
- Ubuntu下配置adb环境连接Android设备进行调试08-17