1.方法重写课堂练习
package com.logic.override_;
//编写一个Person类 包括属性/private(name, age) 构造器 方法say(返回自我介绍的字符串)
//编写一个Student类 继承Person类 增加id score 属性/private 以及构造器
//定义say方法(返回自我介绍的信息)
//在main中 分别创建Person和Student对象 调用say方法输出自我介绍
public class OverrideExercise {
public static void main(String[] args) {
Student student = new Student("logic", 18, 100, 123);
Person person = new Person("Lucy", 19);
System.out.println(person.say());
System.out.println(student.say());
}
}
package com.logic.override_;
public class Person {
private String name;
private int age;
public Person(String name, int age) {
this.name = name;
this.age = age;
}
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 String say() {
return "Hello " + name + " " + age;
}
}
package com.logic.override_;
public class Student extends Person {
private double score;
private int id;
public Student(String name, int age, double score, int id) {
super(name, age);
this.score = score;
this.id = id;
}
public double getScore() {
return score;
}
public void setScore(double score) {
this.score = score;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String say() {
return super.say() + ", score: " + score + ", id: " + id;
}
}