寫在前面
前面講的是java中集合這部分的內容,今天給大家發一個上期題目參考答案!
Person類:
package com.shujia.TiMu_1000.ten2.Ti15;
/**
* @author cjy
* @create 2024-08-07-20:47
*/
public abstract class Person {
private String name;
private int age;
private String gender;
public Person() {
}
public Person(String name, int age, String gender) {
this.name = name;
this.age = setAge(age);
this.gender = gender;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
private int setAge(int age) {
if (age < 0) {
System.out.println("年齡輸入錯誤,預設為0");
return 0;
}
return age;
}
public String getGender() {
return gender;
}
public void setGender(String gender) {
this.gender = gender;
}
@Override
public String toString() {
return "Person{" +
"name='" + name + '\'' +
", age=" + age +
", gender='" + gender + '\'' +
'}';
}
@Override
protected void finalize() throws Throwable {
try {
System.out.println("物件 " + name + " 正在被銷燬");
} finally {
super.finalize();
}
}
public void increaseAge(int age) {
if (age > 0 && setAge(this.age) != 0) {
this.age += age;
} else {
System.out.println("你增加的年齡為負數,不能增加!");
}
}
public abstract String speak();
}
School類:
package com.shujia.TiMu_1000.ten2.Ti15;
/**
* @author cjy
* @create 2024-08-07-21:05
*/
public class School {
public Person[] people;
public School() {
}
public School(int size) {
people = new Person[size];
}
public void print(int index) {
if (index >= 0 && index < people.length && people[index] != null) {
System.out.println(people[index].speak());
} else {
System.out.println("索引超出範圍或物件為空");
}
}
}
Student類:
package com.shujia.TiMu_1000.ten2.Ti15;
/**
* @author cjy
* @create 2024-08-07-20:58
*/
public class Student extends Person{
private String studentId;
public Student() {
}
public Student(String name, int age, String gender, String studentId) {
super(name, age, gender);
this.studentId = studentId;
}
public String getStudentId() {
return studentId;
}
public void setStudentId(String studentId) {
this.studentId = studentId;
}
@Override
public String toString() {
return "Student{" +
"name='" + getName() + '\'' +
", age=" + getAge() +
", gender='" + getGender() + '\'' +
", studentId='" + studentId + '\'' +
'}';
}
@Override
public String speak() {
return getName()+"正在學高數!!";
}
}
Teacher類:
package com.shujia.TiMu_1000.ten2.Ti15;
/**
* @author cjy
* @create 2024-08-07-21:04
*/
public class Teacher extends Person implements Smoking{
public Teacher() {
}
public Teacher(String name, int age, String gender) {
super(name, age, gender);
}
@Override
public String speak() {
return getName()+"正在教高數!!";
}
@Override
public void smoking() {
System.out.println(getName()+"正在吸菸");
}
public void preparing(){
System.out.println(getName()+"正在備課");
}
}
Test類:
package com.shujia.TiMu_1000.ten2.Ti15;
/**
* @author cjy
* @create 2024-08-07-21:01
*/
public class Test {
public static void main(String[] args) {
// Person p4 = new Person("小明",21,"男");
// System.out.println(p4.toString());
// Person p4 = new Person("小明", 20, "男");
// System.out.println(p4.toString());
// System.out.println("================================");
// p4.increaseAge(2);
// System.out.println(p4.toString());
// Student p1 =new Student("李子傑",23,"男","1001");
// System.out.println(p1.toString());
Student p1 =new Student("李子傑",23,"男","1001");
Student p2 =new Student("尤海濤",22,"男","1002");
Teacher p3 =new Teacher("黃崇濤",21,"男");
School s1 = new School(3);
s1.people[0] = p1;
s1.people[1] = p2;
s1.people[2] = p3;
System.out.println(p1.speak());
System.out.println(p2.speak());
System.out.println(p3.speak());
Teacher t1 = (Teacher) s1.people[2];
t1.preparing();
//
// p3.smoking();
}
}
抽菸介面:
package com.shujia.TiMu_1000.ten2.Ti15;
/**
* @author cjy
* @create 2024-08-08-20:15
*/
public interface Smoking {
abstract void smoking();
}
好了,今天的分享就結束了。答案也是僅供參考,不代表最終答案,如果有錯誤或更簡單的方法,歡迎在下面留言!!