Java文件中类的规则
javac编译后,会生成多个类名.class文件。运行时,使用java 类名执行具有主函数的类。Jar文件的创建与执行
java -jar test.jar执行前,需要在MANIFEST.MF中定义Main-Class属性,例如:Main-Class: test.someClassName。包(package)
/表示层级,例如 E:/Java/workspace/Demo/bin/p1/p2/Test.java。p1.p2.Test 表示包 p1.p2 下的 Test 类。import 导入
import语句只能导入包中的类或接口,不能导入整个包。*导入包下所有类或接口,例如:import java.util.*;。覆盖规则:
final后,子类不能覆盖。super.method调用父类方法,但不能调用父类的private方法。静态成员(静态变量和静态方法)
类名.成员)。静态代码块
注意事项
this和super关键字。main方法)是静态的。main一般是public static void main(String[] args) 。this
super
public static final(变量)和public abstract(方法)。implements。接口之间可用extends继承,且支持多继承。super();)。super(参数列表)调用父类构造函数。this调用本类其他构造函数,则不能同时使用super,因为二者必须在第一行。成员变量
成员函数
静态方法
示例:
java展开代码outer.inner in = new outer().new inner();
in.show();
静态内部类:
java展开代码outer.inner in = new outer.inner();
in.show(); 
java展开代码class Outer {
    int num = 3;
    class Inner {
        int num = 4;
        void show() {
            int num = 5;
            System.out.println(Outer.this.num);
        }
    }
    void method() {
        new Inner().show();
    }
}
final修饰的局部变量。示例:
java展开代码abstract class Demo {
    abstract void show();
}
class Outer {
    int num = 4;
    public void method() {
        new Demo() {
            void show() {
                System.out.println("show ..." + num);
            }
        }.show();
    }
}
class InnerClassDemo4 {
    public static void main(String[] args) {
        new Outer().method();
    }
}
错误示例:
java展开代码Object obj = new Object() {
    public void show() {
        System.out.println("show run");
    }
};
obj.show();
因为Object类没有show()方法。
典型使用场景:函数参数为接口类型时,使用匿名内部类。
java展开代码interface Inter {
    void show1();
    void show2();
}
class InnerClassDemo5 {
    public static void main(String[] args) {
        show(new Inter() {
            public void show1() {}
            public void show2() {}
        });
    }
    public static void show(Inter in) {
        in.show1();
        in.show2();
    }
}
catch捕获异常,无法解决时使用throws延续异常处理。自定义异常:继承自Exception或RuntimeException。
类间访问权限:
| public | protected | default | private | |
|---|---|---|---|---|
| 同一类中 | Y | Y | Y | Y | 
| 同一包中 | Y | Y | Y | N | 
| 子类中 | Y | Y | N | N | 
| 不同包中 | Y | N | N | N | 
java展开代码class SubThread extends Thread {
    public void run() {
        System.out.println("hahah");
    }
}
SubThread s = new SubThread();
s.start();
run方法。start方法。好处:分离任务和线程实现,避免单继承限制。
java展开代码class ThreadImpl implements Runnable {
    public void run() {
        System.out.println("runnable run");
    }
}
Thread t = new Thread(new ThreadImpl());
t.start();
call方法,有返回值。get方法获取返回值。java展开代码import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.FutureTask;
public class CallableThreadTest implements Callable<Integer> {
    public static void main(String[] args) {
        CallableThreadTest ctt = new CallableThreadTest();
        FutureTask<Integer> ft = new FutureTask<>(ctt);
        for (int i = 0; i < 100; i++) {
            System.out.println(Thread.currentThread().getName() + " 的循环变量i的值" + i);
            if (i == 20) {
                new Thread(ft, "有返回值的线程").start();
            }
        }
        try {
            System.out.println("子线程的返回值:" + ft.get());
        } catch (InterruptedException | ExecutionException e) {
            e.printStackTrace();
        }
    }
    @Override
    public Integer call() throws Exception {
        int i = 0;
        for (; i < 100; i++) {
            System.out.println(Thread.currentThread().getName() + " " + i);
        }
        return i;
    }
}
Runnable、Callable接口方式:
Thread.currentThread()获取当前线程。Thread继承方式:
this获取当前线程。

本文作者:Dong
本文链接:
版权声明:本博客所有文章除特别声明外,均采用 CC BY-NC。本作品采用《知识共享署名-非商业性使用 4.0 国际许可协议》进行许可。您可以在非商业用途下自由转载和修改,但必须注明出处并提供原作者链接。 许可协议。转载请注明出处!