接口

接口中成员修饰符是固定的:

成员常量:public static final 成员函数:public abstract

通过接口间接实现了多重继承

接口的特点

接口是对外暴露的规则 接口是程序的工功能扩展 接口的出现降低耦合性 接口可以用来多实现 类与接口之间是实现关系,而且类可以继承一个类的同时实现多个接口 接口与接口之间可以有继承关系

接口与抽象类

抽象类体现继承关系,一个类只能单继承 接口体现实现关系,一个类可以多实现

抽象类中可以定义非抽象方法,供子类直接使用 接口的方法都是抽象,接口中的成员都有固定修饰符

一个简单的实现例子:

package study_java.ex7;

public class InterfaceDemo1 {
    public static void main(String[] args){
        PC pc = new PC();
        Mouse m = new Mouse();
        Iphone ip = new Iphone();
        pc.insertUSB(m);
        pc.insertUSB(ip);
    }
}

// 定义接口
interface USB{
    void connect();
}

// 类实现了接口
class Mouse implements USB{
    public void connect(){
        System.out.println("我是鼠标");
    }
}

class Iphone implements USB{
    public void connect(){
        System.out.println("我是Iphone");
    }
}

class PC {
    public void insertUSB(USB usb){
        System.out.println("插入了usb设备");
        usb.connect();
    }
}

如果有多个接口,并且接口存在继承的时候,代码例子如下:

package study_java.ex7;

public class InterfaceDemo2 {
    public static void main(String[] args){
        TuHao th = new TuHao();
        WomanStar ws = new WomanStar();
        th.marry(ws);
    }
}

interface White{
    public void white();
}

interface Rich{
    public void rich();
}

interface  Beautiful{
    public void beautiful();
}

interface BFM extends White,Rich,Beautiful{

}

class WomanStar implements BFM{
    public void white(){
        System.out.println("我很白");
    }
    public void rich(){
        System.out.println("我很有钱");
    }
    public void beautiful(){
        System.out.println("我很有漂亮");
    }
}


class TuHao{
    public void marry(BFM bfm){
        bfm.white();
        bfm.rich();
        bfm.beautiful();
    }
}

多态

定义:某一类事物的多种存在形态

多态的特点:

成员函数 编译时:要查看引用变量所属类中是否有所调用的成员

在运行时:要查看对象所属类是否有所调用的成员 成员变量 只看引用变量所属的类

关于多态的一个代码例子:

package study_java.ex7;

public class MutilstateDemo1 {
    public static void main(String[] args){
        Jing8 jing8 = new Jing8();
        jing8.meng();
        jing8.cry();

        Dog dog = new Jing8();
        dog.cry();
        dog.watch();



        Animal a = dog;
        a.cry();

        Pet pet = (Pet) a;
        pet.meng();


    }
}


abstract class Animal{
    public void cry(){
        System.out.println("crying...");
    }
}

class Dog extends Animal{
    public void watch(){
        System.out.println("来人了");
    }
}

class Jing8 extends Dog implements Pet{
    public void meng(){
        System.out.println("么么哒");
    }
}

interface Pet{
    void meng();
}

继承中方法可以被覆盖,但是成员变量不能被覆盖

代码例子:

package study_java.ex7;

public class MultiStatDemo2 {
    public static void main(String[] args){
        Jing8 j = new Jing8();
        j.cry();

        Dog dog = j;
        dog.cry();
        System.out.println(j.name);
        System.out.println(dog.name);
    }
}

abstract class Animal{
    abstract public void cry();
}

class Dog extends Animal{
    public String name = "大黄";
    public void cry(){
        System.out.println("旺旺");
    }
}

class Jing8 extends Dog{
    public String name = "小黄";
    public void cry(){
        System.out.println("嘿嘿");
    }
}

异常

异常的体系: Throwable: Error:通常出现重大问题如:运行的类不存在或者内存溢出,不编写代码针对处理 Exception:运行时出现的一些情况,可以通过try catch finally

Exception 和Error的子类名都是以父类名作为后缀的

Trowable中的方法

getMessage():获取异常信息,返回字符串

toString():获取异常信息和异常类名,返回字符串

printStackTrace():获取异常类名和异常信息,以及异常出现在程序中的位置,返回值void

printStackTrace(printStram s):通常用该方法将异常内容保存在日志文件中

throws和throw

throws用于标识函数暴露出的异常 throw用于抛出异常对象 throws和throw的区别: throws用在函数上,后面跟异常类名,声明抛出异常时使用的关键字 throw用在函数内,后面跟异常对象,抛出异常的语句

异常处理

try { 需要检测的代码 } catch { 异常处理代码 } finally { 一定会执行的代码 }

Finally代码只有一种情况不会被执行,就是在之前执行了System.exit(0)

package study_java.ex8;

public class ThrowableDemo1 {
    public static void main(String[] args){
        float r = divide(4,0);
        System.out.println(r);
        int[] arr = null;
        int[] arr2 = new int[4];
//        System.out.println(getLength(arr2));
        System.out.println(getLength(arr));
    }

    public static float divide(int a, int b){
        return (float) a / b;
    }

    public static int getLength(int[] arr){
        int len = -1;
        try {
            len =  arr.length;
        }
        catch (Exception e){
            System.out.println("出错了"+ e.getMessage());
            // return -2; 这里一般也不会加return
        }
        finally {
            len += 1;
            System.out.println("程序执行完了");
            // return len; 这里一般不会加return
        }
       return len;
    }
}

