Reflection and mathematical operator classes

Source: Internet
Author: User


1.API (connected to the previous article):Mathematical operator classes (random randomly class)
Generate random number, before packet
Math Class-Basic mathematical operations method:
(1) ABS () return absolute value
(2) Math.max (A, b) compare the size of a B value, output a larger value
(3) Math.min (A, b) compare the size of a, B value, and output a smaller value
(4) round () rounding
(5) Floor down rounding
(6) Ceil () rounding up
(7) random () returns a decimal number between 0 and 1
2. Reflection: (class loading mechanism classloder)
The runtime explores and uses classes that are not known at compile time
Reflection is the Operation class
The first way:
Dog D=new Dog ();
Class D2=d.getclass (); Specific instances Get class
The second way:
Class D3=dog.class (); Know the name of the class, but no instance object
The Third Way:
Class D4=class.forname ("package name. Class name"); Dynamic incoming at run time
Explore the basic information of the class: 1. Filed class: Getdeclaredfiled//Get Properties
Class 2.Method: Getdeclaredmethod//Get method
Class 3.Constructor: Getdeclaredconstructor//Get constructor
Filed:field[] fields= d4.getdeclaredfields ();//Get all the properties
For example://Get the property of the dog Class 1 and modify
/*class d3=class.forname ("Randontest.dog");
Dog d= (dog) d3.newinstance (); Instantiate an object using the instance method
Dog D2=new Dog (); Instantiate an object directly with the new method
Field[] fields= d3.getdeclaredfields ();//Get all the properties
for (Field field:fields) {
if (Field.getname (). Equals ("Age")) {
Field.set (d, "100");
}

}*/

————————————————————————————

Package randontest;

public class Dog {
int age=20;
String name= "Wangwanghao";
public void Speaking (int s) {
System.out.println ("Hello" + "+name");
}

public void saying () {
System.out.println ("Hello,say" +age);
}
}

————————————————————————————

Ways to get the dog class
Class d2=class.forname ("Randontest.dog");
Method[] Methods=d2.getmethods ();
for (Method method:methods) {
System.out.println (method);
System.out.println (Method.getreturntype ());
System.out.println (Method.getparametertypes ());
}
Dog d1= (dog) d2.newinstance ();

Method Method=d2.getmethod ("speaking", Int.class);
Method.invoke (d1, 6);

Reflection and mathematical operator classes

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.