• 博客园logo
  • 会员
  • 众包
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • HarmonyOS
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
小K-yf
博客园    首页    新随笔    联系   管理    订阅  订阅
面向对象3(Java)

多态

基本介绍

  • 即同一方法可以根据发送对象的不同而采用多种不同的行为方式
  • 一个对象的实际类型是确定的,但是可以指向对象的引用类型可以很多
  • 多态存在的条件:a.有继承关系;b.子类重写父类方法;c.父类引用指向子类对象
  • 多态注意事项:
    • 多态是方法的多态,属性没有多态
    • 父类和子类,有联系 类型转换异常(ClassCastException)
    • 存在条件:继承关系;子类重写父类方法;父类引用指向子类对象
    • 还有三种方法不能重写:1.static方法(属于类,不属于实例)2.fianl方法(常量,不可改变)3.private方法(私有方法)

运用实例

DuoTai.java

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

        //可以指向的引用类型就不确定了
        //Student能调用的方法都是自己或者继承父类的
        Student s1 = new Student();
        //Person父类型,可以指向子类,但是不能调用子类的独有方法
        Person s2 = new Student();//父类的引用指向子类
        Object s3 = new Student();

        //对象能执行那些方法,主要看对象左边的类型,和右边关系不大
        s1.run();//当子类的方法为空时,子类调用的是父类的方法
        s2.run();//当子类重写了父类的方法时,父类调用的方法是子类的方法

        s1.son();
        //s2.son();
    }
}

父类:Person.java

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

子类:Student.java

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

    public void son(){
        System.out.println("son2");
    }
}

Instanceof关键字的作用

public class Instanceof {
    public static void main(String[] args) {
        //Object>String
        //Object>Person>Student
        //Object>Person>Teacher

        //System.out.println(x instanceof y);代表能不能通过

        Object object=new Student();
        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(Student和Teacher没有继承关系,属于平级,父类都是Person)
        System.out.println(object instanceof String);//false(Student和String没有继承关系)

        System.out.println("=========================");
        Person person=new Student();
        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);//编译报错(因为person和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);//编译报错(因为Student和Teacher没有关系)
        //System.out.println(student instanceof String);//编译报错(因为Student和String没有关系)
    }
}

父类:Person.java

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

子类:Student.java

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

    public void son(){
        System.out.println("son2");
    }
}

子类:Teacher.java

public class Teacher extends Person {

}

类型转换

基本介绍

  • 父类引用指向子类的对象
  • 把子类转换成父类,向上转换
  • 把父类转换成子类,向下转换,强制转换
  • 方便方法的调用,减少重复的代码,简介

运用实例

public class LeiXing {
    public static void main(String[] args) {
        //类型之间的转换:父类    子类
        Student student=new Student();
        student.son();

        //子类转换成父类,可能会丢失自己本来的一些方法
        Person person=student;
        //person.son();(编译报错,无法使用son()方法了)

        //父类转换成子类,强制转换
        ((Student)person).son();//第一种方法

        Student person1= (Student) person;//第二种方法
        person1.son();
    }
}

static关键字详解

实例1

public class Static {
    private static int age;//静态属性
    private int score;//非静态属性

    public static void run(){//静态方法

    }
    public void go(){//非静态方法

    }

    public static void main(String[] args) {
        Static s1 = new Static();

        System.out.println(Static.age);//静态属性可以直接通过类名来调用
        //System.out.println(Static.score);//编译报错
        System.out.println(s1.score);//非静态属性则需要通过对象.属性来调用
        System.out.println(s1.age);//静态属也可以通过对象.属性来调用

        //方法和属性的性质相同
        run();
        Static.run();
        //Static.go();
        s1.go();
    }
}

实例2

public class Static2 {
    //第二执行(一般是用来赋初值)
    {
        System.out.println("匿名代码块");
    }
    //最先执行1(只执行一次)
    static {
        System.out.println("静态代码块");
    }
    //第三执行
    public Static2() {
        System.out.println("构造方法");
    }

    public static void main(String[] args) {
        Static2 static2 = new Static2();
        System.out.println("=============");
        Static2 static3 = new Static2();
    }
}
运行结果:
静态代码块
匿名代码块
构造方法
=============
匿名代码块
构造方法

实例3

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

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

抽象类

基本介绍

  • abstract修饰符可以用来修饰方法也可以修饰类,如果修饰方法则该方法就是抽象方法,如果是修饰类则该类就是抽象类
  • 抽象类中可以没有抽象方法,但是抽象方法的类必须是抽象类
  • 抽象类,不能使用new关键字来创建对象,他是用来让子类继承的,没有构造器
  • 抽象方法,只有方法声明,没有方法的实现,他是让子类实现的
  • 子类继承抽象类,那么必须实现抽象类没有实现的抽象方法,否则该子类必须声明为抽象类
  • 抽象为单继承,接口则可以多继承

运用实例

//abstract抽象类
public abstract class Abstract {
    //约束,有人帮我们实现
    //抽象方法,只有方法声明,没有方法的实现
    public abstract void doSome();
}

public class Abs extends Abstract{
    //子类继承抽象类,那么必须实现抽象类没有实现的抽象方法,否则该子类必须声明为抽象类
    @Override
    public void doSome() {

    }
}
posted on 2022-05-23 15:23  糖果Tian  阅读(28)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3