多态
基本概念
- 现实事物经常会体现出多种形态,如学生,学生是人的一种,则一个具体的同学张三既是学生也是人,即出现两种形态。
- 即同一方法可以根据发送对象的不同而采取多种不同的行为方式
- 一个对象的实际类型是确定的,但可以指向对象的引用类型有很多
- 存在条件:
- 有继承关系
- 子类重写父类方法
- static方法不能重写
- final不能重写
- private不能重写
- 父类引用指向子类对象
- 多态是方法的多态,属性没有多态
测试类
import com.oop.demo06.Person;
import com.oop.demo06.Student;
public class Application {
public static void main(String[] args) {
//一个对象的实际类型是确定的
// new Student();
// new Person();
//一个对象可以指向的引用类型是不确定的:父类的引用指向子类
//子类能调用的方法都是自己的或者继承父类的。
//父类可以指向子类,但是不能调用子类独有的方法
Student s1 = new Student();
Person s2 = new Student();
Object s3 = new Student();
s2.run();//子类重写了父类的方法,执行子类的方法
s1.run();
//对象能执行哪些方法,主要看对象左边的类型,和右边关系不大
((Student)s2).eat();
s1.eat();
}
}
倒数第二行原本s2是不能调用eat方法的,因为person类(父类)中根本没有eat这个方法,而父类是不能调用子类中独有的方法的,原因是子类本就是父类的延伸,自然其独有的东西是不能被父类所调用的
父类
public class Person {
public void run(){
System.out.println("run");
}
}
子类
public class Student extends Person{
@Override
public void run() {
System.out.println("son");
}
public void eat(){
System.out.println("eat");
}
}
输出结果为
son
son
eat
eat
instanceof
-
要想编译成功,需要具有父子关系
System.out.println("X instanceof Y");
-
实例
import com.oop.demo06.Person;
import com.oop.demo06.Student;
import com.oop.demo06.Teacher;
public class Application {
public static void main(String[] args) {
Object object = new Student();
System.out.println(object instanceof Student);
System.out.println(object instanceof Person);
System.out.println(object instanceof Object);
System.out.println(object instanceof Teacher);
System.out.println(object instanceof String);
System.out.println("=======================================");
Person person=new Student();
System.out.println(person instanceof Student);
System.out.println(person instanceof Person);
System.out.println(person instanceof Object);
System.out.println(person instanceof Teacher);
//System.out.println(person instanceof String);//编译即会报错,因为两个根本没一点关系
System.out.println("=======================================");
Student student = new Student();
System.out.println(student instanceof Student);
System.out.println(student instanceof Person);
System.out.println(student instanceof Object);
//System.out.println(student instanceof Teacher);//编译即报错,因为两个一点关系也没有
//System.out.println(student instanceof String);//同上
}
}
输出结果为
true
true
true
false
false
==================================
true
true
true
false
===================================
true
true
true
- 父类转子类:强制转化,不丢失方法;子类转父类:默认转,丢失本身方法
static
- 静态变量,静态属性
//static
public class Student {
private static int age;//静态变量(常用于多线程)
private double score;//非静态变量
public void run(){
}
public static void go(){
}
public static void main(String[] args) {
new Student().run();
Student.go();//Student甚至可以省略
}
}
- 代码块
public class Person {
{
//代码块(匿名代码块)
}
static {
//静态代码块
}
}
public class Person {
//2:常用来赋初始值
{
System.out.println("匿名代码块");
}
//1:只执行一次
static {
System.out.println("静态代码块");
}
//3
public Person(){
System.out.println("构造方法");
}
//
public static void main(String[] args) {
Person person1 = new Person();
System.out.println("==============================");
Person person2 = new Person();
}
}
输出结果为
静态代码块
匿名代码块
构造方法
=====================================
匿名代码块
构造方法
可见静态代码块是最先被执行的,但是只会执行一次