JavaSE基础

面向对象

以类的方式组织代码,以对象的组织(封装)数据

重写

需要有继承关系,子类重写父类的方法!

  1. 方法名必须相同
  2. 参数列表必须相同
  3. 修饰符:范围可以扩大但不能缩小: public > protected > default > private
  4. 抛出异常:范围,可以被缩小,但不能扩大: ClassNotFoundException --> Exception (大)

重写,子类的方法和父类必须一致,方法体不同!

为什么需要重写:

  1. 父类的功能,子类不一定需要,或者不一定满足!

    Alt + Insert; override;

    多态

    1. 多态是方法的多态,属性没有多态

    2. 父类和子类,有关系, 类型转换异常!ClassCastException!

    3. 存在条件:继承关系,方法需要重写,父类的引用指向子类的对象! Father f1 = new Son();

      方法不能重写:1. static方法,属于类,它不属于实例

      1. final 常量;

      2. private 方法;

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

instanceof(类型转换)引用类型

package com.oop;

import com.oop.demo02.Person;
import com.oop.demo02.Student;
import com.oop.demo02.Teacher;

public class Application {
    public static void main(String[] args) {
        
        //Object > String
        //Object > Person > Teacher
        //Object > Person > Student
        Object object = new Student();

        //System.out.println(X instanceof Y); //能不能编译通过,必须存在子父类关系
        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 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); // 编译报错!

        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); // 编译报错!
    }
}

抽象类

  1. 抽象类不能new,只能通过子类实现;约束!
  2. 抽象类中可以有普通方法
  3. 抽象方法必须在抽象类中

接口

  1. 只有抽象类,自己无法写方法,专业的约束!约束和实现分离:面向接口编程
  2. 接口就是规范,接口的本质是契约
  3. 接口中的所有定义其实都是抽象的 public abstract
  4. 接口都需要有实现类,实现了接口的类,就需要重写接口的方法
  5. 接口中的属性都是静态常量
  6. 接口不能被实例化,接口中没有构造方法

异常 Throwable

检查性异常:

运行时异常:

错误:

package com.exception;

public class Demo01 {
    public static void main(String[] args) {
        int a = 1;
        int b = 0;
        //ctrl + alt + t
        try{
            System.out.println(a / b);
        }catch (Error e){
            System.out.println("除数不能为0");
        }catch (Exception e){
            System.out.println("除数不能为1");

        }catch (Throwable e){
            System.out.println("除数不能为2");
        } finally {
            System.out.println("finally");
        }
    }
    //假设这个方法中处理不了这个异常。方法上抛出异常
    public void test(int a, int b) throws ArithmeticException{
        throw new ArithmeticException(); //主动抛出异常,一般在方法中使用
    }
}

自定义异常

MyException.class

package com.exception.demo02;

public class MyException extends Exception{
    //传递数字>10;
    private int detail;

    public MyException(int a){
        this.detail = a;
    }

    //toString :异常的打印信息
    @Override
    public String toString() {
        return "MyException{" +
                "detail=" + detail +
                '}';
    }
}

Test.class

package com.exception.demo02;

public class Test {
    static void test(int a) throws MyException {
        System.out.println("传递的参数为:" + a);
        if(a > 10){
            throw new MyException(a); //抛出
        }
        System.out.println("OK");
    }

    public static void main(String[] args) {
        try {
            test(11);
        } catch (MyException e) {
            //增加一些处理异常的代码
            System.out.println("MyException=>" + e);
        }

    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值