??xml version="1.0" encoding="utf-8" standalone="yes"?>全色精品综合影院,国内久久视频,a日韩av网址 http://www.aygfsteel.com/zk0000/hello java zh-cn Sat, 21 Jun 2025 20:38:18 GMT Sat, 21 Jun 2025 20:38:18 GMT 60 交通灯 http://www.aygfsteel.com/zk0000/articles/342754.html杰点 杰点 Tue, 11 Jan 2011 03:08:00 GMT http://www.aygfsteel.com/zk0000/articles/342754.html http://www.aygfsteel.com/zk0000/comments/342754.html http://www.aygfsteel.com/zk0000/articles/342754.html#Feedback 0 http://www.aygfsteel.com/zk0000/comments/commentRss/342754.html http://www.aygfsteel.com/zk0000/services/trackbacks/342754.html
灯的数量是确定的Q用了枚D。ƈ且由车的通行对称性,得出对称的灯颜色需要一致。ƈ使用定时器设|灯的点亮?br />
1 // 抽象出所有的灯,?2个灯
2 package traffic;
3
4 // 每个方向都有一个灯Q因R辆行驶是对称l构Q?nbsp;q且对称的等的显C颜色是一L
5
6 public enum Lamp {
7 // 元素各表CZ个控制方向的?nbsp;
8 S2N( " N2S " , " S2W " , false ),S2W( " N2E " , " E2W " , false ),E2W( " W2E " , " E2S " , false ),E2S( " W2N " , " S2N " , false ),
9
10 N2S( null , null , false ),N2E( null , null , false ),W2E( null , null , false ),W2N( null , null , false ),
11
12 // q几个方向已l包含在了上面定义的方向?/span>
13 S2E( null , null , true ),E2N( null , null , true ),N2W( null , null , true ),W2S( null , null , true );
14
15 private Lamp(String opposite,String next, boolean lighted){
16 this .opposite = opposite;
17 this .next = next;
18 this .lighted = lighted;
19 }
20
21
22
23 private boolean lighted;
24
25 private String opposite;
26
27 private String next;
28 public boolean isLighted(){
29 // 是否l灯
30 return lighted;
31 }
32
33 // 对称方向的灯颜色要一?/span>
34 public void light(){
35 this .lighted = true ;
36 if (opposite != null ){
37 Lamp.valueOf(opposite).light();
38 }
39 System.out.println(name() + " lamp is greenQ有6个方向可以看到R通过 " );
40
41 }
42
43 // 对称的灯颜色一?nbsp;q且点绿下一个灯
44 public Lamp blackOut(){
45 this .lighted = false ;
46 if (opposite != null ){
47 Lamp.valueOf(opposite).blackOut();
48 }
49
50 Lamp nextLamp = null ;
51 if (next != null ){
52 nextLamp = Lamp.valueOf(next);
53 System.out.println( " l灯K?/span>" + name() + " 切换?/span>" + next);
54 nextLamp.light();
55 }
56 return nextLamp;
57 }
58 }
59
1 // 灯控制器Q根据R辆通行的对U性可知,对称方向的灯要颜色一?/span>
2 package traffic;
3
4 import java.util.concurrent.Executors;
5 import java.util.concurrent.ScheduledExecutorService;
6 import java.util.concurrent.TimeUnit;
7
8 public class LampController {
9 private Lamp currentLamp;
10
11 public LampController(){
12 // 刚开始让由南向北的灯变绿;
13 currentLamp = Lamp.S2N;
14 currentLamp.light();
15
16 // 每隔10U将灯变U?nbsp;点亮下一个绿?/span>
17 ScheduledExecutorService timer = Executors.newScheduledThreadPool( 1 );
18 timer.scheduleAtFixedRate(
19 new Runnable(){
20 public void run(){
21 currentLamp = currentLamp.blackOut();
22 }
23 },
24 10 ,
25 10 ,
26 TimeUnit.SECONDS);
27 }
28 }
29
1 // p\产生车,q检灯的颜Ԍ放行汽R
2 package traffic;
3
4 import java.util.ArrayList;
5 import java.util.List;
6 import java.util.Random;
7 import java.util.concurrent.ExecutorService;
8 import java.util.concurrent.Executors;
9 import java.util.concurrent.ScheduledExecutorService;
10 import java.util.concurrent.TimeUnit;
11
12 // road 产生车辆 Q一个方向对?条\Q?nbsp;共有4个方?nbsp;12条\
13 public class Road {
14 private List < String > vechicles = new ArrayList < String > ();
15
16 private String name = null ;
17 public Road(String name){
18 this .name = name;
19
20 // 模拟车辆
21 ExecutorService pool = Executors.newSingleThreadExecutor();
22 pool.execute( new Runnable(){
23 public void run(){
24 for ( int i = 1 ;i < 1000 ;i ++ ){
25 try {
26 Thread.sleep(( new Random().nextInt( 10 ) + 1 ) * 1000 );
27 } catch (InterruptedException e) {
28 e.printStackTrace();
29 }
30 vechicles.add(Road. this .name + " _ " + i);
31 }
32 }
33
34 });
35
36 // 每隔一U检查灯
37 ScheduledExecutorService timer = Executors.newScheduledThreadPool( 1 );
38 timer.scheduleAtFixedRate(
39 new Runnable(){
40 public void run(){
41 if (vechicles.size() > 0 ){
42 boolean lighted = Lamp.valueOf(Road. this .name).isLighted();
43 if (lighted){
44 System.out.println(vechicles.remove( 0 ) + " is traversing ! " );
45 }
46 }
47
48 }
49 },
50 1 ,
51 1 ,
52 TimeUnit.SECONDS);
53
54 }
55 }
56
1 // ȝ
2 package traffic;
3
4 public class MainClass {
5
6
7 public static void main(String[] args) {
8
9 // 生成12条\
10 String [] directions = new String[]{
11 " S2N " , " S2W " , " E2W " , " E2S " , " N2S " , " N2E " , " W2E " , " W2N " , " S2E " , " E2N " , " N2W " , " W2S "
12 };
13 for ( int i = 0 ;i < directions.length;i ++ ){
14 new Road(directions[i]);
15 }
16
17 // 生成交通灯Q开始自动运?nbsp;
18 new LampController();
19 }
20
21 }
22
]]> 银行调度 http://www.aygfsteel.com/zk0000/articles/342750.html杰点 杰点 Tue, 11 Jan 2011 02:45:00 GMT http://www.aygfsteel.com/zk0000/articles/342750.html http://www.aygfsteel.com/zk0000/comments/342750.html http://www.aygfsteel.com/zk0000/articles/342750.html#Feedback 0 http://www.aygfsteel.com/zk0000/comments/commentRss/342750.html http://www.aygfsteel.com/zk0000/services/trackbacks/342750.html 阅读全文 ]]> 泛型 http://www.aygfsteel.com/zk0000/articles/342249.html杰点 杰点 Tue, 04 Jan 2011 02:05:00 GMT http://www.aygfsteel.com/zk0000/articles/342249.html http://www.aygfsteel.com/zk0000/comments/342249.html http://www.aygfsteel.com/zk0000/articles/342249.html#Feedback 0 http://www.aygfsteel.com/zk0000/comments/commentRss/342249.html http://www.aygfsteel.com/zk0000/services/trackbacks/342249.html 泛型
<>发音为typeof ArrayList<Integer> ArrayList typeof Integer
ArrayList成ؓ原始cd
1.泛型是给~译器看的,Z规范数据cd。以前的集合可以d各种cd的对象?/p>
2.通过字节码比?collection3.getClass() == collection2.getClass()
可以发现Q编译过后的字节码中q没有类型信息?/p>
3.可以通过反射Q透过泛型往集合中添加各U类型元?br />
collection3.getClass().getMethod("add",Object.class).invoke(collection3,"abc");
4.参数化类型不考虑cd参数的?br />
Vector<String> v= new Vector<Object>() //错误
Vector<Object> v= new Vector<String>() //错误
5.在创建数l实例时Q数l的元素不能使用参数化的cd
Vector<Integer> vectorList[] = new Vector<Integer>[10];
? 通配W?用于通配Lcd的集?nbsp;
public static void printCollection(Collection<?> collection){
collection.size();
//collection.add(); 可以调用与参数化无关的方?br />
}
]]>注解 http://www.aygfsteel.com/zk0000/articles/342248.html杰点 杰点 Tue, 04 Jan 2011 02:01:00 GMT http://www.aygfsteel.com/zk0000/articles/342248.html http://www.aygfsteel.com/zk0000/comments/342248.html http://www.aygfsteel.com/zk0000/articles/342248.html#Feedback 0 http://www.aygfsteel.com/zk0000/comments/commentRss/342248.html http://www.aygfsteel.com/zk0000/services/trackbacks/342248.html 注解 Annotation
一个注解就是一个类
@SuppressWarnings("deprecation") 源文仉D?/p>
@Override 重写 源文仉D?/p>
@Deprecated q时 RUNTIME阶段
注解c?br />
@interface A{
}
应用?#8220;注解c?#8221;的类
@A
class B{
}
注解反射
class C{
B.class.isAnnotationPresent(A.class)
A a=B.class.getAnnotation(A.class)
}
@Retention元注?br />
RetetionPolicy.SOURCE 对应源文?br />
RetetionPolicy.CLASS 对应class文g
RetetionPolicy.RUNTIME 内存中的字节?/p>
RetetionPolicy是枚举,
只包?SOURCE,CLASS,RUNTIME三个元素
1 // 注解c?/span>
2 import java.lang.annotation.ElementType;
3 import java.lang.annotation.Retention;
4 import java.lang.annotation.RetentionPolicy;
5 import java.lang.annotation.Target;
6
7 @Retention(RetentionPolicy.RUNTIME)
8 @Target({ElementType.METHOD,ElementType.TYPE})
9 // Target 参数 一个可以,两个以上 参数为数l?br />
10 // 注解的注解目?{Ҏ,c}) 描述classQinterface enum 的父c都是TYPE
11 public @ interface MyAnnotation {
12 // 为注解添加属性,q里的属性就是方法,不需要加public abstract。系l默认加
13 String color() default " blue " ; // 为属性增加默认?/span>
14 String value(); // 当只有一个value属性时Q赋值时可以不用value=
15
16 }
17
1 // 注解的?/span>
2 @MyAnnotation(color = " red " ,value = " 444 " )
3
4 public class AnnotationTest {
5
6 /**
7 * @param args
8 */
9 @SuppressWarnings( " deprecation " )
10 @MyAnnotation( " bacd " )
11 public static void main(String[] args) {
12 // TODO Auto-generated method stub
13 System.runFinalizersOnExit( true );
14 // 判断字节码中是否有注解,不加RUNTIME则没有,字节码在内存?RUNTIME内存q行阶段
15 if (AnnotationTest. class .isAnnotationPresent(MyAnnotation. class ))
16 {
17 MyAnnotation myAnnotation = (MyAnnotation)AnnotationTest. class .getAnnotation(MyAnnotation. class );
18 System.out.println(myAnnotation.color());
19 }
20 }
21
22 }
23
]]> cd载器 http://www.aygfsteel.com/zk0000/articles/342247.html杰点 杰点 Tue, 04 Jan 2011 02:00:00 GMT http://www.aygfsteel.com/zk0000/articles/342247.html http://www.aygfsteel.com/zk0000/comments/342247.html http://www.aygfsteel.com/zk0000/articles/342247.html#Feedback 0 http://www.aygfsteel.com/zk0000/comments/commentRss/342247.html http://www.aygfsteel.com/zk0000/services/trackbacks/342247.html BootStrap JRE/lib/rt.jar
|
ExtClassLoader JRE/lib/ext/*.jar
|
AppClassLoader CLASSPATH指定的所有jar或目?/p>
| |
自定义类加蝲?nbsp;
cd载器也是一个类Q但BootStrap不是Q它嵌入虚拟机,是最栚w的加载器Q加载下层类?br />
cd载器实行委托机制Q如AppClassLoader发v加蝲c,则委托父c,则一直委托到BootStrapQ再开始往下寻扄Q?br />
一直到AppClassLoaderQ如果它也找不到Q则抛出异常
自定义类加蝲器必ȝ承ClassLoaderc,q指定一个父cd载器?/p>
1.E序q行阶段Q碰C个类Q先调用该线E里的类加蝲器加载类?br />
可以为线E指定类加蝲器?br />
2.如果cA引用cBQJAVA虚拟机将使用加蝲了A的类加蝲器加载B?/p>
]]> 集合 http://www.aygfsteel.com/zk0000/articles/341897.html杰点 杰点 Wed, 29 Dec 2010 05:31:00 GMT http://www.aygfsteel.com/zk0000/articles/341897.html http://www.aygfsteel.com/zk0000/comments/341897.html http://www.aygfsteel.com/zk0000/articles/341897.html#Feedback 0 http://www.aygfsteel.com/zk0000/comments/commentRss/341897.html http://www.aygfsteel.com/zk0000/services/trackbacks/341897.html 9.集合框架
1)基本概念:用于存放对象的对?与数l类?即集合也是对?只能存对?
2)和数l的差异
A.数组存放的内容由数组cd指定,集合存放的内容ؓObjectcd的引?br />
B.数组只有1U数据结?按照索引存储),集合的数据结构分为Set.List和Map三大c?每个cMq包含若q的类
C.数组的容量在创徏时指?无法在用中动态扩?所有集合类型的定w都是不定?Ҏ需求自动扩?/p>
3)基本l成
A.接口:用于定义集合的功能框?br />
B.实现c?实现对应的接?实现集合的具体功?br />
C.工具:提供寚w合操作的辅助功能
4)接口
A.Collection
定义存储内容为单个对象的数据l构及相关的操作Ҏ
B.Set
l承Collection,存储内容无序且不可重?br />
C.List
l承Collection,存储元素有序且可以重?br />
D.SortedSet
l承Set.可以对存储内容进行排?br />
E.Map
定义存储内容?个对象(键值对Q的数据l构及相关的操作Ҏ
键(标识?所以不可重复)对象无序且不可重复,值对象可以重复顺序由对应的键对象军_
F.SortedMap
l承MapQ可以对键对象进行排?/p>
5)名词解释
A.无序:存储序与放入顺序无?br />
B.有序:存储序与放入顺序一?br />
C.排序:按照一定的逻辑对内容进行排?存储序依照排序的结果进行排?/p>
6)实现c?br />
A.List(3个实现类)
ArrayList:内部存储形式为数l?擅长查询操作,Ҏ入和删除操作效率较低
LinkedList:内部存储形式为双向链?擅长插入和删除操?Ҏ询操作效率较?/p>
Vector:ArrayList的线E安全版?可保证多U程q发防问时的数据准确和一致?但对效率影响很大,实际开发中使用较少
B.HashSet的重复性判?br />
i)在执行add()操作时进?加入的对象与集合中已存在的对象q行比较,满相等性条件时,待加入的对象无法加入集合?br />
ii)比较内容
?个对象调用equals()l果为true
?个对象分别调用hashcode()的结果相?br />
同时满以上2个条件则判断两个对象相同
C.TreeSet
构造器
TreeSet()--使用默认的排序和重复性判断逻辑
?只有量cd(String.包装cd)存在默认排序和重复性判断逻辑,如Integer升序;如果Ҏ有默认的排序和重复性判断逻辑的对象用该操作,在执行时生ClassCastException
TreeSet(Comparator)--使用指定的比较器实现排序和重复性判断逻辑
实现原理:在加入元素时执行比较(原有元素和待加入的元?,比较时调用比较器的compare().Ҏq回值决定排列的序是否重复
compare()的第一个参?-待加入的元素
compare()的第二个参数--集合中原有的元素
q回?br />
>0 待加入元素在原有元素的右?br />
<0 待加入元素在原有元素的左?br />
=0 相同,待加入元素不加入
??gt;0?lt;0?如集合的相应方向存在未比较的元素,则应l箋比较直至完全定位置后才执行加入
D.Map的实现类
HashMap和HashTable的关pȝ当于ArrayList和Vector的关p?br />
HashMap的重复性判断规?key)
在元素加入时(put)q行判断.方式与HashSet完全相同
E.TreeMap的排序和重复性判断规?key)
在元素加入时(put)q行判断.方式与TreeSet完全相同
1 // List集合 实现cLinkedList ArrayList
2 import java.util. * ;
3 public class ListTest {
4
5 /**
6 * @param args
7 */
8 public static void main(String[] args) {
9 // TODO Auto-generated method stub
10 String a = " A " ,b = " B " ,c = " C " ,d = " D " ,e = " E " ; // 定义字符串对?/span>
11 List < String > list = new LinkedList < String > (); // 创徏List集合
12 list.add(a); // 集合中添加元?/span>
13 list.add(e);
14 list.add(d);
15 Iterator < String > fristIterator = list.iterator(); // 创徏集合q代?/span>
16 System.out.println( " 修改前集合中的元素ؓ " );
17 while (fristIterator.hasNext()){
18 System.out.println(fristIterator.next() + " " );
19 }
20 list.set( 1 ,b);
21 list.set( 2 ,c);
22 Iterator < String > it = list.iterator();
23 System.out.println( " 修改后集合中的元素ؓ " );
24 while (it.hasNext()){
25 System.out.println(it.next() + " " );
26 }
27
28 }
29
30 }
1 // MAP集合 ?nbsp;泛型
2 import java.util. * ;
3 public class MapTest {
4
5 /** 泛型应用
6 * @param args
7 */
8 public static void main(String[] args) throws Exception {
9 // TODO Auto-generated method stub
10
11 // HashMap 定义泛型 cd变量
12 HashMap < String,Integer > maps = new HashMap < String,Integer > ();
13 maps.put( " zhangsan " , 28 ); // d元素
14 maps.put( " lisi " , 33 );
15 maps.put( " wangwu " , 23 );
16
17 // q回Set集合
18 Set < Map.Entry < String, Integer >> entrySet = maps.entrySet();
19
20 // q代输出集合
21 for (Map.Entry < String,Integer > entry : entrySet)
22 {
23 System.out.println(entry.getKey() + " " + entry.getValue());
24 }
25 // 转换cd
26 Object ob = " abc " ;
27 String x3 = autoConvert(ob);
28 }
29
30 private static < T > T autoConvert(Object object){
31 // 泛型Ҏ cd转换
32 return (T)object;
33 }
34
35 private static < T > void fillArray(T[] a,T obj ){
36 // 某个类型的对象 填充?nbsp;对应cd的数l?/span>
37 for ( int i = 0 ;i < a.length;i ++ )
38 a[i] = obj;
39 }
40
41 }
]]> 反射 http://www.aygfsteel.com/zk0000/articles/341895.html杰点 杰点 Wed, 29 Dec 2010 05:21:00 GMT http://www.aygfsteel.com/zk0000/articles/341895.html http://www.aygfsteel.com/zk0000/comments/341895.html http://www.aygfsteel.com/zk0000/articles/341895.html#Feedback 0 http://www.aygfsteel.com/zk0000/comments/commentRss/341895.html http://www.aygfsteel.com/zk0000/services/trackbacks/341895.html 反射的基是Classc?br />
ClassQ?/p>
JAVAcȝ于描qCcM物的共性。而是什么属性则是由cȝ实例对象来确定的Q不同的实例对象有不同的属性倹{?/p>
Class则是用于描述JAVAcȝ共性的c,描述了类的名字,讉K属性,包名Q字D名U列表,Ҏ名称列表{?/p>
Class的实例对象ؓ某个cȝ字节码?br />
获取字节码的实例对象Ҏ有三U?br />
1. cd.class System.class
2. 对象.getClass new Date().getClass()
3. Class.forName("cd") Class.forName("java.util.Date")
Class cls1=Date.class //Datecȝ字节?,已经加蝲到内?br />
Class cls2=p1.getClass();//调用对象的getClass()得到cȝ字节?br />
Class.forName("java.lang.String") //q回该类字节?br />
反射是把JavacM的各U成分映成相应的Javac,如:一个JavacM用Classcȝ对象来表C,一个类中组成的部分Q?br />
成员变量Q方法,构造方法,包等信息Q也是用一个个JavacL表示?/strong>
Class代表一份类字节?/p>
Method cȝ实例对象代表 cȝҎ
Constructorc?br />
ConstructorcM表类中的一个构造方?/p>
//获取某个cȝ所有构造方?br />
Constructor[] constructors=Class.forName("java.lang.String").getConstructors();
//获取某个构造方?要用的参数类??String(StringBuffer buffer)
Constructor constructor=Class.forName("java.lang.String").getConstructor(StringBuffer.class);
创徏实例对象
通常ҎQString str=new String(new StringBuffer("abcd"));
//Constructorcȝ newInstance() Ҏ
反射ҎQString str=(String)constructor.newInstance(new StringBuffer("abc"))
Fieldc,代表某个cȝ属?br />
Field fieldy=pt1.getClass().getField("y"); 得出的是cȝ属性,值是不固定的Q根据对象来定
fieldy.get(pt1);
改field需要调用fieldy.set(obj,value);
Methodc?br />
getMethod(name,parameterytpes) //cdQ方法参数类?/p>
//执行Ҏ用invoke(obj,parameters)
Method methodCharAt=String.class.getMethod("charAt",int.class)
//静态方?name=null
methodCharAt.invoke(str1,1);
1 // 创徏一个测试反的c?/span>
2 public class ReflectPoint {
3 private int x;
4 public int y;
5 public String str1 = " china " ;
6 public String str2 = " basketbool " ;
7 public String str3 = " itcast " ;
8 public ReflectPoint( int x, int y) {
9 super ();
10 this .x = x;
11 this .y = y;
12 }
13
14 }
15
1 // 反射试
2 import java.lang.reflect. * ;
3 public class ReflectTest {
4
5 /** 反射基本知识
6 *
7 * 反射是把javacM的各个成分映ؓjavac?br />
8 * 反射的基是Classc,它是javacȝ描述c,Classcȝ实例是某个类的字节码
9 * @param args
10 */
11 public static void main(String[] args) throws Exception {
12 // TODO Auto-generated method stub
13 // Class试
14 // Class的实例是cȝ字节?/span>
15 String str1 = " abcde " ;
16
17 // 获取Class实例的三U方?/span>
18 Class cls1 = str1.getClass(); // 获取该对象类的字节码
19 Class cls2 = String. class ; // 直接调用cȝ字节?/span>
20 Class cls3 = Class.forName( " java.lang.String " ); // Ҏcd获取字节?/span>
21
22 System.out.println(cls1 == cls2); // true
23 System.out.println(cls1 == cls3); // true
24
25 // isPrimitive()是否是基本数据类型的字节?nbsp;8U?void
26 System.out.println(cls1.isPrimitive()); // false
27 System.out.println(Integer. class .isPrimitive()); // false
28 System.out.println( int . class .isPrimitive()); // true
29
30 // Integer.TYPE帔R 代表包装的基本类?nbsp;?nbsp;字节?/span>
31 System.out.println( int . class == Integer.TYPE); // true
32
33 // 数组不是基本数据cdQ是对象
34 System.out.println( int []. class .isPrimitive()); // false
35 System.out.println( int []. class .isArray()); // true
36
37 // 得到Stringcȝ String(StringBuffer)构造方?/span>
38 Constructor constructor = String. class .getConstructor(StringBuffer. class );
39 // 可以构造方法,创徏实例 Qƈq行cd转换
40 String str2 = (String)constructor.newInstance( new StringBuffer( " abcsd " ));
41
42 ReflectPoint pt1 = new ReflectPoint( 3 , 5 );
43
44 // Fieldc?/span>
45 Field fieldy = pt1.getClass().getField( " y " );
46 // fieldy的值是5吗? 不是Qfieldy不代表具体的|只代表类的y变量
47 // 因ؓ取的是类的字节码Q类中变量的值是Ҏ 对象来决定的
48 // 通过.get(object) 来获取某个对象的?/span>
49 System.out.println(fieldy.get(pt1));
50
51 Field fieldx = pt1.getClass().getDeclaredField( " x " ); // 获取声明q的属?/span>
52 fieldx.setAccessible( true ); // 讉K权限讄为true
53 System.out.println(fieldx.get(pt1));
54
55 changeString(pt1);
56 System.out.println(pt1.str1);
57 System.out.println(pt1.str2);
58 System.out.println(pt1.str3);
59
60 // Method
61 // 取得cd节码 中的charAtҎ
62 Method methodCharAt = String. class .getMethod( " charAt " , int . class );
63 // 调用invoke(obj,parameter) 执行Ҏ
64 System.out.println(methodCharAt.invoke(str1, 1 ));
65
66
67 // 调用main()Ҏ,普通方?br />
68 // TestArguments.main(new String[]{"abc","1111"});
69 // 使用反射调用Qؓ了解冻Il定cdQ而不知道实现代码的情况下?br />
70 // 从类字节?nbsp;得到 mainҎ
71 Method methodmain = Class.forName(args[ 0 ]).getMethod( " main " ,String[]. class );
72 methodmain.invoke( null , new Object[]{ new String[]{ " abc " , " 1111 " }});
73
74 // 数组的反?/span>
75 int [] a1 = new int [ 3 ];
76 int [] a2 = new int [ 4 ];
77 int [][] a3 = new int [ 2 ][ 3 ];
78 String[] a4 = new String[ 2 ];
79 // 数组的字节码比较Q看的是数组cd和数l维?/span>
80 System.out.println(a1.getClass() == a2.getClass()); // true
81 System.out.println(a1.getClass() == a3.getClass()); // false
82 System.out.println(a1.getClass() == a4.getClass()); // false
83 System.out.println(a3.getClass() == a4.getClass()); // false
84 // 获取父类
85 System.out.println(a1.getClass().getName());
86 System.out.println(a1.getClass().getSuperclass().getName());
87 System.out.println(a4.getClass().getSuperclass().getName());
88 // 得出父类都是Objectc?/span>
89 Object aobj1 = a1;
90 Object aobj2 = a2;
91 Object aobj4 = a4;
92 // Object[] aobj5=a1; // 里面装的是基本类型,基本cd不是对象Q不能装?/span>
93 Object[] aobj5 = a3;
94
95 int [] t1 = new int []{ 12 , 34 , 2 };
96 printObject(t1); // 打印对象Q数l是对象Q但t1数组元素是基本类?/span>
97 printObject( " xyz " ); // 打印对象
98
99 }
100
101 private static void printObject(Object obj){
102 Class clazz = obj.getClass(); // 获取该对象类的字节码
103 if (clazz.isArray()){
104 // Array是数l的反射c?/span>
105 for ( int i = 0 ;i < Array.getLength(obj);i ++ ){
106 System.out.println(Array.get(obj, i));
107 }
108 } else {
109 System.out.println(obj);
110 }
111 }
112
113 private static void changeString(Object obj){
114 Field[] fields = obj.getClass().getFields();
115 for (Field field:fields){
116 if (field.getType() == String. class ){ // 同一份字节码 ?=
117 try {
118 String newValue = ((String)field.get(obj)).replace( ' a ' , ' b ' );
119 field.set(obj, newValue);
120 } catch (Exception e) {
121 // TODO: handle exception
122 }
123 }
124 }
125 }
126 }
]]>枚D http://www.aygfsteel.com/zk0000/articles/341894.html杰点 杰点 Wed, 29 Dec 2010 05:15:00 GMT http://www.aygfsteel.com/zk0000/articles/341894.html http://www.aygfsteel.com/zk0000/comments/341894.html http://www.aygfsteel.com/zk0000/articles/341894.html#Feedback 0 http://www.aygfsteel.com/zk0000/comments/commentRss/341894.html http://www.aygfsteel.com/zk0000/services/trackbacks/341894.html 1 // 枚Dl合试
2 public class EnumTest {
3
4 /**
5 * @param args
6 */
7 public static void main(String[] args) {
8 // TODO Auto-generated method stub
9 WeekDay1 Mon = WeekDay1.MON; // 实例?/span>
10 System.out.println(Mon.nextday()); // 调用枚Dcd的方?/span>
11 WeekDay weekday2 = WeekDay.FRI;
12 System.out.println(weekday2);
13 System.out.println(weekday2.name()); // 昄枚D元素名称
14 System.out.println(weekday2.ordinal()); // 昄枚D元素索引
15 System.out.println(WeekDay.valueOf( " SUN " )); // Ҏ字符串得到枚丑֯?br />
16 // WeekDay.values() q回枚D元素数组
17 System.out.println(WeekDay.values().length);
18
19 System.out.println(TrafficLamp.valueOf( " RED " ).nextlamp());
20 }
21
22 public enum WeekDay{
23 SUN( 1 ),MON,TUE,WED,THI,FRI,SAT; // 枚D元素必须位于W一?/span>
24 /* 枚D的构造方法必ȝ?/span>*/
25 private WeekDay(){System.out.println( " first " );}
26 private WeekDay( int day){System.out.println( " second " );}
27 }
28
29 public enum TrafficLamp{
30 /*
31 * 带有抽象Ҏ的枚?br />
32 * 每个元素都是枚D的子c?br />
33 * 在枚丑օ素{}?nbsp;实现抽象Ҏ
34 *
35 * 只有一个元素的枚DQ可以实现单例模?br />
36 * */
37 RED( 30 ){
38 public TrafficLamp nextlamp(){
39 return GREEN;
40 }
41 },GREEN( 45 ){
42 public TrafficLamp nextlamp(){
43 return YELLOW;
44 }
45 },YELLOW( 5 ){
46 public TrafficLamp nextlamp(){
47 return RED;
48 }
49 };
50
51 public abstract TrafficLamp nextlamp();
52 private int time;
53 private TrafficLamp( int time){ this .time = time;}
54 }
55
56 }
57
1 // 模拟枚DQ枚举是一U特D的c,每个元素都是枚D的实例对象,枚D可以有自q属性和Ҏ
2 public abstract class WeekDay1 {
3 private WeekDay1(){} // U有化构造函敎ͼ则外界无法实例化
4
5 public final static WeekDay1 MON = new WeekDay1(){
6
7 @Override
8 public WeekDay1 nextday() {
9 // TODO Auto-generated method stub
10 return SUN;
11 }
12 // WeekDay的子c,要实现抽象方?/span>
13 };
14 // 定义成员
15 public final static WeekDay1 SUN = new WeekDay1(){
16 @Override
17 public WeekDay1 nextday(){
18 return MON;
19 }
20 };
21
22 public abstract WeekDay1 nextday(); // 定义一个抽象方?/span>
23 /* public WeekDay nexday(){
24 this是指该对象,因ؓcȝ非静态方法只能有对象来调?br />
25 *
26 if(this==MON){
27 return SUN;
28 }else{
29 return MON;
30 }
31 } */
32
33 public String toString(){
34 return this == MON ? " MON " : " SUN " ;
35 }
36
37 }
38
]]> 基本数据cd的装和拆箱 http://www.aygfsteel.com/zk0000/articles/341891.html杰点 杰点 Wed, 29 Dec 2010 05:08:00 GMT http://www.aygfsteel.com/zk0000/articles/341891.html http://www.aygfsteel.com/zk0000/comments/341891.html http://www.aygfsteel.com/zk0000/articles/341891.html#Feedback 0 http://www.aygfsteel.com/zk0000/comments/commentRss/341891.html http://www.aygfsteel.com/zk0000/services/trackbacks/341891.html
1 // 装箱 拆箱 享元模式
2 public class flyWeight {
3
4 public static void main(String[] args){
5 Integer i = 1127 ; // 装箱 基本数据类型{换ؓ对象
6 System.out.println(i + 3 ); // 拆箱 对象{换ؓ基本cd
7
8 Integer i2 = 127 ;
9 Integer i3 = 127 ;
10 Integer i4 = 128 ;
11 Integer i5 = 128 ;
12 System.out.println(i2 == i3); // true 享元模式
13 System.out.println(i4 == i5); // false 享元模式 小的数?nbsp;-128?27 ׃n一个对?/span>
14
15 String str1 = " abcds " ;
16 String str2 = " abcds " ;
17 System.out.println(str1 == str2); // true 享元模式
18 }
19 }
20
]]> TCP通信 http://www.aygfsteel.com/zk0000/articles/341889.html杰点 杰点 Wed, 29 Dec 2010 04:46:00 GMT http://www.aygfsteel.com/zk0000/articles/341889.html http://www.aygfsteel.com/zk0000/comments/341889.html http://www.aygfsteel.com/zk0000/articles/341889.html#Feedback 0 http://www.aygfsteel.com/zk0000/comments/commentRss/341889.html http://www.aygfsteel.com/zk0000/services/trackbacks/341889.html ServerSocketc?
构造函?br />
public ServerSocket()
public ServerSocket(int port) //默认数量?0?br />
public ServerSocket(int port,int backlog)
public ServerSocket(int port,int backlog,
InetAddress bindAddr)
close()
accept() q回socket对象
Socketc?
构造方?br />
public Socket()
public Socket(String host,int port)
public Socket(InetAddress address,int port)
public Socket(String host,int port,
InetAddress localAddr,int localPort)
public Socket(InetAddress address,int port,
InetAddress localAddr,int localPort)
Ҏ
getInputStream() q回输入对?br />
getOutputStream()q回输出对?br />
1 // ZTCP的服务器?/span>
2 import java.net. * ;
3 import java.io. * ;
4 public class TcpServer {
5
6 /**
7 * @param args
8 */
9 public static void main(String[] args) throws Exception {
10 // TODO Auto-generated method stub
11 ServerSocket ss = new ServerSocket( 8001 ); // 创徏服务器socket对象
12 Socket s = ss.accept(); // {待客户端连接,q回socket对象
13 InputStream ips = s.getInputStream(); // q回 输入对?/span>
14 OutputStream ops = s.getOutputStream(); // 获得 输出对?/span>
15
16 ops.write( " welcome,jiedian's Server\n " .getBytes());
17
18 BufferedReader br = new BufferedReader( new InputStreamReader(ips));
19 // 包装|络输入,字节?/span>
20 System.out.println(br.readLine());
21 /*
22 byte[] buf=new byte[1024];
23 int length=ips.read(buf);
24 System.out.println(new String(buf,0,length));
25 */
26
27 br.close();
28 // ips.close();
29 ops.close();
30 s.close();
31 ss.close();
32 }
33
34 }
35
]]>
վ֩ģ壺
|
|
|
Ϸ |
|
п |
|
|
ʯ |
ƽ |
Ȩ |
â |
|
۩ |
|
|
ָɽ |
ݶ |
|
Ϫ |
ʯ |
|
|
|
|
|
|
ƽ |
|
|
|
|
® |
|
|
ٺ |
|
˶ |
ʼ |
ؿ˹ |
|