文章

Practice3

2020.10.18 ・ 共 1220 字,您可能需要 3 分钟阅读

Tags: 代码

  • 学生类(Student):属性:姓名、年龄、数学成绩、英语成绩;方法:构造函数(初始化姓名和年龄),设置成绩,求均分;子类(Newstudent):属性:政治成绩;方法:构造函数,设置成绩(与父类的设置成绩构成重载),求均分;在main函数中定义子类的对象,调用其所有方法;
public class Test14 {
  public static void main(String[] args) {
    NewStudent a = new NewStudent("Zhang", 20);
    a.setGrade(45, 35, 77);
    System.out.println(a.getAvg());
  }
}

class Student {
  String name;
  int age;
  float mathGrade;
  float engGrade;

  Student(String name, int age) {
    this.name = name;
    this.age = age;
  }

  void setGrade(float mathGrade, float engGrade) {
    this.mathGrade = mathGrade;
    this.engGrade = engGrade;
  }

  float getAvg() {
    return (mathGrade + engGrade) / 2;
  }
}

class NewStudent extends Student {
  float polGrade;

  NewStudent(String name, int age) {
    super(name, age);
  }

  void setGrade(float mathGrade, float engGrade, float polGrade) {
    this.mathGrade = mathGrade;
    this.engGrade = engGrade;
    this.polGrade = polGrade;
  }

  float getAvg() {
    return (mathGrade + engGrade + polGrade) / 3;
  }
}
  • 日期(Date):属性:年,月,日;方法:设置日期,输出日期;子类(NewDate):属性:时,秒,分;方法:设置日期(和父类构成重载),输出日期(和父类构成重写,但是要调用父类的输出日期函数);在main函数中定义子类的对象,调用其所有方法;
public class Test15 {
  public static void main(String[] args) {
    NewDate a = new NewDate();
    a.setDate(2020, 10, 19, 9, 40, 0);
    a.outputDate();
  }
}

class Date {
  int year;
  int month;
  int day;

  void setDate(int year, int month, int day) {
    this.year = year;
    this.month = month;
    this.day = day;
  }

  void outputDate() {
    System.out.printf("%d年%d月%d日", year, month, day);
  }
}

class NewDate extends Date {
  int hour;
  int second;
  int minute;

  void setDate(int year, int month, int day, int hour, int minute, int second) {
    this.year = year;
    this.month = month;
    this.day = day;
    this.hour = hour;
    this.second = second;
    this.minute = minute;
  }

  void outputDate() {
    super.outputDate();
    System.out.printf("%d时%d分%d秒", hour, minute, second);
  }
}
  • 父类(Data):属性:int a, 方法:求绝对值,求阶乘;子类(Newdata):属性 int n, 方法:求1-n的绝对值和,求1-n的阶乘和;在main函数中定义子类的对象,调用其所有方法;
public class Test16 {
  public static void main(String[] args) {
    NewData a = new NewData();
    a.n = 2;
    System.out.println(a.getA());
    System.out.println(a.getFactorial());
  }
}

class Data {
  int a;

  int getA() {
    if (a < 0) return -a;
    else return a;
  }

  int getFactorial() {
    int result = 1;
    if (a == 0) return 1;
    else {
      for (int i = 1; i <= a; ++i) {
        result *= i;
      }
    }
    return -1;
  }
}

class NewData extends Data {
  int n;

  int getA() {
    int result = 0;
    int m = n;
    if (n < 0) m = -n;
    for (int i = 1; i <= m; ++i) {
      result += i;
    }
    return result;
  }

  int getFactorial() {
    int result = 0;
    if (n < 0) return -1;
    for (int i = 1; i <= n; ++i) {
      int temp = 1;
      for (int j = 1; j <= i; ++j) {
        temp *= j;
      }
      result += temp;
    }
    return result;
  }
}

  • 父类(Employee):属性:姓名、工号、工资;方法:构造函数(初始化姓名、工号),设置工资,输出信息(姓名,工号,工资);输出等级(工资>5000, 为C,否则为D);子类(Leader):属性:等级;方法:设置等级,输出信息(姓名,工号,等级和工资),计算工资(等级为A的,工资+5000;等级为B的,工资增加2000);在main函数中定义子类的对象,调用其所有方法;
public class Test17 {
  public static void main(String[] args) {
    Leader a = new Leader("Zhang", 123);
    a.setPay(5000);
    a.setLevel('A');
    a.outputInf();
    a.calPay();
  }
}

class Employee {
  String name;
  int id;
  float pay;

  Employee(String name, int id) {
    this.id = id;
    this.name = name;
  }

  void setPay(float pay) {
    this.pay = pay;
  }

  void outputInf() {
    System.out.printf("姓名:%s 工号:%d 工资:%f", name, id, pay);
  }

  void outputLevel() {
    if (pay > 5000) System.out.println('C');
    else System.out.println('D');
  }
}

class Leader extends Employee {
  char level;

  Leader(String name, int id) {
    super(name, id);
  }

  void setLevel(char level) {
    this.level = level;
  }

  void outputInf() {
    System.out.printf("姓名:%s 工号:%d 等级:%c 工资:%f", name, id, level, pay);
  }

  void calPay() {
    if (level == 'A') pay += 5000;
    if (level == 'B') pay += 2000;
  }
}