继承
定义Person类
public class Person{
public String name;
public int age;
public String getInfo(){
}
}
定义Student类
public class Student{
public String name;
public int age;
public String school;
public String getInfo(){
}
}
人类{
姓名
性别
年龄
打印信息的方法
}
学生类{
姓名
性别
年龄
学校
打印信息的方法
}
工人类{
姓名
性别
年龄
工种
打印信息的方法
}
教师类{
姓名
性别
年龄
科目
打印信息的方法
}
问题:按照上面说的写代码,重复写某些东西,如果直接这样硬写,那代码重复的比较多
public class Person {
int age;
String name;
int sex;
public void showInfo(){
System.out.println(this.age);
System.out.println(this.name);
System.out.println(this.sex);
}
}
public class Student extends Person {
String school;
public void showInfo(){
System.out.println(this.age);
System.out.println(this.name);
System.out.println(this.sex);
System.out.println(this.school);
}
}
1.设置一个父类–人类
通过继承父类的方式去省掉共性代码–子类
子类的共性代码都是继承父类的,每个子类都只要写自己特有的代码
把共性的东西抽取出来形成父类实际需求的子类在继承父类的基础上写自己特有父代码即可
2.作用:
继承的出现提高了代码的复用性。
继承的出现让类与类之间产生了关系,提供了多态的前提。
不要仅为了获取其他类中某个功能而去继承
注:
人类{
姓名
性别
年龄}
狗类 {
姓名
性别
年龄
品种}
3.问题:发现狗这个类和人这个类有重复的属性,可以狗继承人这个类么?不可以,不能为了使用人类的相近属性而继承,如果想做个狗的父类,需要写个动物的类,然后狗继承动物的类。
继承是要有逻辑关系在里面的,不要随意继承。
4.子类继承了父类,就继承了父类的方法和属性。
在子类中,可以使用父类中定义的方法和属性,也可以创建新的数据和方法。
在Java 中,继承的关键字用的是“extends”,即子类不是父类的子集,而是对父类的“扩展”。
5.Java只支持单继承,不允许多重继承
单继承
练习:
public class ManKind {
int sex;
int salary;
public int getSex() {
return sex;
}
public void setSex(int sex) {
this.sex = sex;
}
public int getSalary() {
return salary;
}
public void setSalary(int salary) {
this.salary = salary;
}
public void manOrWoman(){
if(this.sex==1){
System.out.println("man");
} else if(this.sex==0){
System.out.println("Woman");
}
}
public void employee(){
if(this.salary==0){
System.out.println("no job");
}else{
System.out.println("job");
}
}
}
public class Kids extends ManKind {
int yearold;
public int getYearold() {
return yearold;
}
public void setYearold(int yearold) {
this.yearold = yearold;
}
public void printAge(){
System.out.println(this.yearold);
}
public static void main(String[] args){
Kids someKid=new Kids();
someKid.setSex(0);
someKid.setSalary(100);
someKid.manOrWoman();
someKid.employee();
}
}
6.方法的重写(override)
在子类中可以根据需要对从父类中继承来的方法进行改造,也称方法的重置、覆盖。在程序执行时,子类的方法将覆盖父类的方法。
要求:
重写方法必须和被重写方法具有相同的方法名称、参数列表和返回值类型。(子类重写父类的方法,只是重写编写方法体的代码{})
重写方法不能使用比被重写方法更严格的访问权限。
(如果父类的方法是public,子类重写的时候就不能使用缺省以下)
重写和被重写的方法须同时为static的,或同时为非static的
子类方法抛出的异常不能大于父类被重写方法的异常
public class Person {
int age;
String name;
int sex;
public void showInfo(){
System.out.println(this.age);
System.out.println(this.name);
System.out.println(this.sex);
}
public void setInfo(int age,String name,int sex){
this.age=age;
this.name =name;
this.sex=sex;
}
}
public class Student extends Person {
String school;
@Override
public void showInfo() {
System.out.println("以下是Student类对Person类的showInfo方法的重写");
System.out.println(this.age);
System.out.println(this.name);
System.out.println(this.sex);
}
// @Override
// public void setInfo(int age, String name, int sex) {
// System.out.println(age);
// System.out.println(name);
// System.out.println(sex);
// }
public static void main(String[] args){
Student stu=new Student();
stu.showInfo();
}
}
子类可以重新写父类的方法,覆盖父类的方法
如果子类和父类在同一个包下,那么对于父类的成员修饰符只要不是私有的private那就都可以使用。
如果子类和父类不在一个包下,那么子类只能使用父类中Protected和public修饰的成员变量。