JavaSE基础知识分享(八)
cnblogs 2024-08-17 08:09:00 阅读 83
今天给大家发一下上期在文末题目的参考答案!
写在前面
前面讲的是java中集合这部分的内容,今天给大家发一个上期题目参考答案!
Person类:
<code>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 + '\'' +code>
", age=" + age +
", gender='" + gender + '\'' +code>
'}';
}
@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() + '\'' +code>
", age=" + getAge() +
", gender='" + getGender() + '\'' +code>
", studentId='" + studentId + '\'' +code>
'}';
}
@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();
}
好了,今天的分享就结束了。答案也是仅供参考,不代表最终答案,如果有错误或更简单的方法,欢迎在下面留言!!
声明
本文内容仅代表作者观点,或转载于其他网站,本站不以此文作为商业用途
如有涉及侵权,请联系本站进行删除
转载本站原创文章,请注明来源及作者。