`
wjjbfq
  • 浏览: 80563 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

java基础加强+枚举+可变参数+反射+内省

阅读更多

 Enum
1.不能有public  的构造方法,谨防外部调用者实例化。
2.枚举值用,隔开

3.Public static final
4.每个枚举值可以覆盖toString()
5.所有的枚举都是java.lang.Enum  的子类
6.每个枚举值可以看做它所在的枚举类型的实例(不是部分与整体的关系)
7.(高级)使用枚举是实现单列模式的一种优化方式。

package com.itcast;

public class EnumHomeTest {

	enum Director {
		east{
			public String toString(){
				return "you choose the east";
			}
		},
		south,
		north,
		west;
		private void Director(){
			
		}
		public void test(String str){
			System.out.println("测试枚举中的方法"+str);
		}
	}
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Director[] dirs=Director.values();
		//使用增强的for循环
		for(Director dir:dirs){
			System.out.print(dir.ordinal()+"\t");
			System.out.print(dir.name()+"\t");
			System.out.println(dir);
		}
		//调用单个枚举的值
		System.out.println(Director.valueOf(Director.east.name()));
		//调用枚举中的方法
		Director.south.test("wangjie");
	}

}

运行结果为

0	east	you choose the east
1	south	south
2	north	north
3	west	west
you choose the east
测试枚举中的方法wangjie

  

 

可变参数:
定义:参数的个数是可变的。
作用:减少重载方法的个数
注意:
1.  每个方法只能有一个可变类型,并且只能作为最后一个参数。

package com.itcast;

public class Person {

	public static void eat( String name,String...  strings){
		
		{
			System.out.println(strings.length);
			System.out.print(name);
			for(String str:strings){
				System.out.print("\t"+str);
			}
			System.out.println();
		}
	}
	
	
	public static void main(String[] args) {
		Person.eat("suhaiming");
		Person.eat("Hutao", "rice");
		Person.eat("lining", "rice","wheat");
		Person.eat("lining", "rice","wheat","water");

	}

}

 

运行结果为:

0
suhaiming
1
Hutao	rice
2
lining	rice	wheat
3
lining	rice	wheat	water

 

 

反射:
在运行阶段,动态获取对象的属性、方法、类型的一种机制。
reflect
Class
Field--------代表类中变量
Method------代表类中的方法

package com.itcast.reflect;

import java.lang.reflect.Field;
import java.lang.reflect.Method;

public class ReflectTest {

	public static void getEmployeeAllFieldNames(){
		Employee employee = new Employee();
		
		//get all the fields in the Employee获得所有声明的属性,不包括父类的
		Field[]fields = employee.getClass().getDeclaredFields();
		
		for(Field field:fields){
			//employee(object)-----Employee(class)-----Field[]--------field name
			String fieldName = field.getName();
			System.out.println("\t"+fieldName);
		}
		
		//以下代码获得所有的属性,包括父类的属性。
		Class tempClass = employee.getClass();
		do{
		   
		   Field[]superClassFields = tempClass.getDeclaredFields();	
		   for(Field field:superClassFields){
				//employee(object)-----Employee(class)-----Field[]--------field name
				String fieldName = field.getName();
				System.out.println("\t"+fieldName);
			}
		      tempClass = tempClass.getSuperclass();
		}while(tempClass != null);
		
	}
	
	
	public static void getEmployeeAllMethods(){
		//获得所有的方法
		Method[]methods = Employee.class.getDeclaredMethods();
		
		for(Method method:methods){
			System.out.println("\t"+method.getName());
		}
	}
	//测试获取父类的类型
	public static String getClassName(Object obj){
		Class clazz = obj.getClass();
		return clazz.getName();
	}
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		String str = "";
		System.out.println(getClassName(str));

		//getEmployeeAllFieldNames();
		
		//getEmployeeAllMethods();
	}

}

 

 

内省:
Introspector--------BeanInfo--------PropertyDescriptor---------属性的名称,类型等

 

package com.itcast.reflect;

import java.beans.BeanInfo;
import java.beans.IntrospectionException;
import java.beans.Introspector;
import java.beans.PropertyDescriptor;

public class IntrospectTest {

	
	public static void getAllPropertyName(){
		//Introspector ----BeanInfo-----PropertyDescriptor-------property name
		try {
			BeanInfo beanInfo = Introspector.getBeanInfo(Employee.class);
			
			PropertyDescriptor[]propertyDesccriptors = beanInfo.getPropertyDescriptors();
		
		    for(PropertyDescriptor propertyDescriptor:propertyDesccriptors){
		    	System.out.print(propertyDescriptor.getName()+"\t");
		    	System.out.println(propertyDescriptor.getPropertyType()+"\t");
		    	System.out.println(propertyDescriptor.getReadMethod());
		    	
		    }
		
		} catch (IntrospectionException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		
	}
	
	
	public static void main(String[] args) {
		getAllPropertyName();

	}

}

 运行结果:

name	class java.lang.String	
public java.lang.String com.itcast.reflect.Employee.getName()
salary	class java.lang.Float	
public java.lang.Float com.itcast.reflect.Employee.getSalary()

 其中 Employee.class 为javabean 在此省略。

 

获得beans中的属性和setter方法,调用setter方法:

PropertyDescripter[] ps = Introspector.getBeanInfo(xx.getClass()).getPropertyDescriptors();//获得属性描述
for(PropertyDescriptor properdesc : ps){
String propertyName = propertydesc.getName();//获取属性的名称
Method setter =  propertydesc.getWriterMethod();//获取属性的setter方法
//这里假设获取到一个String类型的值,而beans中的属性不是String类型的,所以就需要转换,转换需要使用commons-beanutils 包

//这里将xxxx转换为属性拥有的类型。
Object value = ConvertUtils.convert("xxxx",properdesc.getPropertyType());

setter.invoke(bean,value);//把对象注入到属性中
}

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics