2016/04/17

Source: Internet
Author: User

Package Com.wode;

Import Java.lang.reflect.Constructor;
Import Java.lang.reflect.Field;
Import Java.lang.reflect.Method;
Import Java.util.Random;

Import Javax.activation.FileDataSource;
/***
*
* @author Administrator
*random class math using methods and reflection mechanism
*/
public class Test04_16 {

public static void Main (string[] args) throws Exception {
The random class represents a randomly generated number
Random random = new random ();
System.out.println (Random.nextint ());
Random.nextint () does not give arguments in parentheses, so the number of int types is randomly generated and the range is in long
System.out.println (Random.nextint (10));
Random.nextint (10) indicates that 0-9 10 digits of the int type are randomly generated he is not covered after the bag
If you want to be random to 10 then the code should be: System.out.println (Random.nextint (10) +1);

---------------------------------------the split line June-------------------------------------------------------

How to use Math
The math class is a mathematical tool class that is commonly used in the Java language and can be called from the Math. Method Name ().
System.out.println (Math.Abs (-50));//Run Result: 50
Math.Abs represents the absolute value of the number in the output ()
System.out.println (Math.max (5,-9));//Run Result: 5
Math.max the maximum value in the output ()
System.out.println (Math.min (5,-9));//Operating Result: 9
Math.min the smallest value in the output ()
System.out.println (Math.Round (5.4));//Run Result: 5
Math.Round value after rounding in output ()
System.out.println (Math.floor (-8.1));//Operating Result: 9.0
Math.floor output () the maximum close to the positive infinity is rounded down if it is () 9.9 returns a value of 9 this is the downward rounding
System.out.println (Math.ceil (-8.9));//Operating Result: 8.0
Math.ceil indicates that rounding up if () is 8.1 The return value is 9.0 which is rounding up
System.out.println (Math.random ());
Math.random randomly generates a random value between 0-1 and is likely to produce a 0 chance very little attention () cannot be filled in parameters
If you want a random number between 0-10 code is: System.out.println (Math.random () *10);

---------------------------------------the split line June-------------------------------------------------------

The class loading mechanism is reflected by the ClassLoader: the reflection mechanism of Java is one of the characteristics of Java
Simple sentence: Reflection is the runtime exploration and use of the compiler unknown class

Dog dog = new Dog ();
Class Clazz = Dog.getclass ();//indicates that a specific instance of the class is obtained if you know that there is a specific instance to use GetClass ()
System.out.println (clazz);//Run Result: Class Com.wode.Dog know where he comes from and where he's from.
Class clazz1 = dog.class;//know the name of the class but no instance object
Class clazz2 = Class.forName ("Com.wode.Dog");//Dynamic pass-through during runtime use when you do not know the name of the instance and the class
Class.forName is likely to produce an exception in order for the code to look good I throw an exception and throw the parent class exception all exceptions so that when there is an exception, the exception is thrown again

Filed class: Provides all property information for a class or interface, even if it is private.
Field [] fields = Clazz2.getdeclaredfields ();//Get all of the dog's attributes and store them in an array
for (int i = 0; i < fields.length; i++) {//Use the loop method to print out all the properties in the Dog class
System.out.println (Fields[i]);
}
Filed get all the attributes in dog so how to modify?
for (Field field:fields) {//Loop out all properties
if (Field.getname () Equals ("Age")) {//finds The age attribute and modifies the Name property if you want to modify it, it must be an error because the Name property is protected and can only be seen and cannot be modified
Field.set (dog, 8);
System.out.println (Dog.age);
}
}

Method class: Get the methods inside the dog
Method [] methods = Clazz.getdeclaredmethods ();
Getdeclaredmethods to get all the methods in the dog and use the For loop output
for (int i = 0; i < methods.length; i++) {
System.out.println (Methods[i]);
}
How do I call the method after I get the method?
method = Clazz.getmethod ("show", String.class);
Using GetMethod to invoke methods
Method.invoke (dog, "Ah Wang");
Then use Method.invoke to use the method in parentheses, respectively, to instantiate the object and the property to be modified.

Constructor class: Getting the constructor in dog
Constructor Constructor = Clazz.getdeclaredconstructor (null);//Use Getdeclaredconstructor to get the parameterless constructor in dog
Constructor.newinstance (null);//Call an argument-free constructor
Constructor Constructor2 = Clazz.getconstructor (Int.class,string.class);
Constructor2.newinstance (5, "parameter");//Call the parameter constructor
Object o = clazz.newinstance (); Instantiating an object by using object because object is a parent class all will not have a strong turn
Dog dog2 = (dog) clazz.newinstance ();//Use Newinstauce to instantiate the object here is a downward transition so it involves a strong turn
Newinstauce can also instantiate objects, invoke constructors to instantiate objects, and Class.constructor provide newinstauce methods
NEW: Strong type is relatively efficient to invoke any class public class
Newinstauce: Weak type is inefficient and can only call constructors
}
}

Package Com.wode;
/***
*
* @author Administrator
* Create a Dog class
*/
public class Dog {

Private String Name;//private The idea of proprietary encapsulation
int age;
public void Show (String name) {
System.out.println (name+ ": Wang Bark" + "+" I: "+age+");
}
The appropriate get and set methods are provided below
Public String GetName () {
return name;
}
public void SetName (String name) {
THIS.name = name;
}
Public Dog () {
SYSTEM.OUT.PRINTLN ("No parameter constructor! ");
}

Public Dog (int age,string neme) {
Super ();
This.age = age;
THIS.name = Neme;
System.out.println ("Parametric constructor! ");
}
}

2016/04/17

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.