package com.hualan;
public class Person {
private String name;
private String sex;
private int age;
private String tel;
private String address;
public Person(String name, String sex, int age, String tel, String address) {
this.name = name;
this.sex = sex;
this.age = age;
this.tel = tel;
this.address = address;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getTel() {
return tel;
}
public void setTel(String tel) {
this.tel = tel;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
}
package com.hualan;
/*extends用来完成类的继承,注意能够继承的两个类之间一般存在包含与被包含的关系,其中extends右侧在继承关系中被称为父类,左侧的被称为子类*/
/*
* 继承的优点:
* 1.继承可以有效的减少子类的代码量,从而提高代码的可读性和代码后期的可维护性
* 2.继承可有效提升类的可扩展性,有利于开发人员根据自身需要对通用类进行二次封装
* 继承的缺点:
* 存在继承关系的两个类之间存在一定的关联性,此时父类功能的升级会造成自身功能同时升级,从而降低了类的独立性*/
/*
* 注意:
* 1.子类继承父类时,会将父类中所有的属性和方法全部继承,此时父类的属性如果为private,子类继承的属性无法直接使用,需要在父类中为对应的属性添加setting和getting方法
* 2.子类在父类继承的方法有三种处理方案
* 2.1直接使用:即子类可以直接调用父类中定义的方法,父类中定义的方法在被子类对象调用时,方法的this指向子类对象
* 2.2重写:即子类中定义一个和父类相同的方法(方法名,返回值类型和参数列表完全相同),子类对象在调用重写的方法时,执行按照子类定义的内容执行
* 2.3添加:子类定义一个和父类相同的方法,在该方法中调用父类相同的操作,同时增加属于子类对象特有的操作*/
/*
* super关键字
* 在对象方法中的关键字,作用是告知程序子级对象调用的功能按照父级的定义去实现
* */
public class Student extends Person{
private String studentId;
private String section;
private String major;
public Student(String name, String sex, int age,String studentId,String section,String major) {
/*
*super()函数
*在Java子类的构造函数中,通过super();函数调用父类的构造函数
*作用:告知程序当前的构造函数被子类对象调用,执行时按照父类的构造函数内容执行 */
super(name, sex, age,null,null);
this.studentId=studentId;
this.section=section;
this.major=major;
}
public String getStudentId() {
return studentId;
}
public void setStudentId(String studentId) {
this.studentId = studentId;
}
public String getSection() {
return section;
}
public void setSection(String section) {
this.section = section;
}
public String getMajor() {
return major;
}
public void setMajor(String major) {
this.major = major;
}
public void learn(){
System.out.println(getName()+"正在学习");
}
public void introduce(){
System.out.printf("我叫%s,今年%d岁,性别:%s,学号:%s,班级:%s,专业:%s",this.getName(),this.getAge(),this.getSex(),this.studentId,this.section,this.major);
}
}
package com.hualan;
public class Teacher extends Person{
private String teacherId;
private String position;
public Teacher(String name, String sex, int age, String teacherId, String position) {
super(name,sex,age,null,null);
this.teacherId = teacherId;
this.position = position;
}
public String getTeacherId() {
return teacherId;
}
public void setTeacherId(String teacherId) {
this.teacherId = teacherId;
}
public String getPosition() {
return position;
}
public void setPosition(String position) {
this.position = position;
}
public void lecture(){
System.out.println(this.getName()+"正在讲课");
}
public void introduce(){
System.out.printf("我叫%s,今年%d岁,性别%s",this.getName(),this.getAge(),this.getSex() );
}
}
package com.test;
import com.hualan.Student;
import com.hualan.Teacher;
public class test {
public static void main(String[] args) {
Student student1=new Student("wsj","男",18,"211021107","18","计算机科学与技术");
student1.introduce();
System.out.println();
Teacher teacher1=new Teacher("aww","男",41,null,null);
teacher1.introduce();
}
}