Java basics enhanced 1

Source: Internet
Author: User

1. Enumeration
* Create an enumeration class
Public Enum grade {
A, B, C, D, E;
}
* Use an anonymous internal class
Enum grade {
A ("100-90 "){
Public String tolocalstring () {return "";}
}
}
* Private constructor
A ("100-90 ")
Private string value;
Private grade (string value ){
System. Out. println ("value" + value );
This. value = value;
}
Public String getvalue () {return value ;}

2. Traverse
* Normal Traversal
For (INT I = 0; I <list. Size (); I ++)
* Traversal in normal mode, using the iterator
Iterator <string> it = List. iterator ();
While (it. hasnext ())
* Use the enhanced for loop to use generics
For (string STR: List)
* Use an enhanced for loop without Using Generics
For (Object STR: List)
* Use an enhanced for loop. When the array is stored, the JavaBean
For (person: Persons)

3. variable parameters
Public static integer sum (integer... arrs)

4. Reflection
* call the class attribute of a class (no call method required, better performance)
class clazz = person. class;
* call the getclass () method of an object
person = new person ();
class clazz = person. getclass ();
* use the forname (classname) Static Method of class
class clazz = Class. forname ("CN. itcast. reflect. person ");
* Call method
class [] parametertypes = {Java. lang. string []. class };< br> method = clazz. getmethod ("Main", parametertypes);
string [] ARGs = {"AA", "BB" };< br> method. invoke (person, new object [] {ARGs});
* obtain a field
Field field = clazz. getdeclaredfield ("Grade");
* obtain the field value
field. get (person)
* set the field value
field. set (person, "N");
* Get object
Object OBJ = clazz. newinstance ();
person = (person) OBJ;
* Get constructor
class [] parametertypes = {Java. lang. string. class, Java. lang. integer. class };< br> constructor = clazz. getconstructor (parametertypes);

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.