接口
作用:
约束
定义一些方法,让不同的人实现
public abstract
接口不能被实例化,接口中没有构造方法
implements可以实现多个接口
必须要重写接口中的方法
package oop.demo01.demo01.demo09; //类 可以实现接口 implements 接口 //实现了接口的类,就需要重写接口的方法 //多继承 利用接口实现多继承 public class UserServiceImpl implements UserService,TimeService{ @Override public void query(String name) { } @Override public void update(String name) { } @Override public void delete(String name) { } @Override public void add(String name) { } @Override public void timer() { } }package oop.demo01.demo01.demo09; //interface 接口关键字 ,接口都需要有实现类 public interface UserService { //常量 int age = 99; //接口中的所有定义其实都是抽象的 public abstract void add(String name); void delete(String name); void update(String name); void query(String name); }package oop.demo01.demo01.demo09; public interface TimeService { void timer(); }内部类
package oop.demo01.demo01.demo02; import oop.demo01.demo01.demo06.A; import oop.demo01.demo01.demo06.B; import oop.demo01.demo01.demo07.Person; import oop.demo01.demo01.demo07.Student; import oop.demo01.demo01.demo07.Teacher; import oop.demo01.demo01.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(); } }package oop.demo01.demo01.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); } } }package oop.demo01.demo01.demo10; public class Outer { } //一个java类中可以有多个class类,但只能有一个public 类 class A{ }异常机制
package Exception.demo01; public class test { public static void main(String[] args) { try { new test().test1(1,0); } catch (Exception e) { throw new RuntimeException(e); } } //假设这方法中,处理不了这个异常,方法上抛出异常 public void test1(int a,int b){ if(b==0){//主动抛出异常 throw throws throw new ArithmeticException();//主动抛出异常,一般在方法中使用 } System.out.println(a/b); } /* int a=1; int b=0; //假设要捕获多个异常:从小到大 try{//try监控区域 if(b==0){//主动抛出异常 throw throws throw new ArithmeticException();//主动抛出异常 } System.out.println(a/b); } catch (Error e) {//catch 捕获异常 System.out.println("Error"); }catch (Exception e){ System.out.println("Exception"); }catch (Throwable t){ System.out.println("Throwable"); } finally { System.out.println("finally"); } //finally 可以不要finally,假设IO,资源,关闭! */ }package Exception.demo01; public class Test2 { public static void main(String[] args) { int a=1; int b=0; //Ctrl + Alt + T try { System.out.println(a/b); } catch (Exception e) { throw new RuntimeException(e); } finally { } } }自定义异常
package Exception.demo01.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 Exception.demo01.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) { System.out.println("MyException=>" + e); } } }