1.重载
子类覆写父类的方法称为重载Override。
父类和子类拥有一摸一样的方法(方法的名字、返回值、参数是相同的,但是方法的语句是不一样的)
- 方法签名如果不同就不是重载,而是创建了一个新的方法。
- 加上@Override可以让编译器帮助检查是否进行了正确的覆写
- @Override不是必需的
Person.java
public class Person /*extends Object */{
protected String name;
private int age;
public Person(){
this.name = "王重阳";
}
public Person(String name){
this.name = name;
}
public void setAge(int age){
this.age = age;
}
public int getAge(){
return this.age;
}
public void run(){
System.out.println(name+" is running!
");
};
}
Student.java
public class Student extends Person{
private int score;
public Student(String name){
super(name);
}
public Student(){
super();
}
public int getScore(){
return this.score;
}
public void setScore(int score){
this.score = score;
}
@Override
public void run(){
System.out.printf("Student %s is running
", name);
}
}
Hello.java
public class Hello {
public static void main(String[] args){
//Person和Student拥有相同的run方法,
Person s = new Student("张三丰");
Person p = new Person();
s.run();
p.run();
}
}
# 2.多态
引用变量的声明类型可能与其实际类型不符。如s是Person引用类型的变量,但其指向的实例是Student类型实例。从运行结果得出,实例对象的方法调用总是对应实际类型。Java的实例方法调用是基于运行时实际类型的动态调用。这种动态调用成为多态。
* 多态是针对某个类型的方法调用,其真正执行的方法取决于运行时期实际类型的方法。
* 对某个类型调用某个方法,执行的方法可能是某个子类的覆写方法
* 利用多态,允许添加更多类型的子类实现功能扩展。
Object定义的几个重要方法:
- toString:把instance输出为String
- equals:判断两个instance是否逻辑相等
- hashCode:计算一个instance的哈希值
Person.java使用Object的toString方法
public class Person /*extends Object */{
protected String name;
private int age;
public Person(){
this.name = "王重阳";
}
public Person(String name){
this.name = name;
}
public void setAge(int age){
this.age = age;
}
public int getAge(){
return this.age;
}
public void run(){
System.out.println(name+" is running!
");
};
}
Person.java覆写toString方法
public class Person /*extends Object */{
protected String name;
private int age;
public Person(){
this.name = "王重阳";
}
public Person(String name){
this.name = name;
}
public void setAge(int age){
this.age = age;
}
public int getAge(){
return this.age;
}
public void run(){
System.out.println(name+" is running!
");
};
public String toString(){
return "Person "+this.name;
}
}
Hello.java
public class Hello {
public static void main(String[] args){
Person p = new Person();
System.out.println(p);
System.out.println(p.toString());
}
}
覆写前和覆写后的Person,分别运行Hello.java。
3.super可以调用父类的被Override的方法
Person.java
public class Person /*extends Object */{
protected String name;
private int age;
public Person(){
this.name = "王重阳";
}
public Person(String name){
this.name = name;
}
public void setAge(int age){
this.age = age;
}
public int getAge(){
return this.age;
}
public void run(){
System.out.println(name+" is running!");
};
}
Student.java
public class Student extends Person{
private int score;
public Student(String name){
super(name);
}
public Student(){
super();
}
public int getScore(){
return this.score;
}
public void setScore(int score){
this.score = score;
}
@Override
public void run(){
super.run();
System.out.printf("Student %s is running
", name);
}
}
Hello.java
public class Hello {
public static void main(String[] args){
Person s = new Student();
s.run();
}
}
# 4.final
* 用final修饰的方法不能被Override
* 用final修饰的类不能被继承
* 用final修饰的字段在初始化后不能被修改
```#java
public class Person{
public final void serName(String name){ }
}
public final class Student{
private final int score;
}
```
# 5.总结:
* 子类可以覆写父类的方法Override
* 覆写在子类中,改变了父类方法的行为
* 多态:Java的方法调用总是作用于对象的实际类型
* final修饰的方法可以阻止被覆写
* final修饰的class可以阻止被继承
* final修饰的field必须在创建对象时初始化