class inheritance in java

[Title description]:

a. Declare a Person class with name (String type), age (int type), and sex (char type) attributes.

      Assignment via constructor method. Each property has getter/setter methods.

      A show method, returning String type, the content is as follows:

         XX Male (Female) Age

b. Declare a Student class, inherit the Person class, add the id (int, student number) attribute, and use the constructor method to call the parent class constructor method for variable assignment. Each property has getter/setter methods. The show method of the Override parent class returns the String type, with the following content:

         XX Male (Female) Age Student Number

Tip: Use super to call the show method of the parent class to get the String except the student number part, and then add the student number information.

c . Declare a Teacher class, inherit from Person, add the course (String, taught course) attribute, and use the constructor method to call the parent class constructor method for variable assignment. Each property has getter/setter methods. The show method of the Override parent class returns the String type, with the following content:

      XX Male (Female) Age Courses Taught

Tip: Use super to call the show method of the parent class to get the String except the part of the course taught, and then add the information of the course taught.

d . Declare the PersonApp class, write a static method appMain method, declare variables of type Person, Student, and Teacher respectively in the method, initialize them through the construction method, and then display their respective information. The information is as follows [Note: The following display results

What follows Person is an English colon followed by a half-width space, not a Chinese colon;

The character after Student is an English colon followed by a half-width space, not a Chinese colon;

What follows Teacher is an English colon followed by a half-width space, not a Chinese colon]. The running results are as follows (the first 1 is input by the user and is used to test the appMain written by the user. For details, see the main method):

[Output sample]:

【code show as below】:

import java.util.Scanner;
//声明一个PersonApp类
public class PersonApp{
	public static void appMain(String[] args) {
		Person p=new Person("张淼",21,'男');
		Student s=new Student("高坤",22,'男',5001212);
		Teacher t=new Teacher("李芳香",35,'女',"程序设计基础");
		System.out.println("Person: "+p.show());//显示各自信息
		System.out.println("Student: "+s.show());
		System.out.println("Teacher: "+t.show());
	}
	public static void main(String[] args) {
		@SuppressWarnings("resource")
		Scanner sc = new Scanner(System.in);
		int func = sc.nextInt();
		switch (func) {
		case 1:
			appMain(null);
			break;
		case 2:
			personTest();
			break;
		case 3:
			studentTest();
			break;
		case 4:
			teacherTest();
			break;
      
		default:
			break;
		}
	}
	private static void teacherTest() {
		Teacher t = new Teacher("李芳香", 35, '女', "程序设计基础");
		test(t.getName().equals("李芳香") && t.getAge()==35
				&& t.getSex()=='女' && t.getCourse().equals("程序设计基础"),
				"Teacher构造函数正确","Teacher构造函数不正确");
		test(t.show().equals("李芳香 女 35 程序设计基础"),"teacher.show()通过","teacher.show()未通过");
	}

	private static void studentTest() {
		Student s = new Student("高坤", 22, '男', 5001212);
		test(s.getName().equals("高坤") && s.getAge()==22
				&& s.getSex()=='男' && s.getId()==5001212,
				"Student构造函数正确","Student构造函数不正确");
		test(s.show().equals("高坤 男 22 5001212"),"student.show()通过","student.show()未通过");
	}

	private static void personTest() {
		Person p = new Person("张淼", 21, '男');
		test(p.getName().equals("张淼")  && p.getAge()==21
				&& p.getSex()=='男', "Person构造函数正确","Person构造函数不正确");
		test(p.show().equals("张淼 男 21"),"person.show()通过","person.show()未通过");   
	}

	private static void test(boolean pass, String corr, String wrong) {
		if  (pass) {
			System.out.println(corr);
		} else {
			System.out.println(wrong);
		}  
	}
}

//声明一个Person类
class Person{ //有name age sex属性
	String name;
	int age;
	char sex;
	//通过构造方法进行赋值
	Person(String name,int age,char sex){
		this.name=name;
		this.age=age;
		this.sex=sex;
	}
	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 getSex() {
		return sex;
	}
	public void setSex(char sex) {
		this.sex=sex;
	}
	//一个show方法,返回String类型
	public String show() {
		return this.getName()+" "+this.getSex()+" "+this.getAge();
	}
}
//声明一个Student类继承Person,增加id属性
class Student extends Person{
	private int id;
	//通过构造方法,利用super调用父类构造方法来进行变量赋值
	public Student(String name,int age,char sex,int id) {
		super(name,age,sex);
		this.id=id;
	}
	public String getName() {
		return name;
	}
	public int getAge() {
		return age;
	}
	public int getId() {
		return id;
	}
	public char getSex() {
		return sex;
	}
	//Override父类的show方法
	public String show() {
		return super.show()+" "+this.getId();
	}
}
//声明一个teacher类继承Person,增加course属性
class Teacher extends Person{
	String course;
	//通过构造方法,利用super调用父类构造方法来进行变量赋值
	public Teacher(String name,int age,char sex,String course) {
		super(name,age,sex);
		this.course=course;
	}
	public String getName() {
		return name;
	}
	public int getAge() {
		return age;
	}
	public char getSex() {
		return sex;
	}
	public Object getCourse() {
		return course;
	}
	//Override父类的show方法
	public String show() {
		return super.show()+" "+this.getCourse();
	}
}

Guess you like

Origin blog.csdn.net/ZQY211210400628/article/details/129779139