• Java笔记八(instanceof,类型转换,static详解,抽象类,接口,内部类以及异常)


    instanceof

    引用类型,判断一个对象是什么类型

    使用方法:

    System.out.println(X instanceof Y);

    代码理解:

    1. public class Application {
    2. public static void main(String[] args) {
    3. //Obiect>String
    4. //Obiect>Person>Teacher
    5. //Obiect>Person>Student
    6. Object object=new Student();
    7. System.out.println(object instanceof Student);
    8. System.out.println(object instanceof Person);
    9. System.out.println(object instanceof Object);
    10. System.out.println(object instanceof Teacher);
    11. System.out.println(object instanceof String);
    12. System.out.println("==========================");
    13. Person person=new Student();
    14. System.out.println(person instanceof Student);
    15. System.out.println(person instanceof Person);
    16. System.out.println(person instanceof Object);
    17. System.out.println(person instanceof Teacher);
    18. //System.out.println(person instanceof String);编译报错
    19. System.out.println("=========================");
    20. Student student=new Student();
    21. System.out.println(student instanceof Student);
    22. System.out.println(student instanceof Person);
    23. System.out.println(student instanceof Object);
    24. /*System.out.println(student instanceof Teacher);
    25. System.out.println(student instanceof String);编译报错*/
    26. }
    27. }

     其Person是Student与Teacher的父类

    类型转换

    父类的引用指向子类的对象

    把子类转换为父类,向上转型

    把父类转换为子类,向下转换:强制转换

    方便方法调用,减少重复的代码

    强制转换:

    1. package com.oop.demo06;
    2. public class Application {
    3. public static void main(String[] args) {
    4. //类型之间的转换
    5. //子类转换为父类可能丢失自己本来的一些方法
    6. Person obj=new Student();
    7. //student将这个对象转换为Student类型,我们就可以使用Student类型的方法了
    8. Student student=(Student)obj;
    9. student.go();
    10. }
    11. }
    1. package com.oop.demo06;
    2. public class Student extends Person {
    3. public void go(){
    4. System.out.println("go");
    5. }
    6. }

    也可以这样写

    1. package com.oop.demo06;
    2. public class Application {
    3. public static void main(String[] args) {
    4. //类型之间的转换
    5. //子类转换为父类可能丢失自己本来的一些方法
    6. Person obj=new Student();
    7. //student将这个对象转换为Student类型,我们就可以使用Student类型的方法了
    8. Student student=(Student)obj;
    9. ((Student) obj).go();
    10. }
    11. }

    低转高自动转换

    1. package com.oop.demo06;
    2. public class Application {
    3. public static void main(String[] args) {
    4. Student student=new Student();
    5. student.go();
    6. Person person=student;
    7. }
    8. }

    static关键字详解

    非静态的方法可以调用静态里面的方法

    静态方法只能调用静态方法的

    1. package com.oop.demo07;
    2. //static
    3. public class Student {
    4. private static int age;//静态变量
    5. private double score;//非静态变量
    6. public void run(){
    7. go();//非静态的方法可以调用静态里面的方法
    8. }
    9. public static void go(){
    10. }
    11. public static void main(String[] args) {
    12. go();//静态方法只能调用静态方法的
    13. new Student().run();
    14. }
    15. }

    关于static的静态代码块

    1. package com.oop.demo07;
    2. public class Person {
    3. {
    4. System.out.println("匿名代码块");//代码块(匿名代码块)
    5. }
    6. static{
    7. System.out.println("静态代码块");
    8. //静态代码块
    9. }
    10. public Person() {
    11. System.out.println("构造方法");
    12. }
    13. public static void main(String[] args) {
    14. Person person=new Person();
    15. System.out.println("================");
    16. Person person1=new Person();
    17. }
    18. }

    运行结果

     

    由此可见静态代码块先执行且只执行依次,匿名代码块可以用来赋初始值

    静态导入包

    1. package com.oop.demo07;
    2. public class Text {
    3. public static void main(String[] args) {
    4. System.out.println(Math.random());//随机生成一个数
    5. }
    6. }

    使用静态导入包

    1. package com.oop.demo07;
    2. import static java.lang.Math.random;
    3. import static java.lang.Math.PI;
    4. public class Text {
    5. public static void main(String[] args) {
    6. System.out.println(random());
    7. System.out.println(PI);
    8. }
    9. }

    运行结果

    注意:通过final修饰的类不能被继承,也就是没有子类

     

    抽象类

    1. package com.oop.demo09;
    2. //abstract 抽象类 extend:单继承 接口可以多继承
    3. public abstract class Action {
    4. //约束,有人帮实现
    5. //abstract 抽象方法,只有方法名字没有方法的实现
    6. public abstract void doSomething();
    7. }
    1. package com.oop.demo09;
    2. //抽象类的所有方法,继承了它的子类,都必须要实现它的方法
    3. public class A extends Action{
    4. @Override
    5. public void doSomething() {
    6. }
    7. }

    1.不能new这个抽象类,只能靠子类区实现他

    2.抽象类中可以写普通方法

    3.抽象方法必须在抽象类中

    接口

    普通类:只有具体实现

    抽象类:具体实现和规范(抽象方法)都有

    接口:只有规范,自己无法写方法。专业的约束,约束和实现分离

    接口的本质是契约,就像是人间的法律一样,制定好之后大家都遵守

    声明类的关键字是class,声明接口的关键字是interface

    实现了接口的类就需要重写接口中的方法,并且可以利用接口实现多继承

    如下代码拥有两个接口,每个接口拥有多个方法

    1. package com.oop.demo10;
    2. //interface
    3. public interface UserService {
    4. //接口中的所有定义其实都是抽象的 public abstract
    5. int AGE=99;
    6. void add(String name);
    7. void delete(String name);
    8. void update(String name);
    9. void query(String name);
    10. }
    1. package com.oop.demo10;
    2. public interface TimeService {
    3. void timer();
    4. }

     

    1. package com.oop.demo10;
    2. //抽象类:extends
    3. //类 可以实现接口 implements 接口
    4. //实现了接口的类就需要重写接口中的方法
    5. //利用接口实现多继承
    6. public class UserServiceImpl implements UserService,TimeService{
    7. @Override
    8. public void query(String name) {
    9. }
    10. @Override
    11. public void update(String name) {
    12. }
    13. @Override
    14. public void delete(String name) {
    15. }
    16. @Override
    17. public void add(String name) {
    18. }
    19. @Override
    20. public void timer() {
    21. }
    22. }

     作用:

    1.约束

    2.定义一些方法,让不同的人实现

    3.接口不能被实例化,接口中没有构造方法

    4.implements可以实现多个接口

    5.必须重写接口中的方法

     

    内部类

    内部类就是在一个类的内部再定义一个类,比如,A类中定义一个B类,那么B类相对A类来说就称为内部类,而A类相对B类来说就是外部类了

    1.成员内部类

    1. package com.oop.demo11;
    2. public class Outer {
    3. private int id=10;
    4. public void out(){
    5. System.out.println("这是外部类的方法");
    6. }
    7. public class Inner{
    8. public void in(){
    9. System.out.println("这是内部类的方法");
    10. }
    11. //获得外部类的私有属性
    12. public void getID(){
    13. System.out.println(id);
    14. }
    15. }
    16. }
    1. package com.oop.demo11;
    2. public class Application {
    3. public static void main(String[] args) {
    4. Outer outer=new Outer();
    5. //通过这个外部类来实例化内部类
    6. Outer.Inner inner=outer.new Inner();
    7. inner.getID();
    8. }
    9. }

    2.静态内部类:使用static修饰,则无法获得外部类的私有属性

    1. package com.oop.demo11;
    2. public class Outer {
    3. private int id=10;
    4. public void out(){
    5. System.out.println("这是外部类的方法");
    6. }
    7. public static class Inner{
    8. public void in(){
    9. System.out.println("这是内部类的方法");
    10. }
    11. //获得外部类的私有属性
    12. }
    13. }

     局部内部类

    1. package com.oop.demo11;
    2. public class Outer {
    3. public void method(){
    4. class Inner{
    5. public void in(){
    6. }
    7. }
    8. }
    9. }

    与局部变量相似

    异常处理机制

    这是一个错误代码,因为0不能作为除数

    1. package com.oop.exception;
    2. public class Test {
    3. public static void main(String[] args) {
    4. int a=1;
    5. int b=0;
    6. System.out.println(a/b);
    7. }
    8. }

    使用catch捕获异常:

     

    1. package com.oop.exception;
    2. public class Test {
    3. public static void main(String[] args) {
    4. int a=1;
    5. int b=0;
    6. try {
    7. System.out.println(a/b);
    8. }catch (ArithmeticException e){//catch 捕获异常
    9. System.out.println("程序出现异常,变量b不能为0");
    10. }finally {//处理善后工作
    11. System.out.println("finally");
    12. }
    13. }
    14. }

     假设要捕获多个异常:从小到大

    1. package com.oop.exception;
    2. public class Test {
    3. public static void main(String[] args) {
    4. int a=1;
    5. int b=0;
    6. try {
    7. System.out.println(a/b);
    8. }catch (Error e){//catch 捕获异常
    9. System.out.println("Error");
    10. }catch (Exception e){
    11. System.out.println("Exception");
    12. }catch (Throwable t){
    13. System.out.println("Throwable");
    14. }finally {
    15. System.out.println("finally");
    16. }
    17. }
    18. }

     快捷键ctrl+alt+t,快速将代码块使用功能包裹

     主动抛出异常:

    1. package com.oop.exception;
    2. public class Test {
    3. public static void main(String[] args) {
    4. int a=1;
    5. int b=0;
    6. try {
    7. if (b==0){
    8. throw new ArithmeticException();
    9. }
    10. System.out.println(a/b);
    11. }catch (Exception e){
    12. System.out.println("Exception");
    13. }finally {
    14. System.out.println("finally");
    15. }
    16. }
    17. }

    运行结果:

    在方法中抛出异常:

    1. package com.oop.exception;
    2. public class Test {
    3. public static void main(String[] args) {
    4. new Test().test(1,0);
    5. }
    6. public void test(int a,int b){
    7. if (b==0){
    8. throw new ArithmeticException();//主动抛出异常,一般在方法中使用
    9. }
    10. //System.out.println(a/b);
    11. }
    12. }

    可见即使不用输出也可以抛出异常

    自定义异常

    这里自定义一个传递数字的异常,如果传递的数字大于10则抛出异常

    1. package com.oop.demo12;
    2. //自定义的异常类
    3. public class MyException extends Exception{
    4. //传递数字,大于10抛异常
    5. private int detail;
    6. public MyException(int a) {
    7. this.detail=a;
    8. }
    9. //toString异常的打印信息
    10. @Override
    11. public String toString() {
    12. return "MyException{"+detail+'}';
    13. }
    14. }
    1. package com.oop.demo12;
    2. public class Test {
    3. //可能会存在异常的方法
    4. static void test(int a) throws MyException {
    5. System.out.println("传递的参数为:"+a);
    6. if (a > 10) {
    7. throw new MyException(a);
    8. }
    9. System.out.println("OK");
    10. }
    11. public static void main(String[] args) {
    12. try {
    13. test(11);
    14. }catch (MyException e){
    15. System.out.println("MyException=>"+e);
    16. }
    17. }
    18. }

    可见我们此时传递的数字为11,此时抛出我们自定义的异常

    经验总结

    ◆处理运行时异常时,采用逻辑去合理规避同时辅助try-catch处理

    ◆在多重catch块后面,可以加一个catch (Exception) 来处理可能会被遗漏的异常

    ◆对于不确定的代码,也可以加上try-catch,处理潜在的异常

    ◆尽量去处理异常,切忌只是简单地调用printStackTrace() 去打印输出

    ◆具体如何处理异常,要根据不同的业务需求和异常类型去决定

    ◆尽量添加finally语句块去释放占用的资源

    在此狂神java基础笔记全部结束,用时一个月左右学完,也不能说是学完,算是看完吧,真的很喜欢狂神的java基础课,仍然记得狂神的那句话:学编程是为了更好得建模这个世界。愿我们与月作伴的日子不要忘记自己的初心。路漫漫其修远兮,吾将上下而求索!

    狂神哔哩哔哩主页:遇见狂神说的个人空间-遇见狂神说个人主页-哔哩哔哩视频

  • 相关阅读:
    使用OpenResty+Lua实现灰度测试(金丝雀)
    RabbitMQ的高可用和高可靠
    MyBatis与MySql关系阐述
    office办公软件太贵了 Microsoft的Word为什么要买 Microsoft365家庭版多少钱 Microsoft365密钥
    java高级用法之:在JNA中使用类型映射
    跨境电商如何搭建独立站?
    Redis02-高级使用
    Python灰帽编程——初识Python下(函数与文件)
    TypeScript学习文档-基础篇(完结)
    数据库sql查询优化
  • 原文地址:https://blog.csdn.net/m0_73770225/article/details/133689288