[Java Basics2] Iterable, Socket, Reflection, Proxy
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了[Java Basics2] Iterable, Socket, Reflection, Proxy,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含3711字,纯文字阅读大概需要6分钟。
内容图文
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 = 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 = 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可以是在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
原文:http://www.cnblogs.com/chayu3/p/3938008.html
内容总结
以上是互联网集市为您收集整理的[Java Basics2] Iterable, Socket, Reflection, Proxy全部内容,希望文章能够帮你解决[Java Basics2] Iterable, Socket, Reflection, Proxy所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。