第八天:面向对象编程
1 instanceof
instanceof(类型转换)引用类型,判断一个对象是什么类型。
(object) instanceof (type)
判断对象是否符合指定的类型,结果要么是 true,要么是 false。该对象指定的类型可以是他的父类,祖宗类。
class Father{}
class Son extends Father{}
class Daughter extends Father{}public class Application {public static void main(String[] args) {Son son = new Son();System.out.println(son instanceof Son);//trueSystem.out.println(son instanceof Father);//trueSystem.out.println(son instanceof Object);//true//System.out.println(son instanceof Daughter);System.out.println("========================================");Father father = new Father();System.out.println(father instanceof Father);//trueSystem.out.println(father instanceof Object);//trueSystem.out.println(father instanceof Son);//falseSystem.out.println("========================================");Father son1 = new Son();/*1. **`Father`**: 是一个类(通常是父类或接口)。2. **`Son`**: 是 `Father` 的子类(或实现类)。3. **`new Son()`**: 创建一个 `Son` 类的实例对象。4. **`Father son1`**: 声明一个类型为 `Father` 的引用变量 `son1`。5. **`son1 = new Son()`**: 将 `Son` 的实例赋值给 `Father` 类型的引用。* */System.out.println(son1 instanceof Son);//trueSystem.out.println(son1 instanceof Father);//trueSystem.out.println(son1 instanceof Object);//true}}
2 类型转换
2.1 高转低
把父类转成子类,向下转型。
class Person{public void run(){System.out.println("跑");}
}
class Student extends Person{public void go(){System.out.println("走");}
}public class Application {public static void main(String[] args) {Person obj = new Student();//obj.go();((Student)obj).go();//将Person类型的obj转换成Student类型}
}
2.2 低转高
把子类转成父类,向上转型。
class Person{public void run(){System.out.println("跑");}
}
class Student extends Person{public void go(){System.out.println("走");}
}public class Application {public static void main(String[] args) {//子类转成父类,可能会丢失自己本来的一些方法Student student = new Student();student.go();Person person=student;//person.go();student转成Person类型后丢掉了自己的go方法}
}
3 static关键字
用static修饰的类变量或者方法,可以通过类名去直接调用,不用生成对象。
public class Application {public static void main(String[] args) {Student.go();//编译成功Student.run();//编译失败int age = Student.age;//编译成功int score=Student.score;编译失败}
}
class Student{static int age;private double score;public static void go(){}public void run(){}
}
3.1 静态代码块
随类加载,只执行一次后面长存在内存。
public class Application {public static void main(String[] args) {S s = new S();/** 静态代码块匿名代码块构造方法* */System.out.println("======================");S s1 = new S();/*匿名代码块构造方法*/}
}
class S{{System.out.println("匿名代码块");}static {System.out.println("静态代码块");}public S() {System.out.println("构造方法");}
}
4 抽象类
- 不能new抽象类,只能靠子类去继承它,它存在的意义是约束
- 抽象类中可以有普通方法,抽象方法必须在抽象类中
- 当某个类继承该抽象类时,必须重写它的抽象方法
abstract class Action{public abstract void run();
}class DoSomething extends Action{@Overridepublic void run() {System.out.println("跑");}
}
public class Application {public static void main(String[] args) {DoSomething doSomething = new DoSomething();doSomething.run();}
}
5 接口
- 普通类:只有具体实现
- 抽象类:具体实现和规范(抽象方法)都有。
- 接口:只有规范,自己无法写具体的方法,只有方法名,没方法体,约束和实现分离
- 接口中的方法如果不写public abstract那么会默认是public abstract的
- 一般实现接口的类都以Impl结尾
- 可以继承多个接口
- 实现接口的类需要重写接口的方法
- 接口中定义的常量public static final(但一般不这样做)
- 接口不能被实例化,没有构造方法
public interface TimeService {void timer();
}
public interface UserService {public static final double PI=3.14;//变量,不写public static final也可以会默认是这样。void add(String name);void delete(String name);void update(String name);void query(String name);
}
public class UserServiceImpl implements UserService,TimeService{@Overridepublic void add(String name) {}@Overridepublic void delete(String name) {}@Overridepublic void update(String name) {}@Overridepublic void query(String name) {}@Overridepublic void timer() {}
}
6 内部类
内部类就是在一个类的内部再定义一个类,比如,A类中定义一个B类,那么B类相对于A类来说就称为内部类。
6.1 成员内部类
- 内部类可以访问外部类的私有属性,私有方法
public class Outer {private int id=10;public void out(){System.out.println("这是外部类的方法");}class Inner{//内部类public void in(){System.out.println("这是内部类的方法");}//获得外部类的私有属性public void GetId(){System.out.println(id);//10}}
}
public class Applicatin {public static void main(String[] args) {Outer outer = new Outer();Outer.Inner inner = outer.new Inner();inner.GetId();//10}
}
6.2 静态内部类
无法访问外部类非静态属性
6.3 局部内部类
public class Outer {public void method(){class Inner{//局部内部类public void in(){}}}
}
6.4 匿名内部类
没有名字初始化类
new Apple().eat();
7 异常
异常处理的五个关键字
try catch finally throw throws
快捷键
Ctrl+Alt+T
java中有异常处理框架
7.1 Error和Exception
error意思是错误,exception意思是异常。
异常发生再程序运行期间,它影响了正常程序执行流程。
- 检查性异常
- 运行时异常
- 错误Error
7.1.1 Error
7.1.2 Exception
7.2 异常处理机制
public class Demo1 {public static void main(String[] args) {int a=1;int b=0;try {//try监控区域System.out.println(a/b);}catch (ArithmeticException e){//catch捕获异常,catch里面的参数是想要捕获的异常类型//最高的异常是ThrowableSystem.out.println("程序出现异常,变量b不能为0");}finally {//处理善后工作,不一定要有System.out.println("finally");}//finally可以不需要,若是涉及到IO流,资源相关的东西需要关闭}
}
假设要捕获多个异常,需要从小异常开始捕获再到大异常
public class Demo2 {public static void main(String[] args) {int a=1;int b=0;try {System.out.println(a/b);}catch (Error e){System.out.println("error");}catch (Exception e){System.out.println("Exception");}catch (Throwable e){System.out.println("Throwable");}finally {System.out.println("finally");}}
}
7.2.1 throw 主动抛出异常
手动制造一个异常,一般在方法中使用
try {
if (count<0){
throw new ArithmeticException(“人员数量是负数:”+count);
}
上面这段代码是制造一个异常
public class Demo5 {public static void main(String[] args) {int count=-100;if (count<0){throw new ArithmeticException("人员数量是负数:"+count);}
Exception in thread “main” java.lang.ArithmeticException: 人员数量是负数:-100
at com.xie.exception.Demo5.main(Demo5.java:7)
public class Demo5 {public static void main(String[] args) {int count =-100;try {if (count<0){throw new ArithmeticException("人员数量是负数:"+count);}} catch (ArithmeticException e) {System.out.println("捕捉到了异常");}}
}
捕捉到了异常
7.2.2 throws
throws将代码中可能产生的异常交给别人来处理
如下面代码,当主程序调用该方法时就必须对该方法抛出的异常进行处理。若是main()函数抛出异常,则是由虚拟机处理的(但不提倡这种写法,因为自己无法处理该异常,因此若是主函数产生的异常更多用try catch来处理)。
public class Demo4 {public static void show() throws InterruptedException {Thread.sleep(1000);}public static void main(String[] args) {try {show();} catch (InterruptedException e) {throw new RuntimeException(e);}}
}
7.3 自定义异常
出异常,则是由虚拟机处理的(但不提倡这种写法,因为自己无法处理该异常,因此若是主函数产生的异常更多用try catch来处理)。
public class Demo4 {public static void show() throws InterruptedException {Thread.sleep(1000);}public static void main(String[] args) {try {show();} catch (InterruptedException e) {throw new RuntimeException(e);}}
}
7.3 自定义异常
[外链图片转存中…(img-soSznBpv-1748400503358)]