自定义异常

自定义类继承Exception或者其子类

通过构造函数定义异常信息

通过throw将自定义异常抛出

一个简单的例子:

package study_java.ex7;

public class ExceptionDemo1 {
    public static void main(String[] args){
        Person p = new Person();
        try{
            p.setAge(1000);
        }
        catch (Exception e){
            ((AgeTooBigException)e).printlnError();
        }

    }
}

class Person{
    private int age;
    public int getAge(){
        return age;
    }

    public void setAge(int age) throws  AgeTooBigException {
        if (age > 200) {
            throw new AgeTooBigException();
        }
        this.age = age;
    }
}


class AgeTooBigException extends Exception{
   private String info;
   public AgeTooBigException(String info){
       this.info=info;
   }
   public AgeTooBigException(){
       this("年龄太大了");
   }
   public void printlnError(){
       System.out.println(info);
   }
}

一个稍微复杂的例子:

package study_java.ex7;

public class ExceptionDemo1 {
    public static void main(String[] args){
        Person p = new Person();
        try{
            p.setAge(-10);
        }
        catch (AgeTooBigException e){
            e.printlnError();
        }
        catch (AgeTooSmallException e){
            e.printlnError();
        }
        catch (AgeInvalidException e){
            e.printlnError();
        }

    }
}

class Person{
    private int age;
    public int getAge(){
        return age;
    }

    public void setAge(int age) throws AgeTooBigException,
                                       AgeTooSmallException,
                                       AgeInvalidException {
        if (age > 200) {
            throw new AgeTooBigException();
        }
        else if (age <10 &amp;&amp; age > 0){
            throw new AgeTooSmallException();
        }
        else if (age < 0){
            throw new AgeInvalidException("年龄非法");
        }
        else{
            this.age = age;
        }

    }
}

// 年龄非法异常
class AgeInvalidException extends Exception{
    private String info;
    public AgeInvalidException(String info){
        this.info = info;
    }
    public void printlnError(){
        System.out.println(info);
    }
}

// 年龄太大异常
class AgeTooBigException extends AgeInvalidException{
   public AgeTooBigException(String info){
       super(info);
   }
   public AgeTooBigException(){
       this("年龄太大了");
   }
}

// 年龄太小异常
class AgeTooSmallException extends AgeInvalidException{
    public AgeTooSmallException(String info){
        super(info);
    }
    public AgeTooSmallException(){
        this("年龄太小了");
    }
}

如果我们不处理异常而是直接抛出可以直接在man函数里抛出异常,这样就将异常抛给了java虚拟机

package study_java.ex8;

public class ExceptionDemo1 {
    public static void main(String[] args) throws Exception {
        Person p = new Person();
        p.setAge(-10);
        // 处理异常
        /*
        try{
            p.setAge(2);
        }
        catch (AgeTooSmallException e){
            e.printlnError();
        }
        catch (AgeTooBigException e){
            e.printlnError();
        }
        catch (AgeInvalidException e){
            e.printlnError();
        }
        */
    }
}

class Person{
    private int age;
    public int getAge(){
        return age;
    }

    public void setAge(int age) throws AgeTooBigException,
                                       AgeTooSmallException,
                                       AgeInvalidException{
        if (age> 200){
            throw new AgeTooBigException();
        }
        else if (age < 10 &amp;&amp; age > 0){
            throw new AgeTooSmallException();
        }
        else if (age < 0){
            throw new AgeInvalidException("年龄不合法");
        }
        else {
            this.age = age;
        }

    }
}

class AgeInvalidException extends Exception{
    private String info;

    public AgeInvalidException(String info){
        this.info = info;
    }

    public void printlnError(){
        System.out.println(info);
    }
}

class AgeTooBigException extends AgeInvalidException{
    public AgeTooBigException(String info){
        super(info);
    }
    public AgeTooBigException(){
        this("年龄太大了");
    }
}

class AgeTooSmallException extends AgeInvalidException{
    public AgeTooSmallException(String info){
        super(info);
    }
    public AgeTooSmallException(){
        this("年龄太小了");
    }
}

关于异常处理的细节:

RuntimeException 以及其子类如果在函数中被throw抛出,可以不用再函数上声明抛出语句,也不是必须用try catch语句处理 一个方法被覆盖时,覆盖它的方法必须抛出相同的异常或者异常的子类 如果父类抛出多个异常,那么重写(覆盖)方法必须抛出那些异常的一个子集,不能抛出新的异常

CheckedException 待检异常,也就是非运行时异常,必须使用try catch语句处理

包的概念

对类文件进行分类管理 给类提供多层命名空间 卸载程序文件的第一行 类名的全称的是包名.类名 包也是一种封装方式

如: javac -d classes PackageDemo1.java 编译java源文件,制定存放目录 java -cp classes com.zhaofan.java.PackageDemo1 运行程序,类全限定名

包之间的访问: 被访问的包中的类权限必须是public 类中的成员权限:public 或者protected protected是为其他包中的子类提供的一种权限

如果一个类是public 文件名必须是类名

这里顺便整理一下常见的几种权限在java中:

图片描述

import导入

一个程序文件中只有一个package,可以有多个import 用来导包的类,不导入包中的包

小结:

private 私有的不能继承

public

protected 受保护的,针对其他包中的子类

default 默认,不写,不同包不能继承