9.26学习记录(构造器、对象内存分析、super、重写、多态、接口、抽象、异常)

构造器

  • 特征

    • 和类名相同

    • 没有返回值

  • 作用

    • new 本质在调用构造方法

    • 初始化对象的值

  • 注意点

    • 定义有参构造之后,如果想使用无参构造,显示的定义一个无参构造

    • alt+insert快捷生成构造器

     

public class Person {
  //一个类即使什么都不写,它也会存在一个方法
   String name;
   //使用new关键字, 本质实在调用构造器
   //用来初始化值
   public Person(){
       
  }
   //有参构造:一旦定义了有参构造,无参就必须显示定义
   public Person(String name) {
       this.name = name;
  }
}

 

对象内存分析

  • 栈:存放的都是方法中的局部变量。方法的运行一定要在栈中运行。

    • 局部变量:方法的参数,或者是方法{}内部的变量

    • 作用域:一旦超出作用域,立刻从栈内存当中消失

  • 堆:凡是new出来的东西,都在堆当中。

    • 堆内存里面的东西都是一个地址值:16进制

    • 堆内存里面的数据,都有默认值。规则:

      • 如果是整数: 默认为0

      • 如果是浮点数: 默认为0.0

      • 如果是字符: 默认为'\u0000'

      • 如果是布尔: 默认为false

      • 如果是引用类型: 默认为null

  • 方法区:存储.class相关信息,包含方法的信息

 

super注意点

  1. super调用父类的构造方法,必须在构造方法的第一个

  2. super必须只能出现在子类的方法或者构造方法中!

  3. super 和 this 不能同时调用构造方法!

  • super和this的不同

    • 代表的对象不同

      • this:本身调用者这个对象

      • super:代表父类对象的应用

    • 前提

      • this:没有继承也可以使用

      • super:只能在继承条件下才能使用

    • 构造方法

      • this():本类的构造

      • super():父类的构造

 

重写 @Override

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

    • 方法名必须相同

    • 参数列表必须相同

    • 修饰符:范围可以扩大但不能缩小

    • 抛出的异常:范围可以被缩小但不能被扩大 ClassNotFoundException - - > Exception(大)

  • 重写:子类的方法和父类的方法必须要一致;但方法体不同

  • 为什么要重写:父类的方法子类不一定需要,或者不一定满足!

 

多态

  • 注意事项:

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

    2. 父类和子类才可以转换 否则类型转换异常!ClassCastException!

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

  • 不能被重写的方法

    • static方法,属于类,它不属于实例

    • final 常量

    • private方法

 

instanceof和类型转换

  • instanceof引用类型,判断一个对象是什么类型,判断两个类直接是否存在父子关系

 public static void main(String[] args) {
       //Object > String
       //Object > Person > Teacher
       //Object > Person > Student

//       System.out.println(x instanceof y);   用于判断是否可以编译通过!
       Object object=new Student();
       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);//false 编译出错!
       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);//false   编译出错!
//       System.out.println(student instanceof String);//false   编译出错!
       
  }
  • 强制转换

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

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

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

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

    • 子转父可能丢失方法;父转子可能损失精度

 

Static关键字

  • 匿名代码块及静态代码块

public class Person {
   //第二个执行
  {
       //匿名代码块   与对象同时产生,在构造方法之前,用于赋初始值。
       System.out.println("匿名代码块");
  }
   //第一个执行,最早执行,与类一起加载
   static{
       //静态代码块   永久只执行一次
       System.out.println("静态代码块");
     
  }
   //第三个执行
   public Person() {
       System.out.println("构造方法");
  }

   public static void main(String[] args) {
       Person person = new Person();

  }

 

  • 静态导入包 静态导入后无需Math.就能使用

//静态导入包
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这个抽象类,只能靠子类去实现它,继承自抽象类的子类,必须实现父类的所有抽象方法(Override)

  • 抽象类中可以写普通的方法

  • 抽象方法必须在抽象类中

  • 抽象方法只有方法名字,没有方法的实现

  • 抽象的抽象:约束

存在的意义:提高开发效率,后期可扩展性高!

 

 

接口(interface)

  • 作用

    • 约束

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

    • 接口中默认的方法都是public abstract

    • 接口中所有的常量都是public static final

    • 接口不能被实例化 -----因为接口中没有构造方法

    • implements可以实现多个接口

    • 实现接口必须要重写接口中的方法

 

异常

  • 快捷键 :选中 CTRL+ALT+T 快捷生成try catch

  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 static void main(String[] args) {
       try {
           new Test1().test(1,0);
      } catch (ArithmeticException e) {
           e.printStackTrace();
      }
  }
   //假设这个方法中,处理不了这个异常,在方法上抛出异常
   public void test(int a,int b) throws ArithmeticException{
       if (b==0){
           throw new ArithmeticException();//主动抛出的异常,一般在方法中使用
      }
  }
  • 自定义异常

    • 异常的定义

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

         public MyException(int a) {
             this.detail = a;
        }
      // toString:异常的打印信息
         @Override
         public String toString() {
             return "MyException{"+ detail + '}';
        }
      }
    • 异常的使用

    • 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(1);
            } catch (MyException e) {
                 System.out.println("MyException=>"+e);
            }
        }
      }

 

集合,IO流,多线程

 

posted @ 2020-10-07 13:04  A1varo  阅读(139)  评论(0)    收藏  举报