面向对象编程(五)
多态
- 动态编译:类型:可扩展性
- 即同一方法可以根据发送对象的不同而采取多种不同的行为方式
- 一个对象的实际类型是确定的,但可以指向对象的引用的类型有很多
- 多态存在的条件
- 有继承关系
- 子类重写父类的方法
- 父类引用指向子类对象
- 注意:多态是方法的多态,属性没有多态性
- **instanceof ** (类型转换) 引用类型,判断一个对象是什么类型
package com.oop;
import com.oop.demo06.Person;
import com.oop.demo06.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.run();
}
}
package com.oop.demo06;
public class Student extends Person{
@Override
public void run() {
System.out.println("son");
}
public void eat(){
System.out.println("eat");
}
}
package com.oop.demo06;
public class Person {
public void run(){
System.out.println("run");
}
}
/*
多态注意事项:
1.多态是方法的多态
2.父类和子类有联系, 类型转换异常 ClassCastException
3.存在条件:继承关系, 方法重写,父类引用指向子类对象
不能重写:
1.static 方法,属于类,不属于实例
2.final 常量
3.private方法
*/
instanceof 和 类型转换
package com.oop;
import com.oop.demo06.Student;
import com.oop.demo06.Person;
import com.oop.demo06.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(object instanceof Student);//Ture
System.out.println(object instanceof Person);//Ture
System.out.println(object instanceof Object);//Ture
System.out.println(object instanceof String);//False
System.out.println(object instanceof Teacher);//False
System.out.println("===============================");
Person person = new Student();
System.out.println(person instanceof Student);//Ture
System.out.println(person instanceof Person);//Ture
System.out.println(person instanceof Object);//Ture
//System.out.println(person instanceof String);//False
System.out.println(person instanceof Teacher);//False
System.out.println("===============================");
Student student = new Student();
System.out.println(student instanceof Student);//Ture
System.out.println(student instanceof Person);//Ture
System.out.println(student instanceof Object);//Ture
//System.out.println(student instanceof String);//False
//System.out.println(student instanceof Teacher);//False
}
}
package com.oop;
import com.oop.demo06.Student;
import com.oop.demo06.Person;
import com.oop.demo06.Teacher;
public class Application {
public static void main(String[] args) {
//类型之间的转换: 基本类型转换 高 > 低 64 32 16 8
//类型之间的转换: 父 子 高 > 低
Person a = new Student();
// a 将这个对象转换为Student类型,就可以使用Student类型的方法了
//((Student)a).run();
Student a1 = (Student) a;
a1.eat();
//子类转换父类,可能丢失一些自己本来的方法
}
}
/*
1.父类引用指向子类对象
2.把子类转换成父类,向上转换
3.把父类转换成子类,向下转换
4.方便方法的调用,减少重复代码
*/
static
package com.oop.demo07;
//static :
public class Student {
private static int age;//静态变量 多线程
private double score;//非静态变量
public void run(){
System.out.println("run");
}
public static void go(){
}
public static void main(String[] args) {
go();
new Student().run();
Student s1 = new Student();
System.out.println(Student.age);
System.out.println(s1.score);
System.out.println(s1.age);
}
}
package com.oop.demo07;
public class Person {
//2 :赋初始值
{
System.out.println("匿名代码块");
}
//1 : 只执行一次
static {
System.out.println("静态代码块");
}
//3
public Person(){
System.out.println("构造方法");
}
public static void main(String[] args) {
Person person = new Person();
System.out.println("===============");
Person person1 = new Person();
}
}
package com.oop.demo07;
//静态导入包
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);
}
}
抽象类
- abstract修饰符可以用来修饰方法也可以修饰类,如果修饰方法,那么该方法就是抽象方法;如果修饰类,那么就是抽象类
- 抽象类中可以没有抽象方法,但是有抽象方法的类一定要声明为抽象类
- 抽象类,不能使用new关键字来创建对象,它是用来让子类继承的
- 抽象方法,只有方法的声明,没有方法的实现,它是用来让子类实现的
- 子类继承抽象类,那么就必须要实现抽象类没有实现的抽象方法,否则子类也要声明为抽象类
package com.oop.demo08;
//abstract 抽象类 : 类 extends: 单继承 (接口可以多继承)
public abstract class Action {
//约束方法,需要子类来实现
//abstract, 抽象方法,只有方法名字,没有方法的实现
public abstract void doSomething();
//如果抽象类中有普通方法,想调用也必须通过子类来进行
}
/*
1.不能new这个抽象类
2.抽象类中可以写普通方法
3.抽象方法必须在抽象类中
提高开发效率
*/
package com.oop.demo08;
//子类要继承父类的抽象方法,需要重写,并实现
public class A extends Action{
@Override
public void doSomething() {
}
}
接口
声明类的关键字是class,声明接口的关键字是interface
- 普通类:只有具体实现
- 抽象类:具体实现和规范(抽象方法)都有
- 接口:只有规范,自己无法写方法 专业的约束!约束与实现分离:面向接口编程
- 接口就是规范,定义的是一组规则,体现了世界中“若果你是·····则必须能····”的思想,如果你是汽车,则必须能跑············
- 接口的本质是契约,就像是法律必须要遵守
- OO的精髓,是对对象的抽象,最能体现这一点的就是接口。
package com.oop.demo09;
//抽象类: extends 单继承
//类 可以实现接口, implements 接口可以多继承
//实现了接口的类,就需要重写接口中的方法
public class UserServiceImpl implements UserService,TimeService{
@Override
public void add(String name) {
}
@Override
public void delete(String name) {
}
@Override
public void update(String name) {
}
@Override
public void query(String name) {
}
@Override
public void timer() {
}
}
package com.oop.demo09;
public interface TimeService {
void timer();
}
package com.oop.demo09;
//抽象的思维~
//interface 定义接口的关键词,接口都需要有实现类
public interface UserService {
//接口中的所有定义的方法其实都是抽象的 默认 public abstract
void add(String name);
void delete(String name);
void update(String name);
void query(String name);
//定义的属性都默认是 常量 public static final
int age = 99;
}
作业:
1.约束
2.定义一些方法,让不同的人实现
3. 方法 public abstract
4.静态常量 public static final
5.接口不能被实例化,接口中没有构造方法
6.implements 可以实现多个接口
7.必须要重写接口的方法
内部类
- 内部类就是在一个类的内部再定义一个类
- 成员内部类
- 静态内部类
- 局部内部类
- 匿名内部类
package com.oop.demo10;
//成员内部类
public class Outer {
private int id = 10;
public void out(){
System.out.println("这是外部类的方法");
}
public class Inner{
public void in(){
System.out.println("这是内部类的方法");
}
//获得外部类的私有属性、方法
public void getID(){
System.out.println(id);
}
public void getout(){
out();
}
}
}
package com.oop;
import com.oop.demo10.Outer;
public class Application {
public static void main(String[] args) {
Outer outer = new Outer();
//通过外部类实例化内部类
Outer.Inner inner = outer.new Inner();
inner.in();
inner.getID();
inner.getout();
}
}
package com.oop.demo10;
public class Outer {
}
//一个Java类中可以有多个class类,但是只能有一个public class
class A{
public static void main(String[] args) {
}
}
package com.oop.demo10;
public class Outer {
//局部内部类
public void method(){
class Inner{
public void in(){
}
}
}
}
package com.oop.demo10;
//匿名内部类
public class Test {
public static void main(String[] args) {
//没有名字初始化类,不用将实例保存到变量中
new Apple().eat();
UserService userService = new UserService() {
@Override
public void hello() {
}
};
}
}
class Apple{
public void eat(){
System.out.println("1");
}
}
interface UserService{
void hello();
}
异常机制 Exception
- 异常指程序运行中出现的不期而至的各种状况,如:文件找不到、网络连接失败、非法参数等
- 异常发生在程序运行期间,它影响了正常的程序执行流程
简单分类
- 检查性异常:最具代表性的检查性异常是用户错误或问题引起的异常,这是程序员无法预见的。例如要打开一个不存在的文件时,一个异常就发生了,这些异常在编译时不能被简单地忽略
- 运行时异常:运行时异常是可能被程序员避免的异常。运行时异常可以在编译时被忽略
- 错误:错误不是异常,而是脱离程序员控制的问题。错误在代码中通常被忽略。例如,当栈溢出时,一个错误就发生了,它们在编译时也检查不到的
异常体系结构
- Java把异常当作对象处理,并定义了一个基类java.lang.Throwable作为所有异常的超类
- 在Java API中已经定义了许多异常类,这些异常类分为两大类,错误error和异常exception
Error
Exception
捕获和抛出异常
- 抛出异常、捕获异常
- 异常处理的五个关键字
- try 、 catch 、finally 、throw(用在方法中) 、 throws (用在方法上)
package com.exception;
public class Test {
public static void main(String[] args) {
int a = 1;
int b = 0;
//假设要捕获多个异常:级别从小到大去捕获
try{//try监控区域
if (b == 0){ // 主动抛出异常 throw throws
throw new ArithmeticException();//主动抛出异常
}
System.out.println(a/b);
}catch(Error c){//catch(想要捕获的异常类型) 捕获异常
System.out.println("Error");
}catch(Exception e){
System.out.println("Exception");
}catch(Throwable t){
System.out.println("Throwable");
}finally {//处理善后工作
System.out.println("finally");
}
//fianlly不是必须要有的 finally一般再程序结束用来关闭 IO,资源等等
}
}
package com.exception;
public class Test1 {
public static void main(String[] args) {
int a = 1;
int b = 0;
//快捷键 ctrl + alt + t 可自动生成下面的代码
try {
System.out.println(a/b);
} catch (Exception e) {
e.printStackTrace();//打印错误的栈信息
} finally {
}
}
}
public class Test {
public static void main(String[] args) {
try {
new Test().test(1,0);
} catch (ArithmeticException e) {
e.printStackTrace();
}
}
//假设这个方法中。处理不了这个异常,方法上抛出异常
public void test(int a, int b) throws ArithmeticException{
if (b == 0){ // 主动抛出异常 throw throws
throw new ArithmeticException();//主动抛出异常,一般在方法中使用
}
System.out.println(a/b);
}
}
自定义异常
- 使用Java内置的异常类可以描述在编程时出现的大部分异常。除此之外,用户还可以自定义异常,只需要继承Exception类即可
- 自定义异常可分为以下几个步骤:
- 创建自定义异常
- 在方法中通过throw 关键字抛出异常对象
- 如果在当前抛出异常的方法中处理异常,可以使用try - catch 语句捕获并处理;否则在方法的声明处通过throws 关键字指明要抛出给方法调用的异常,继续下一步操作
- 在出现异常方法的调用者中处理异常
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 +
'}';
}
}
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) {
//增加一些处理异常的代码
//e.printStackTrace();
System.out.println(e);
}
}
}
实际应用中的经验总结
- 处理运行时异常时,采用逻辑去合理规避同时辅助 try - catch 处理
- 在多重catch 块后面,可以加一个catch(Exception)来处理可能会被遗漏的异常
- 对于不确定的代码,也可以加上 try - catch 处理潜在的异常
- 尽量去处理异常,切忌只是简单的调用printStackTrace()去打印输出
- 具体如何处理异常,要根据不同的业务需求和异常类型去决定
- 尽量添加finally 语句块去释放占用的资源