《面向对象上》单元练习
1. 编写一个学生类,提供name
,age
,gender
,phone
,address
,email
成员变量,且为每个成员变量提供setter
、getter
方法。为学生类提供默认的构造器和带所有成员变量的构造器。为学生类提供方法,用于描绘吃、喝、玩、睡等行为。
代码示例
package com.abc.part4.chapterexercises;
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("学生睡觉");
}
}
package com.abc.part4.chapterexercises;
public class StudentTest {
public static void main(String[] args) {
Student student = new Student("小花花", 21, '女', "1008611", "北京市海淀区", "[email protected]");
System.out.println("我叫" + student.getName() + ", 今年" + student.getAge() + ", 我的性别是" + student.getGender() + ", 我的电话是" + student.getPhone() + ", 我的居住地址是" + student.getAddress() + ", 我的邮箱是" + student.getEmail());
student.eat();
student.drink();
student.play();
student.sleep();
}
}
2. 利用第1题定义的Student
类,定义一个Student[]
数组保存多个Student
对象作为通讯录数据。程序可通过name
,email
,address
查询,如果找不到数据,则进行友好提示。
代码示例
package com.abc.part4.chapterexercises;
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("学生睡觉");
}
@Override
public String toString() {
return "Student{" + "name=" + name + ", age=" + age + ", gender=" + gender + ", phone=" + phone + ", address=" + address + ", email=" + email + "}";
}
}
package com.abc.part4.chapterexercises;
import java.util.Scanner;
public class StudentTest {
static Student[] arr = {new Student("小花花", 21, '女', "1008611", "北京市大兴区", "[email protected]"),
new Student("大花花", 25, '女', "1008612", "北京市海淀区", "[email protected]"),
new Student("小刚", 21, '男', "1008613", "北京市朝阳区", "[email protected]")};
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("输入数字序号有误!");
}
}
}
}
}
3. 定义交通工具、汽车、火车、飞机这些类,注意它们的继承关系,为这些类提供超过3个不同的构造器,并通过初始化块提取构造器中的通用代码。
代码示例
package com.abc.part4.chapterexercises;
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() + "米";
}
}
package com.abc.part4.chapterexercises;
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 + "上跑。";
}
}
package com.abc.part4.chapterexercises;
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 + "上跑。";
}
}
package com.abc.part4.chapterexercises;
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());
}
}