1:
01.初始化可以想象成c++指针,所以必须要new,如果不new,那默认就是NULL,是不是很形象;
1.map是基类,实现是hashmap,treemap,前者哈希,后者红黑树;
2.编译后增加了set,get方法;@Data和@Getter/@Setter的区别,@Data注解包含了@Getter@Setter,且有toString(),equals等方法。
@Getter
@Setter
public class Student {
public String name;
public int age;
}
3.没有头文件和实现文件之分,所以不分类内和类外的处理,一切都是在类内做的,类内可以直接对静态成员变量做初始化;未修饰public等权限的,默认包内可见;private权限,类内可见;类方法不能定义静态变量;
public class App
{
static String s; //null
String s1 = new String();
String s2; //null
private int v_ = 2;
public int v1_; //0
public static void main( String[] args )
{
App p = new App();
int a = F();
System.out.println( "Hello World!" + a + " " + p.F1());
System.out.println( "Hello World!" + p.v_ + " " + p.v1_);
App p1 = null; //不初始化报错
static App p2; //报错
System.out.println( "Hello World!" + p.s + " " + p.s1 + " " + p.s2);
}
}
4.interface与abstract修饰类时,都是抽象类,不能实例化;区别是,前者不能有实现,后者可以、前者implement后者extend;
public interface A {
void Tell();
}
public class AA implements A {
@Override
public void Tell() {
System.out.println("AA");
}
}
public abstract class B {
void tell() {
System.out.println("B tell");
}
void tell1() {
System.out.println("B tell1");
}
abstract void tell2();
}
public class BB extends B {
@Override
void tell() {
super.tell1();
}
@Override
void tell2() {
}
}
public class RMain {
public static void main( String[] args ) {
A a = new AA();
B b = new BB();
a.Tell();
b.tell();
}
}
5.初始化除了可以显示初始化,构造初始化,还可以代码块初始化,先运行代码块,再运行构造;有点类似参数列表初始化;
public class C {
private static int v = 2;
{
System.out.println("block");
v++;
}
public C() {
v++;
}
public void Tell() {
System.out.println("C" + v);
}
}
6.wait属于Object,因此可以直接调用,调用该函数必须拥有类的监视器,否则抛出异常;看着可能不太习惯,synchronized修饰方法,可以理解为,进入函数直接上了lock_guard,修饰对象的话,感觉每个对象都像是一个lock,还需验证这个想法;
public class RMain {
// public void Wait() throws InterruptedException {
// wait(4000);
// }
public synchronized void Wait() throws InterruptedException {
wait(4000);
}
public static void main( String[] args ) throws InterruptedException {
RMain m = new RMain();
// synchronized (m) {
// m.Wait();
// }
m.Wait();
}
2.
1.JournalSet这个类设计的还是很有意思的,里面会有多个处理方法write等,但是都有重复代码,是不过处理不一样,这时候,利用JournalClosure多态传参的方式,实现了不同的功能;语法上,在new的阶段进行重写;使用的是外观模式;
public class JournalSet {
private interface JournalClosure {
/**
* The operation on JournalAndStream.
* @param jas Object on which operations are performed.
* @throws IOException
*/
public void apply(JournalAndStream jas) throws IOException;
}
@Override
public void write(final FSEditLogOp op)
throws IOException {
mapJournalsAndReportErrors(new JournalClosure() {
@Override
public void apply(JournalAndStream jas) throws IOException {
if (jas.isActive()) {
jas.getCurrentStream().write(op);
}
}
}, "write op");
}
}