Java日记11
多态
- 动态编译:类型:可扩展性
- 即同一个方法可以根据发送对象的不同而采用多种不同的行为方式
- 一个对象的行为类型是确定的,但可以指向对象的引用类型有很多
- 多态存在的条件
- 有继承关系
- 子类重写父类方法
- 父类引用指向子类对象
- 注意:多态是方法的多态,属性没有多态性
- instanceof
父类:Person
public class Person {
public void run() {
System.out.println("run");
}
}
子类:Student
public class Student extends Person {
@Override
public void run() {
System.out.println("son");
}
public void eat() {
System.out.println("吃");
}
}
测试:Application
import Opp04.Person;
import Opp04.Student;
public class Application {
public static void main(String[] args) {
//一个对象的实际类型是确定的
//new Student();
//new Person();
//可以指向的引用类型不确定,父类的引用指向子类
//Student能调用的方法都是自己的或者继承父类的
Student s1 = new Student();
//Person父类型,可以指向子类,但是不能调用子类独有的方法
Person s2 = new Student();
Object s3 = new Student();
//对象能执行哪些方法,主要看对象左边的类型,和右边关系不大
((Student)s2)/*强制装换*/.eat();//子类重写了父类的方法,执行子类的方法
s1.eat();
}
}
运行结果
吃
吃
多态注意
- 多态是方法的多态,属性没有多态
- 父类和子类,有联系 类型转换异常!Class Cat Exception!
- 存在条件:继承关系,方法需要重写,父类引用指向子类对象!Father f1=new Son();
- static 方法,属于类,他不属于实例
- final 常量
- private方法
编译看左,person instanceof Teacher (Person person与Teacher有)编译对。student instanceof Teacher(Student student与无)编译错;执行看右,他是Student但不是Teacher,所以flase
public class Application {
public static void main(String[] args) {
//Object>String
//Object>Person>Teacher
//Object>Person>Student
Object object = new Student();
//System.out.println(Xinstanceof Y);能否进行编译通过
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
false
测试:Application
public class Application {
public static void main(String[] args) {
//类型之间的转换:父 子
//高 低
Person student =new Student();
//student.go;Person的student不能直接使用Student的方法
//student建这个对象转移为Student类型,就可以使用Student的方法了!
Student student1=(Student) student;
student1.go();
}
}
子类:Student
public class Student extends Person {
public void go() {
System.out.println("go");
}
}
运行结果
go
- 父类引用指向子类对象
- 把字类转化为父类,向上转型:
- 把父类转化为子类,向下转型:强制转换
- 方便方法的调用,减少重复代码,简洁
抽象:封装 ,继承,多态
//static
public class Student {
private static int age;//静态变量
private double score;//非静态变量
public void run() {
go();
}
public static void go() {
}
public static void main(String[] args) {
new Student().run();
go();
}
}
public class Person {
{
//匿名代码块
System.out.println("匿名代码块");//2
}
static{
//静态代码块
System.out.println("静态代码块");//1:只执行一次
}
public Person() {
System.out.println("构造方法");//3
}
public static void main(String[] args) {
Person person1 = new Person();
System.out.println("===============");
Person person2 = new Person();
}
}
静态代码块
匿名代码块
构造方法
===============
匿名代码块
构造方法
//静态导入包
import static java.lang.Math.random;
import static java.lang.Math.PI;
public class Test {
public static void main(String[] args) {
System.out.println(random());//随机数
System.out.println(PI);
}
}
0.5638795469304532
3.141592653589793
抽象
- abstract修饰符可以用来修饰方法也可以修饰类,如果修饰方法,那么该方法就是抽象方法;如果修饰类,那么该类就是抽象类。
- 抽象类中可以没有抽象方法,但是有抽象方法的类一定要声明为抽象类
- 抽象类不能使用new关键字来创建对象,它是用来让子类继承的
- 抽象方法,只有方法的声明,没有方法的实现,它是用来让子类实现的
- 子类继承抽象类,那么就必须要实现抽象类没有实现的抽象方法,否则该子类也要声明为抽象类
//abstract 抽象类;类 extends 单继承 接口可以多继承
public abstract class Action {
//约束.有人帮我们实现
//abstract,抽象方法,只有方法名字,没有方法实现!
public abstract void doSomething();
}
//抽象类的所有方法,继承了他的子类,都必须要实现他的方法,除非子类也是抽象的
public class A extends Action {
@Override
public void doSomething() {
}
}