Java 之面向对象实例 - 继承与重写实例
实例
People.java
package com.company;
public class People {
private String name; // 姓名
private int age; // 年龄
private String sex; // 性别
private String id; // 身份证号
// 默认构造
public People() {}
// 带参构造
public People(String name,int age,String sex,String id) {
this.name = name;
this.age = age;
this.sex = sex;
this.id = id;
}
/**
* 带参构造也可以这样写
public People(String name,int age,String sex,String id) {
this.setName(name);
this.setAge(age);
this.setSex(sex);
this.setId(id);
}
*/
public void setName(String name) {
this.name = name;
}
public String getName() {
return name;
}
public void setAge(int age) {
this.age = age;
}
public int getAge() {
return age;
}
public void setSex(String sex) {
this.sex = sex;
}
public String getSex() {
return sex;
}
public void setId(String id) {
this.id = id;
}
public String getId(){
return id;
}
public void showMyself() {
System.out.println("我的名字是" + getName() + ",我是一名" + getSex() + "生,今年" + getAge() + "岁。我的身份证号是" + getId() + "。");
}
}
Teacher.java
package com.company;
public class Teacher extends People{
private String t_id; // 老师编号
private String teach_subject; // 交的学科
// 默认构造
public Teacher() {}
// 带参构造
public Teacher(String name,int age,String sex,String id,String t_id,String teach_subject) {
// 当调用父类方法时使用super,当使用自身方法时使用this
super.setName(name);
super.setAge(age);
super.setSex(sex);
super.setId(id);
this.setT_id(t_id);
this.setTeach_subject(teach_subject);
}
/**
* 当然这样写也是可以的,只是不是很规范,全部使用this
* 结论:子类继承的变量就是自己的,无论用this,或是用super都一样。
* public Teacher(String name,int age,String sex,String id,String t_id,String teach_subject) {
// 当调用父类方法时使用super,当使用自身方法时使用this
this.setName(name);
this.setAge(age);
this.setSex(sex);
this.setId(id);
this.setT_id(t_id);
this.setTeach_subject(teach_subject);
}
*/
public void setT_id(String t_id) {
this.t_id = t_id;
}
public String getT_id(){
return t_id;
}
public void setTeach_subject(String teach_subject) {
this.teach_subject = teach_subject;
}
public String getTeach_subject(){
return teach_subject;
}
// 重写showMyself()方法
@Override
public void showMyself() {
System.out.println("我是一名教" + getTeach_subject() + "的" + getSex() + "老师,我的名字是" + getName() + ",今年" + getAge() + "岁。我的身份证号是" + getId() + ",教师编号是" + getT_id() + "。");
}
}
Student.java
package com.company;
import java.util.Scanner;
public class Student extends People{
Scanner input = new Scanner(System.in);
private String s_id; // 学号
private String class_num; // 班级
// 成绩
private double Chinese; // 语文
private double Math; // 数学
private double English; // 英语
private double sum; // 总分
// 默认构造
public Student() {}
// 带参构造
public Student(String name,int age,String sex,String id,String s_id,String class_num) {
this.setName(name); // super.setName(name);
this.setAge(age); // super.setAge(age);
this.setSex(sex); // super.setSex(sex);
this.setId(id); // super.setId(id);
this.setS_id(s_id);
this.setClass_num(class_num);
}
public void setS_id(String s_id) {
this.s_id = s_id;
}
public String getS_id() {
return s_id;
}
public void setClass_num(String class_num) {
this.class_num = class_num;
}
public String getClass_num() {
return class_num;
}
public void inputChinese() {
System.out.print("请输入" + getName() + "同学的语文成绩:");
this.Chinese = input.nextDouble();
}
public double getChinese() {
return Chinese;
}
public void inputMath() {
System.out.print("请输入" + getName() + "同学的数学成绩:");
this.Math = input.nextDouble();
}
public double getMath() {
return Math;
}
public void inputEnglish() {
System.out.print("请输入" + getName() + "同学的英语成绩:");
this.English = input.nextDouble();
}
public double getEnglish() {
return English;
}
// 重写
@Override
public void showMyself() {
System.out.println("我的名字是" + getName() + ",我是一名" + getSex() + "生,今年" + getAge() + "岁。我的身份证号是" + getId() + "。");
System.out.println("我来自" + getClass_num());
System.out.println("学号为:" + getS_id());
}
public double addSum() {
return getChinese() + getMath() + getEnglish();
}
// 成绩单
public void showGrade() {
System.out.println(getName() + "的成绩单");
System.out.println("语文:" + getChinese());
System.out.println("数学:" + getMath());
System.out.println("英语:" + getEnglish());
System.out.println("总分:" + addSum());
}
}
Main.java
package com.company;
public class Main{
public static void main(String[] args) {
// People类
People people = new People("张三",12,"男","123456");
people.showMyself();
// Teacher类
Teacher teacher = new Teacher("李四",36,"男","234567","345678","数学");
teacher.showMyself();
// Student类
Student student = new Student("王五",16,"女","456789","9876544","三年二班");
student.showMyself();
student.inputChinese();
student.inputMath();
student.inputEnglish();
student.showGrade();
}
}
结果:
我的名字是张三,我是一名男生,今年12岁。我的身份证号是123456。
我是一名教数学的男老师,我的名字是李四,今年36岁。我的身份证号是234567,教师编号是345678。
我的名字是王五,我是一名女生,今年16岁。我的身份证号是456789。
我来自三年二班
学号为:9876544
请输入王五同学的语文成绩:50
请输入王五同学的数学成绩:60
请输入王五同学的英语成绩:70
王五的成绩单
语文:50.0
数学:60.0
英语:70.0
总分:180.0
注意:
1.使用super关键字调用父类构造方法。
2.默认会调用父类构造,再调用子类构造。
3.构造方法不可继承。
4.Java中只能继承一个父亲。(这点与C++中不同,在C++中可以继承多个父亲)
5.父类私有的属性在子类中不能直接访问。
6.继承关系 - “是一种” is-a关系:父类和子类之间必须存在is-a关系。
⭐7.结论:子类继承的变量就是自己的,无论用this,或是用super都一样。
⭐ Java 继承
继承
继承就是子类继承父类的特征和行为,使得子类对象(实例)具有父类的实例域和方法,或子类从父类继承方法,使得子类具有父类相同的行为。
重写
在子类中提供一个对方法的新的实现。
注意:
1.方法重写发生在通过继承而相关的不同类中。
2.方法重写具有同样的方法签名和返回值类型。
3.@Override称为重写标注,用来保证重写的方法与原方法的签名和返回值一致。