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