zoukankan      html  css  js  c++  java
  • 以人类 Person 为基类设计学生类 Student 和教师类 Teacher

    学习内容:实验二以人类 Person 为基类设计学生类 Student 和教师类 Teacher

    示例代码:

    package 实验二;

    import java.util.Scanner;

    class Person{
    private String no;
    private String name;
    public Person(String no,String name) {
    this.no=no;
    this.name=name;
    System.out.println("Person Constructorrun");
    }
    public void setNo(String no) {
    this.no=no;
    }
    public String getNo() {
    return no;
    }
    public void setName(String name) {
    this.name=name;
    }
    public String getName() {
    return name;
    }
    public void show() {
    System.out.println("No="+no+",Name="+name);
    }
    }
    class Student extends Person{

    private String no;
    private String name;
    private String sNo;
    private String className;
    private double score;
    public Student(String no, String name,String sNo,String className,double score) {
    super(no,name);
    this.no=no;
    this.name=name;
    this.sNo=sNo;
    this.className=className;
    this.score=score;
    System.out.println("Student Constructor run");
    }
    public void setSNo(String sNo) {
    this.sNo=sNo;
    }
    public String getSNo() {
    return sNo;
    }
    public void setClassName(String className) {
    this.className=className;
    }
    public String getClassName() {
    return className;
    }
    public void setScore(double score) {
    this.score=score;
    }
    public double getScore() {
    return score;
    }
    public void show() {
    System.out.println("No="+no+",Name="+name);
    System.out.println("SNo="+ sNo+",ClassName="+ className+",Score="+score);
    }
    }
    public class Teacher extends Person {

    private String no;
    private String name;
    private String tNo;
    private String departmentName;
    private double wages;
    public Teacher(String no,String name,String tNo,String departmentName,double wages) {
    super(no, name);
    this.no=no;
    this.name=name;
    this.tNo=tNo;
    this.departmentName=departmentName;
    this.wages=wages;
    System.out.println("Teacher Constructor run");
    }
    public void setTNo(String tNo){
    this.tNo=tNo;
    }
    public String getTNo() {
    return tNo;
    }
    public void setDepartmentName(String departmentName) {
    this.departmentName=departmentName;
    }
    public String getDepartmentName() {
    return departmentName;
    }
    public void setWages(double wages) {
    this.wages=wages;
    }
    public double getWages() {
    return wages;
    }
    public void show() {
    System.out.println("No="+no+",Name="+name);
    System.out.println("TNo="+ tNo+",DepartmentName="+ departmentName+",Wages="+wages);
    }
    public static void main(String[] args) {
    // TODO 自动生成的方法存根
    String s1="130502190001010332";
    String s2="doublebest";
    String s3="20181234";
    String s4="铁 1801";
    double value=60.67;
    Student stu1=new Student(s1,s2,s3,s4,value);
    stu1.show();
    Student stu2=stu1;
    Scanner sc1=new Scanner(System.in);
    s3=sc1.next();
    Scanner sc2=new Scanner(System.in);
    s4=sc2.next();
    Scanner sc3=new Scanner(System.in);
    value=sc3.nextDouble();
    stu2.setSNo(s3);
    stu2.setClassName(s4);
    stu2.show();
    Teacher t1=new Teacher(s1,s2,s3,s4,value);
    t1.show();
    Teacher t2=t1;
    t2.setTNo(s3);
    t2.setDepartmentName(s3);
    t2.show();
    }

    }

    运行截图:

     明天任务:以圆类 Circle 为基础设计球类 Sphere

  • 相关阅读:
    【Python】多态、协议和鸭子类型
    【Python】魔法方法之__call__,将对象当方法使用
    【Python】策略模式
    【Python】if 后怎么就可以跟对象?变量交换写法是语法糖吗?
    【Python 库】NumPy 超详细教程(3):ndarray 的内部机理及高级迭代
    【Python 库】NumPy 超详细教程(2):数据类型
    【Python 库】NumPy 超详细教程(1):NumPy 数组
    【杂谈】10 年三线小城 IT 开发的感悟
    【PostgreSQL】安装及中文显示
    【Python 库】轻量级 ORM 框架 peewee 用法详解之——增删改查
  • 原文地址:https://www.cnblogs.com/zyj3955/p/13389649.html
Copyright © 2011-2022 走看看