首页 > 代码库 > [Java Basics2] Iterable, Socket, Reflection, Proxy

[Java Basics2] Iterable, Socket, Reflection, Proxy

Parent interface of Collection: Iterable Interface

A class that implements the Iterable can be used with the new for-loop. 

The Iterable interface has only one method:

public interface Iterable<T> {  public Iterator<T> iterator();    }

It is possible to use your own collection type classes with the new for-loop. To do so, your class must implement thejava.lang.Iterable<E> interface. Here is a very basic example:

public class MyCollection<E> implements Iterable<E>{    public Iterator<E> iterator() {        return new MyIterator<E>();    }}

And here is the corresponding implementation skeleton of the MyIterator class:

public class MyIterator <T> implements Iterator<T> {    public boolean hasNext() {            //implement...    }    public T next() {        //implement...;    }    public void remove() {        //implement... if supported.    }}

迭代器应用:
 list l = new ArrayList();
 l.add("aa");
 l.add("bb");
 l.add("cc");
 for (Iterator iter = l.iterator(); iter.hasNext();) {
  String str = (String)iter.next();
  System.out.println(str);
 }

Java sockets

A socket is one end-point of a two-way communication link between two programs running on the network. Socket classes are used to represent the connection between a client program and a server program. The java.net package provides two classes--Socket and ServerSocket--that implement the client side of the connection and the server side of the connection, respectively.

总: Client[port] <----->Server[listening port, communication port] ,

Server side: ServerSocket serverSocket = new ServerSocket(5000);  //Server will be listening to requests from clients on port 5000

Client side: Socket chatSocket = new Socket("192.1.168.100", 5000);  //The server is running on port 5000 at 192.1.168.100

Socket sock = serverSocket.accept();  //Server will return a new port to communicate with clients

InputStreamReader reader = new InputStreamReader(chatSocket.getInputStream());

BufferedReader breader = new BufferedReader(reader);

String msg = breader.readLine();

 

Reflection

1, Get Class: 如果compile time已知class,那可以A.class。如果runtime才知class name,那可以Class.forName("className");

Get Class以后就可以获取这个Class的所有信息,例如.getMethods(), .getConstructors(), .getInterfaces(), .getFields(), .getAnnotations()...

2, Get constructor以后可以Instantiate object:

Constructor constructor=A.class.getConstructor(String.class);

A a= (A) constructor.newInstance("StringArgument");

3, Get fields以后可以get 或set object里的这个field:

Field someField = A.class.getField("FieldName");  //.getField()方法只能获取public fields

A a = new A();

Object value = http://www.mamicode.com/someField.get(a);

someField.set(a, value);

4,Get method以后可以invoke这个method on some object:

Method method = A.class.getMethod("MethodName", String.class);  //.getMethod()方法只能获取public methods

Object returnValue = http://www.mamicode.com/method.invoke(new A(),"StringArgument");

5, Get private field/method

Field privateField = A.class.getDeclaredField("FieldName");  //or .getDeclaredMethod("MethodName");

privateField.setAccessible(true);

Object fieldValue = http://www.mamicode.com/privateField.get(new A());

6, Get annotations

Annotations可以是在class上,或者method, field, parameter上...

在定义自己的@interface的时候,如果@Retention(RetentionPolicy.RUNTIME)表示在runtime时可以获取这个annotation.

Annotation annotation = A.class.getAnnotation(MyAnnotation.class);

if(annotation instanceof MyAnnotation) {annotation.getName();...}

Proxy

To create dynamic implementations of interface at runtime with the help of reflection.

InvocationHandler handler = new MyInvocationHandler(); //implement .invoke()方法

MyInterface proxy = (MyInterface) Proxy.newProxyInstance(MyInterface.class.getClassLoader(), new Class[] {MyInterface.class}, handler);

asd

[Java Basics2] Iterable, Socket, Reflection, Proxy