面向对象
以类的方式组织代码,以对象的组织(封装)数据
重写
需要有继承关系,子类重写父类的方法!
- 方法名必须相同
- 参数列表必须相同
- 修饰符:范围可以扩大但不能缩小: public > protected > default > private
- 抛出异常:范围,可以被缩小,但不能扩大: ClassNotFoundException --> Exception (大)
重写,子类的方法和父类必须一致,方法体不同!
为什么需要重写:
-
父类的功能,子类不一定需要,或者不一定满足!
Alt + Insert; override;
多态
-
多态是方法的多态,属性没有多态
-
父类和子类,有关系, 类型转换异常!ClassCastException!
-
存在条件:继承关系,方法需要重写,父类的引用指向子类的对象! Father f1 = new Son();
方法不能重写:1. static方法,属于类,它不属于实例
-
final 常量;
-
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); // 编译报错!
}
}
抽象类
- 抽象类不能new,只能通过子类实现;约束!
- 抽象类中可以有普通方法
- 抽象方法必须在抽象类中
接口
- 只有抽象类,自己无法写方法,专业的约束!约束和实现分离:面向接口编程
- 接口就是规范,接口的本质是契约
- 接口中的所有定义其实都是抽象的 public abstract
- 接口都需要有实现类,实现了接口的类,就需要重写接口的方法
- 接口中的属性都是静态常量
- 接口不能被实例化,接口中没有构造方法
异常 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);
}
}
}