多态
即同一方法可以根据发送对象的不同而采用多种不同的行为方式。
一个对象的实际类型是确定的,但可以指向对象的引用的类型有很多
多态存在的条件
- 有继承关系
- 子类重写父类方法
不能被重写的代码:
1,static 属于类,它不属于实例
2,final 常量
3,private 私有的
- 父类引用指向子类对象
注意:多态是方法的多态,属性没有多态性
类型转换异常 ClassCastException
instanceof
1,子类转换为父类,向上转型,可以直接转换
2,父类转换为子类,向下转型,需要强制转换
3,方便方法的调用,减少重复的代码
4,子类转换为父类可能会失去一些方法
//子类
public class Student extends Person{
@Override
public void run() {
System.out.println("student");
}
public void eat(){
System.out.println("eat");
}
}
//子类
public class Teacher extends Person{
public void go(){
System.out.println("teacher go");
}
}
父类
public class Person {
public void run(){
System.out.println("person run");
}
}
public class Application {
public static void main(String[] args) {
/*一个对象的实际类型是确定
new Student();
new Teacher();
new Person(); */
//可以指向的引用类型就不确定了
//Student能调用的方法有自己的和继承父类的方法
Student s1= new Student();
//Person是父类,可以指向子类,不能调用子类独有的方法
Person p1 = new Student(); //父类的引用指向了子类
Person p2 = new Person();
Object o1 = new Student(); // Object > Person > Student
Object o2 = new String();
Object o3 = new Object();
Student p01 =(Student)p1;//转换类型
p2.run();
p1.run();//子类重写了父类的方法,指向子类,执行子类的方法
s1.run();
((Student)p1).eat();//强制转换p1到Student类型
//p1.eat; 报错 对象能执行哪些方法,主要看对象左边的类型
System.out.println("============");
System.out.println(o1 instanceof Student); //true
System.out.println(o1 instanceof Object); //true
System.out.println(o1 instanceof Person); //true
System.out.println(o1 instanceof Teacher); //false
System.out.println(o1 instanceof String); //false
System.out.println(o2 instanceof String); //true
System.out.println(o3 instanceof String); //false
System.out.println("============");
System.out.println(p1 instanceof Student); //true
System.out.println(p1 instanceof Object); //true
System.out.println(p1 instanceof Person); //true
System.out.println(p1 instanceof Teacher); //false
//System.out.println(p1 instanceof String); 编译报错
System.out.println("============");
System.out.println(s1 instanceof Student); //true
System.out.println(s1 instanceof Object); //true
System.out.println(s1 instanceof Person); //true
//System.out.println(s1 instanceof Teacher); 编译报错
// System.out.println(x instanceof y);
// x是变量 x所指向的实际类型是y的子类型则编译通过(有继承关系)
System.out.println("============");
//类型之间的转换
Person p001 = new Teacher();
//将p001转换为Teacher类型,就可以使用Teacher类型的方法了
Teacher t001 =(Teacher)p001;
t001.go();
((Teacher)p001).go();
//低转高 子类转换为父类可能会丢失一些方法
Teacher t002 = new Teacher();
Person p002 =t002;
//p002.go(); 报错
}
}
run:
person run
student
student
eat
============
true
true
true
false
false
true
false
============
true
true
true
false
============
true
true
true
============
teacher go
teacher go
static
静态方法: static +方法
静态属性: static +属性
public class Student {
private static int age; //静态变量
private double score; //非静态变量
public static void go(){
//run(); 报错
}
public void run(){
go(); //非静态方法可以调用静态方法
}
public static void main(String[] args) {
Student s1 = new Student();
System.out.println(Student.age);
//Student.age是类变量
System.out.println(s1.score);//通过对象调用
//Student.score 没有static不能直接调用
}
}
public final class Person {
{
//(匿名)代码块 作用:赋初始值
}
static {
//静态代码块 只执行一次
}
{ //第2执行
System.out.println("匿名代码块");
}
static{ //第1执行
System.out.println("静态代码块");
}
public Person(){ //第3执行
System.out.println("构造方法");
}
public static void main(String[] args) {
Person p1 =new Person();
System.out.println("===========");
Person p2 = new Person();
}
}
run:
静态代码块
匿名代码块
构造方法
===========
匿名代码块
构造方法
import static java.lang.Math.random;
//静态导入包
public class Test {
public static void main(String[] args) {
System.out.println(Math.random());
System.out.println(random());//调用之后就可以直接使用
}
//被final修饰的类无法被继承;
}