Java学习Day06 各种类 接口 异常
抽象类
public abstract class Action {
public abstract void doSomething();
public void go(){
}
public Action() {
}
}
public class A extends Action{
@Override
public void doSomething() {
}
}
接口
public interface UserService {
int AGE = 99;
void add(String name);
void delete(String name);
void update(String name);
void query(String name);
}
public class UserServiceImp1 implements UserService,TimeService {
@Override
public void add(String name) {
}
@Override
public void delete(String name) {
}
@Override
public void update(String name) {
}
@Override
public void query(String name) {
}
@Override
public void time() {
}
}
public interface TimeService {
void time();
}
内部类
public class Application {
public static void main(String[] args) {
Outer outer = new Outer();
outer.out();
Outer.Inner inner = outer.new Inner();
inner.in();
inner.getID();
}
}
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);
}
}
public void method(){
class A{
}
}
}
异常
public class Demo01 {
public static void main(String[] args) {
new Demo01().A();
}
public void A(){
B();
}
public void B(){
A();
}
}
public class Test {
public static void main(String[] args) {
int a= 1;
int b= 0;
try {
System.out.println(a/b);
new Test().a();
}catch (Error e){
System.out.println("程序异常,变量b不能为0");
}
catch (Throwable t){
System.out.println("Throwable");
}
finally {
System.out.println("结束");
}
try {
new Test().test(1,2);
} catch (ArithmeticException e) {
throw new RuntimeException(e);
} finally {
}
}
public void a(){b();}
public void b(){a();}
public void test(int a,int b) throws ArithmeticException{
if (b == 0) {
throw new ArithmeticException();
}
System.out.println(a/b);
}
}
public class Test02 {
public static void main(String[] args) {
int a= 1;
int b= 0;
try {
if (b == 0) {
throw new ArithmeticException();
}
System.out.println(a/b);
} catch (Exception e) {
throw new RuntimeException(e);
} finally {
}
}
}
自定义的异常
public class MyException extends Exception{
private int detail;
public MyException(int a){
this.detail = a;
}
@Override
public String toString() {
return "MyException{" +
"detail=" + detail +
'}';
}
}
public class MyTest {
static void test(int a) throws MyException {
System.out.println("传递的参数为:");
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);
} finally {
}
}
}