1. enum 居然有public static void main(String[] args) ,该例子出自wicked cool java.
-
package hezhb_test;
-
-
//enum Dessert{PIE, CAKE, ICECREAM, BROWNIE};
-
enum FruitCategory{SWEET, CITRUS,SMELLY,UNKNOWN};
-
//enum Fruit{APPLE, ORANGE,GRAPEFRUIT,BANANA,DURIAN};
-
-
public enum MyFruit {
-
-
APPLE{ FruitCategory getCategory(){
-
return FruitCategory.SWEET;}
-
},
-
-
ORANGE{ FruitCategory getCategory(){
-
return FruitCategory.CITRUS;}
-
},
-
-
GRAPEFRUIT{ FruitCategory getCategory(){
-
return FruitCategory.CITRUS;}
-
},
-
-
BANANA { FruitCategory getCategory(){
-
return FruitCategory.SWEET;}
-
},
-
-
DURIAN { FruitCategory getCategory(){
-
return FruitCategory.SMELLY;}
-
};
-
-
abstract FruitCategory getCategory();
-
-
public static void main(String[] args){
-
MyFruit a = MyFruit.APPLE;
-
//toString() returns "APPLE"
-
System.out.println("The toString(); for a:"+a);
-
//getCategory() returns "SWEET"
-
System.out.println("a.getCatetory() is: "+a.getCategory());
-
for(MyFruit f: MyFruit.values()){
-
System.out.println("Fruit is :"+f);
-
}
-
}
-
}
2. 写个ThreadSleepDemo看看纳秒的精度,感觉ms和ns的精度有点吓人啊。根本是无法控制的,误差肯定会很大.
当前我犯了个错误而eclipse居然没发现,我知道long在c里面printf要用%ld,我就顺手写了个%ld,然后执行出错,但是eclipse不认为我有错。
报错是这个:
-
Exception in thread "main" java.util.UnknownFormatConversionException: Conversion = 'l'
后来我改成了%d才能执行,看了看这个,发现java 没有%ld
-
package multithread;
-
-
public class ThreadSleepDemo {
-
public static void main(String[] args){
-
Runnable r = ()->{
-
try{
-
Thread.sleep(10); //sleep 10ms
-
Thread.sleep(0,10000); //sleep 10000ns,0.01ms
-
}catch(InterruptedException e){
-
e.printStackTrace();
-
}
-
};
-
-
Thread tA = new Thread(r);
-
long startTime = System.nanoTime();
-
r.run();
-
long endTime = System.nanoTime();
-
long eclapsed = endTime -startTime;
-
System.out.printf("elapsed time is %d",eclapsed);
-
}
-
}
阅读(2540) | 评论(0) | 转发(0) |