定义抽象类Person、派生类Student和类Teacher--java代码(抽象类)

一、要求

设计抽象类Person,派生出具体类:学生类Student和教师类Teacher,创建若干不同类对象后并在主方法中测试。
数据成员定义:
Person [ID,姓名,生日]
Student [专业,成绩]
Teacher [职称,工资]
带参构造方法分别为:
Person(int id,String name, int bir)
Student(int id,String name, int bir, String major,double score)
Teacher(int id,String name, int bir, String title, double salary)
toString方法(Eclipse自动生成)

输入格式:

第一行整数n表示有n个对象,每个对象占2行,第一行为数字0(表示学生)或1(表示教师),第二行为生成对象的参数。

输出格式:

按行输出具体对象的信息。

二、代码

import java.util.Scanner;
public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        Student st;
        Teacher te;
        for (int i = 0; i < n; i++){

            int no = sc.nextInt();

            if(no == 0){
                st = new Student(sc.nextInt(), sc.next(), sc.nextInt(), sc.next(), sc.nextDouble());

                System.out.println(st.toString());

            }else if(no == 1){
                te = new Teacher(sc.nextInt(),sc.next(),sc.nextInt(), sc.next(), sc.nextDouble());
                System.out.println(te.toString());
            }
        }
        sc.close();
    }
}

abstract class person{
    int id;
    String name;
    int bir;
        
    public person(int id, String name, int bir) {
        this.id = id;
        this.name = name;
        this.bir = bir;
    }
    
}

class Teacher extends person{
    String title;
    double salary;
    
    
    public Teacher(int id, String name, int bir, String title, double salary) {
        super(id, name, bir);
        this.title = title;
        this.salary = salary;
    }

    @Override
    public String toString() {
        return "Teacher [id=" + id + ", name=" + name + ", bir=" + bir + ", title=" + title + ", salary=" + salary
                + "]";
    }

}

class Student extends person{
    String major;
    double score;
    
    public Student(int id, String name, int bir, String major, double score) {
        super(id, name, bir);
        this.major = major;
        this.score = score;
    }

    @Override
    public String toString() {
        return "Student [id=" + id + ", name=" + name + ", bir=" + bir + ", major=" + major + ", score=" + score + "]";
    }
    
}

三、测试数据

输入:

5
0
10101 Peter 20010121 Computer 87.5
1
20110014 Crystal 19900103 AI 7000
0
10102 Rose 20010715 E-Commerce 90
1
20120019 David 19781218 Prof 12000
0
10103 Semon 20000405 Computer 88

输出:

Student [id=10101, name=Peter, bir=20010121, major=Computer, score=87.5]
Teacher [id=20110014, name=Crystal, bir=19900103, title=AI, salary=7000.0]
Student [id=10102, name=Rose, bir=20010715, major=E-Commerce, score=90.0]
Teacher [id=20120019, name=David, bir=19781218, title=Prof, salary=12000.0]
Student [id=10103, name=Semon, bir=20000405, major=Computer, score=88.0]

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

motu2

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值