Javase Basic Note 13

Source: Internet
Author: User

13th Chapter Java Reflection mechanism


1. How to create an instance of class

1.1 Procedure: After the source file has been compiled (Javac.exe), one or more. class files are obtained. class file is run (Java.exe), it is necessary to load the classes (through the JVM's ClassLoader), to record the cache in memory, each of the. class files that are put into the cache is an instance of class

An object of 1.2Class, which corresponds to a runtime class, is the equivalent of a runtime class itself acting as an instance of class.

1.3java.long.class is the source of reflection. Classes that involve reflection are then under the Java.long.reflect child package.

1.4 How to instantiate class:

① calls the. Class property of the run-time class

Class clazz = Person.class

② calls its getclass () method through the object of the run-time class

Person p = new person;

Class clazz = P.getclass ();

③ calls the class's static method Forname (String className). This method is reported classnotfoundexception

String className = "person";

Class clazz = Class.forName (className);


2. What you can do after you have the class instance

2.1 Creating an object for the corresponding run-time class

① calling the parameterless constructor to create

Object obj = class.newinstance ();

Person P = (person) obj;

② call to specify constructor creation (with parameter, non-public--->declared)

Constructor cons = clazz.getdeclaredconstuctor (parameter type. class, formal parameter type. class, etc.);

Cons.setaccessible (TRUE);

Person P = (person) cons.newinstance ("Tom", 10);

2.2 Call query Gets the structure of the complete class of the corresponding runtime class: Properties, methods, constructors, packages, parent class, interfaces, generics, annotations, exceptions, inner classes

such as: method[] m1 = Clazz.getmethods (); Gets the method that has the public permission declared in the corresponding runtime class (contains the public method declared in the parent class)

method[] m2 = clazz.getdeclaredmethods (); Gets all the methods in the corresponding run-time class (without the parent class)


2.3 Calls the specified structure in the corresponding run-time class (a specified property, method, constructor)

① calling the specified property

Non-public properties:

Field f1 = Clazz.getdeclaredfield ("name");//property name

F1.setaccessible (TRUE);

F1.set (A, "Jerry");//Object Properties


Public Property:

Field F2 = Clazz.getfield ("Age");//property name

F2.set (a,9);//Object Properties


static property:

Field F3 = Clazz.getdeclaredfield ("desc");//property name

F3.get (null/object/class name. Class);

② calls the specified method:

Call the non-public method:

Method M1 = Clazz.getdeclaredmethod ("Getage");//Methods Name parameter type

M1.setaccessible (TRUE);

int age = (Integer) M1.invoke (P);//method return value type name Obj,args object parameter or None


Call the public method:

Method m2 = Clazz.getmethod ("show", string.class);//Methods Name parameter type

Object returnval = M2.invoke (P, "handsome");//object parameter return value for method


Call the static method:

Method m3 = Clazz.getdeclaredmethod ("Getage");//Methods Name parameter type

M3.setaccessible (TRUE);

M3.invoke (p/person.class/null);//object class NULL


3.java Dynamic Agent

Proxy mode design principle: Use a proxy to wrap the object, and then replace the original object with the proxy object, any calls to the original object through the proxy, the proxy object determines whether and when the method call goes to the

On the original object

Static proxy: Requires the proxy class and the proxy class to implement a corresponding set of interfaces, through the proxy class object calls the method of overriding the interface, is actually executed by the proxy class of the same method call.

Dynamic Proxy: When the program is running, it dynamically creates a proxy class based on the proxy class and its implemented interfaces, and initiates calls to the same method as the proxy class when invoking the abstract method of the implementation of the proxy class.

The technical points involved:

① provides an implementation class for the Invocationhandler interface and overrides its Invoke () method

②proxy.newinstance (Obj.getclass (). getClassLoader (), Obj.getclass (). Getinterfaces (), h);//obj: Proxied class object H: Implements the object of the Invocationhandler interface implementation class


Dynamic Agent and AOP


The 14th Chapter Network programming


1. What are the issues that need to be considered in order to achieve network transmission?

1.1 How can I locate a host on the network exactly?

1.2 How can reliable and efficient data transfer be performed?


2.Java How to achieve network communication

2.1 Use IP Address---Locate a host use port number---Locate an application ===>inetaddress class

How do I create an InetAddress object? Getbyname (* *); for example: inetaddress Inet = Inetaddress.getbyname (*192.168.10.165*);

How do I create a InetAddress object for this machine? Getlocalhost ()

Domain name: gethostname () ip:gethostaddress ()

2.2 Pairs Due Agreement:

OSI Reference Model: Application Layer Presentation Layer Session Layer Transport Layer Network layer Data Link layer physical layer

TCP/IP Reference Model: Application Layer Transport Layer Network layer Physical + data link Layer

For the Transport layer:

TCP protocol:

Before using the TCP protocol, a TCP connection must be established to form a transport data channel

It is reliable to use the "three-time handshake" method before transmission.

Two application processes for communication with the TCP protocol: client, server

Large amount of data transfer in connection

The transmission is complete, the established connection needs to be released, the efficiency is low

UDP protocol:

Encapsulates data, sources, and purposes into packets without establishing a connection

Limit the size of each datagram within 64K

It is unreliable due to the need for no connection

No need to release resources at the end of sending data, fast

Programming for TCP: Socket ServerSocket

Example:

1. The client sends content to the server and the server prints the content to the console.

2. The client sends the content to the server and the service side gives feedback.

3. The client sends the content to the server side, the server is saved locally, and returns "send successfully" to the client and closes the appropriate connection.


Programming for UDP: Datagramsocket datagrampacket


URL Programming: Uniform Resource Locator A URL object that corresponds to a resource on the Internet

This resource can be read ("Download") by invoking its corresponding method through the URL object.



This article from the "Ah Cheng Blog" blog, reproduced please contact the author!

Javase Basic Note 13

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.