[Java BASICS2] Iterable, Socket, Reflection, Proxy

Source: Internet
Author: User
Tags iterable

Parent interface of Collection:iterable interface

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

The interface has only one Iterable 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 does so, your class must implement the java.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.    }}

Iterator Applications:
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, programs running on the network. Socket classes is used to represent the connection between a client program and a server program. The java.net package provides, Classes--socket and serversocket--that implement the client side of the connection and T He server side of the connection, respectively.

Total: Client[port] <----->server[listening port, communication port],

Server side:serversocket serversocket = new ServerSocket (5000); Server'll 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 192.1.168.100

Socket sock = serversocket.accept (); Server would 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: If compile time is known class, it can be a.class. If runtime only knows class name, it can be Class.forName ("ClassName");

The Get class can then get all the information about this class, such as. GetMethods (),. GetConstructors (),. Getinterfaces (),. GetFields (),. Getannotations () ...

2, get constructor can instantiate object later:

Constructor Constructor=a.class.getconstructor (String.class);

A a= (a) constructor.newinstance ("stringargument");

3, get fields can later get or set this field in object:

Field Somefield = A.class.getfield ("FieldName"); The. GetField () method can only get public fields

A = new A ();

Object value = Somefield.get (a);

Somefield.set (A, value);

4,get method can be invoke after this method on some object:

method = A.class.getmethod ("MethodName", String.class); The. GetMethod () method can only get public methods

Object returnvalue = Method.invoke (New A (), "stringargument");

5, Get private Field/method

Field Privatefield = A.class.getdeclaredfield ("FieldName"); Or. Getdeclaredmethod ("MethodName");

Privatefield.setaccessible (TRUE);

Object fieldvalue = privatefield.get (New A ());

6, Get annotations

Annotations can be on class, or method, field, parameter ...

When defining your own @interface, @retention (retentionpolicy.runtime) indicates that this annotation is available at runtime.

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 () method

MyInterface proxy = (myinterface) proxy.newproxyinstance (MyInterface.class.getClassLoader (), new class[] { Myinterface.class}, handler);

Asd

[Java BASICS2] Iterable, Socket, Reflection, Proxy

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.