Java面向对象03

Java 面向对象03

多态

  • 多态编译:类型

  • 即同一方法可以根据发送对象的不用而采取多种不同的行为方式。

  • 一个对象的实际类型是确定的,但可以指向对象的引用的类型有很多

  • 多态存在的条件

    1. 有继承关系
    2. 子类重写父类方法
    3. 父类引用指向子类对象
  • 注意:

    1. 多态是方法的多态,属性没有多态性。
    2. 父类和子类,有联系 ClassCastException! 类型转换异常
    3. 存在条件:继承关系,方法需要重写,父类引用指向子类对象! Father f1 = new Son();
    4. 无法继承常量类

代码示例:

main()

import oop.Demo06.Person;
import oop.Demo06.Student;
public class Application {
    public static void main(String[] args) {
        //一个对象的实际类型是确定的
        //new Person();
        //new Student();

        //可以指向的引用类型就不确定了:父类的引用指向子类

        //Student能调用的类型都是自己的或者从父类继承的
        Student s1 = new Student();
        //Person 父类,可以指向子类但是不能调用子类独有的方法
        Person s2 = new Student();
        Object s3 = new Student();
        //对象能指向那些方法,主要看左边的对象类型
        s2.eat();//报错
        s1.run();
    }
}

Person

public class Person {

    public void run(){
        System.out.println("run!");
    }
}

Student

public class Student extends Person {
    @Override
    public void run() {
        System.out.println("run!son");
    }
    public void eat(){
        System.out.println("eat!");
    }
}
  • instanceof (类型转换) 引用类型

代码示例:

main()

import oop.Demo06.Person;
import oop.Demo06.Student;
import oop.Demo06.Teacher;
public class Application {
    public static void main(String[] args) {
        //Object > String
        //Object > Person > Student
        //Object > String > Teacher
        Object object = new Student();
        //System.out.println(X instanceof Y);[X]
        System.out.println(object instanceof Student );//true
        System.out.println(object instanceof Person);//true
        System.out.println(object instanceof Object);//true
        System.out.println(object instanceof Teacher);//false
        System.out.println(object instanceof String);//false
        System.out.println("=========================");
        
        Person person = new Person();
        System.out.println(person instanceof Student );//true
        System.out.println(person instanceof Person);//true
        System.out.println(person instanceof Object);//true
        System.out.println(person instanceof Teacher);//false
        //System.out.println(person instanceof String);//报错
        System.out.println("==========================");
        
        Student student = new Student();
        System.out.println(student instanceof Student );//true
        System.out.println(student instanceof Person);//true
        System.out.println(student instanceof Object);//true
        //System.out.println(student instanceof Teacher);//报错
        //System.out.println(student instanceof String);//报错
    }
}

类:Person

public class Person {

    public void run(){
        System.out.println("run!");
    }
}

Student

public class Student extends Person {
    @Override
    public void run() {
        System.out.println("run!son");
    }
    public void eat(){
        System.out.println("eat!");
    }
    public void go(){
        System.out.println("go!");
    }
}

Teacher

public class Teacher extends Person{
}

类型转换:

代码示例:

import oop.Demo06.Person;
import oop.Demo06.Student;
public class Application {
    public static void main(String[] args) {
        //类型之间的转换:父类    子类

        //高                    低
        Person student = new Student();
        //student.go();报错Person是Student的父类无法调用其中的方法

        //student,将这个对象转化为Student,我们就剋使用Student类型的方法了
        Student student1 = (Student) student;
        student1.go();
        //或者
        ((Student)student).go();
        //父类转子类可能失去一些原有的方法
        Student student2 = new Student();
        student2.go();
        //低-------------------->高
        Person person = student;
    }
}

总结:

  1. 父类引用指向子类
  2. 把子类转换为父类,向上转型
  3. 把父类转为为子类,向下转型
  4. 方便方法的调用,减少重复代码!简洁

抽象:封装、继承、多态! 抽象类,接口。

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) {
        Student student = new Student();
        System.out.println(Student.age);
        System.out.println(student.age);
        System.out.println(student.score);

        new Student().run();
        Student.go();//或者  go();  可直接调用
    }

}

代码块:

public class Person {
    {
        //匿名代码块  第二个执行
        System.out.println("匿名代码块");
    }

    static {
        //静态代码块 第一个执行 且只执行一次
        System.out.println("静态代码块");
    }

    public Person() {
        System.out.println("构造方法");
    }

    public static void main(String[] args) {
        Person person1 = new Person();
        System.out.println("==================");
        Person person2 = new Person();
    }
}

输出结果:

静态代码块
匿名代码块
构造方法
==================
匿名代码块
构造方法

静态导入包

//静态导入包
import static java.lang.Math.PI;
import static java.lang.Math.random;

public class Test {
    public static void main(String[] args) {
        //System.out.println(Math.random());
        System.out.println(random());//直接使用方法
        System.out.println(PI);

    }
}

抽象类

  • abstract 修饰符可以用来修饰方法也可以修饰类,如果修饰方法,那么该方法就是抽象方法;如果修饰类,那么该类就是抽象类。

  • 抽象类中可以没有抽象方法,但是由抽象方法的类一定要声明为抽象类。

  • 抽象类,不能使用 new 关键字来创建对象,它是用来让子类继承的。

  • 抽象方法,只有方法的声明,没有方法的实现,它是用来让子类实现的。

  • 子类继承抽象类,那么就必须要实现抽象类没有实现的抽象方法,否则该子类也需要声明为抽象类。

代码示例:

Action

//abstract 关键字 抽象类, extends 单继承~    接口可以多继承
public abstract class Action {
    //约束~有人帮我们实现
    //abstract,抽象方法,只有方法名字,没有方法实现!
    public abstract void dosomething();
  
}

A

//抽象类的方法,继承了他的子类都必须要实现他的方法~
public class A extends Action{
    @Override
    public void dosomething() {
    }
}
  1. 不能new抽象类,只能靠子类来实现:约束
  2. 抽象类中可以写普通的方法
  3. 抽象方法必须在抽象类中
    抽象的抽象:约束
  4. 抽象的意义:提高开发效率

思考:抽象类存在构造器吗?

代码:

main():

import oop.Demo08.A;
public class Application {
    public static void main(String[] args) {
        A a = new A();
        a.dosomething();
    }
}

Action

public abstract class Action {
    public abstract void dosomething();
    public Action() {
        System.out.println("构造器");
    }
}

A

public class A extends Action{
    @Override
    public void dosomething() {
        System.out.println("DoSth.");
    }
}

输出:

构造器
DoSth.

抽象类有构造器

posted @ 2021-05-26 22:21  seveN1foR  阅读(34)  评论(0编辑  收藏  举报