1.子类重写父类方法,只是重写编写方法体的代码
2.如果父类方法是public,那么子重写时候不能使用缺省以下
父类Person1
package day07;
public class Person1 {
int age;
String name;
int sex;
public void showInfo(){
System.out.println("我是父类");
System.out.println(this.age);
System.out.println(this.name);
System.out.println(this.sex);
}
public void setInfo(int age,String name,int sex){
System.out.println("我是父类");
this.age=age;
this.name=name;
this.sex=sex;
}
}
子类Student1
package day07;
public class Student1 extends Person1{
String school;
/**
* 在此处eclipse使用快捷键alt+/可以快速重写
* idea使用ctrl+O可以快速重写
* 得到第12行的重写
*/
@Override
public void showInfo() {
// super.showInfo(); 重写过后会得到这条代码,将他随便改即可,比如我改成下面的14行的样子
System.out.println("hello我是重写过的showInfo\n");
}
@Override
public void setInfo(int age, String name, int sex) {
System.out.println("hello我是重写过的setInfo");
System.out.println(age);
System.out.println(name);
System.out.println(sex);
}
public static void main(String[] args) {
Student1 stu=new Student1();
stu.showInfo();
stu.setInfo(12,"abc",2);
}
}
运行截图