代码块:
package oop.demo04;
//静态导入包
import static java.lang.Math.random;
public class Student {
private static int age=10;
//构造方法,在匿名代码块后执行
Student(){
System.out.println("构造");
}
//静态代码块,只执行一次
static {
System.out.println("静态");
}
//匿名代码块,在创建对象时执行
{
System.out.println("匿名代码块");
}
public static void main(String[] args) {
new Student();
System.out.println(Student.age);
System.out.println(random());
}
}
继承:
package oop.demo05;
public abstract class A {
//抽象类可以写普通方法
//抽象方法必须在抽象类中
public abstract void dosomething();
}
package oop.demo05;
public class B extends A
{
@Override
public void dosomething() {
// TODO Auto-generated method stub
}
}
package oop.demo05;
public class Main {
public static void main(String[] args) {
B b=new B();
b.dosomething();
}
}
接口:
package oop.demo06;
public interface User {
void go();
}
package oop.demo06;
public interface UserService {
//public static final int a=1000;
int a=1000;
//public abstarct void run();
void run();
}
package oop.demo06;
public class UserServiceImpl implements UserService,User{
@Override
public void run() {
// TODO Auto-generated method stub
}
@Override
public void go() {
// TODO Auto-generated method stub
}
}
内部类:
package oop.demo07;
public class Main {
public static void main(String[] args) {
Outer outer=new Outer();
Outer.Inner inner=outer.new Inner();
inner.in();
//匿名
new Outer().out();
new C() {
@Override
public void hello() {
// TODO Auto-generated method stub
}
};
}
}
interface C{
void hello();
}
package oop.demo07;
public class Outer {
private int id=10;
public void out() {
System.out.println("外部");
class B{
public void inout() {
System.out.println("局部");
}
}
//匿名
new B().inout();
}
public class Inner{
public void in() {
System.out.println("内部");
System.out.println(id);
}
}
public static class Inner2{
public void in() {
System.out.println("静态");
//System.out.println(id);
}
}
}
class A{
}
异常:
package exception;
public class Demo01 {
public static void main(String[] args) {
int a=1;
int b=0;
try {//监控区域
System.out.println(a/b);
new Demo01().a();
}catch(Error e) {
System.out.println("Error");
}catch(ArithmeticException e) {//捕获异常
System.out.println("除数为0");
//e.printStackTrace();
}catch(Throwable e) {
System.out.println("异常");
}finally {//处理善后工作
System.out.println("finally");
}
Demo01 demo01=new Demo01();
try {
demo01.test(a, b);
} catch (ArithmeticException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
new Test().test(11);
} catch (MyException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void a(){
b();
}
public void b() {
a();
}
public int test(int a,int b) throws ArithmeticException{
if(b==0) {
throw new ArithmeticException();
}
return a/b;
}
}
package exception;
public class MyException extends Exception{
private int detail;
public MyException(){ }
public MyException(int message) {
this.detail=message;
}
public String toString() {
return "MyException{detail="+detail+"}";
}
}
package exception;
public class Test {
void test(int a) throws MyException {
if(a>10) {
throw new MyException(a);
}
}
}