java管理学习_java学习 学生管理系统-v3.0 继承

java学习 学生管理系统-v3.0

本次将使用继承来对上次的学生管理系统-v2.0进行改进。

继承

概述

继承是描述java中类与类之间的关系的;使用extends关键字表示,前面是子类,后面是父类;

什么时候使用继承

当我们描述的多个事物之间存在包含(is a)关系的时候,可以使用继承的技术表示;使用继承之后,子类可以直接使用父类非私有的成员;

继承的语法格式

public class 子类类名 extends 父类类名{

}

继承的好处和弊端

好处

提升代码的复用性

提升代码的维护性

为多态提供了前提

弊端

降低了子类代码的独立性

增强了代码之间的耦合性

继承中成员变量的访问

就近原则

当我们在子类的方法中直接写一个变量名时,如果方法内有局部变量,优先使用局部变量,如果局部变量没有,会使用成员变量,如果成员变量没有,会使用父类非私有的成员变量;

如果想直接访问局部变量,直接写变量名

如果想直接访问本类的成员变量,需要写this.成员变量名

如果想直接访问父类的成员变量,如果父类没有私有,可以使用super.成员变量名,如果私有了,可以利用父类的getXxx()方法获取成员变量的值

继承中成员方法的访问

优先找子类,子类没有再找父类;

代码实现

Student与Teacher

由于Student类与Teacher类中有相同的属性

89a020220974aec6fd2c581ba2d9df77.png

所以可创建一个Person类作为父类,而Student类与Teacher类去继承Person类

public class Person {

private Integer id;

private String name;

private Integer age;

private String birthday;

public Person() {

}

public Person(Integer id, String name, Integer age, String birthday) {

this.id = id;

this.name = name;

this.age = age;

this.birthday = birthday;

}

@Override

public String toString() {

return id + "\t" + name + "\t" + age + "\t" + birthday;

}

public Integer getId() {

return id;

}

public void setId(Integer id) {

this.id = id;

}

public String getName() {

return name;

}

public void setName(String name) {

this.name = name;

}

public Integer getAge() {

return age;

}

public void setAge(Integer age) {

this.age = age;

}

public String getBirthday() {

return birthday;

}

public void setBirthday(String birthday) {

this.birthday = birthday;

}

}

Student类与Teacher类需创建构造方法。

public class Student extends Person{

public Student() {

}

public Student(Integer id, String name, Integer age, String birthday) {

super(id, name, age, birthday);

}

}

public class Teacher extends Person {

public Teacher() {

}

public Teacher(Integer id, String name, Integer age, String birthday) {

super(id, name, age, birthday);

}

}

StudentController

在用户输入input方法中发现,

37325c9227e8a434f6902239a9557616.png

使用set方法过与繁琐,代码臃肿,完全可以使用带参构造方法代替

acec8f0203b12005d737b4b9456c448e.png

但想要遵守开闭原则,保留之前版本的代码,则需要将他们共有的部分提取出来,创建一个BaseStudentController类作为父类

public class BaseStudentController {

}

c799fb8b387901c8cab3b80985480dfd.png

将这些共有的代码放到BaseStudentController父类中,创建OtherStudentController类来写新的使用构造方法的StudentController,与老的StudentController共同继承BaseStudentController。

public class StudentController extends BaseStudentController {

Scanner sc = new Scanner(System.in);

public Student input(Integer id) {

System.out.print("请输入姓名:");

String name = sc.next();

System.out.print("请输入年龄:");

Integer age = sc.nextInt();

System.out.print("请输入生日:");

String birthday = sc.next();

Student student = new Student();

student.setId(id);

student.setName(name);

student.setAge(age);

student.setBirthday(birthday);

return student;

}

}

public class OtherStudentController extends BaseStudentController {

Scanner sc = new Scanner(System.in);

public Student input(Integer id) {

System.out.print("请输入姓名:");

String name = sc.next();

System.out.print("请输入年龄:");

Integer age = sc.nextInt();

System.out.print("请输入生日:");

String birthday = sc.next();

Student student = new Student(id, name, age, birthday);

return student;

}

}

TeacherController与StudentController类似,就不再一一接出来了。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值