java 高新技术 总结(一)



java 技术提升,复习,总结(一)

1:方法的多变参数 

看代码理解:

public static void main(String[] args) {
		System.out.println(sum(1,2,3));
		System.out.println(sumfor(1,2,3));
	}
	/**
	 * 多变参数
	 * @param i
	 * @param args
	 * @return
	 */
	public static int sum(int i ,int ...args){
		int sum = 0;
		for (int j = 0; j < args.length; j++) {
			sum+=args[i];
		}
		return sum;
	}
	/**
	 * 增强for循环
	 * @param i
	 * @param args
	 * @return
	 */
	public static int sumfor(int i ,int ...args){
		int sum = i;//注意此处
		for (int arg : args) {
			sum+=arg;
		}
		return sum;
	}

2:枚举的运用


	public static void main(String[] args) {
		
		System.out.println(WeekDays.SUN);
//		System.out.println(WeekDays.SUN.name());
//		System.out.println(WeekDays.SUN.name().toString());
//		System.out.println(WeekDays.SUN.ordinal());
//		System.out.println(WeekDays.valueOf("SUN").name());
//		System.out.println(WeekDays.values().length);
		
		
	}
	
	public enum WeekDays{
		
		SUN(1),MON(2,3),TUS,WED,THI,FRI,STA;
		private WeekDays() {
			System.out.println("frist");
			
		}
		private WeekDays(int day) {
			System.out.println("second");
		}
		private WeekDays(int day,int name) {
			System.out.println("third");
		}
		
	}
	/**
	 * 带抽象方法
	 * 有构造方法
	 * @author Administrator
	 *
	 */
	public enum Light{
		
		RED(20){

			@Override
			public Light getLight() {
				// TODO Auto-generated method stub
				return null;
			}
			
		},
		GREEN(40){

			@Override
			public Light getLight() {
				// TODO Auto-generated method stub
				return null;
			}
			
		};
		public abstract Light getLight();
		private int time;
		private Light(int time){
			this.time  = time;
		};
		
	}
3:反射;

/**
 * 反射辅助类
 * @author JiaRH
 *
 */
public class FieldReflect {

	private int x = 0;
	public int y = 0;
	
	public String aa1="bbbaaa";
	public String aa2="bbbaaa";
	public String aa3="bbbaaa";
	public FieldReflect(int x, int y) {
		super();
		this.x = x;
		this.y = y;
	}
	
	@Override
	public String toString() {
		// TODO Auto-generated method stub
		return aa1+"=="+aa2+"=="+aa3;
		
	}
}

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

public class Reflect {

	public static void main(String[] args) {
		String str1 = "abc";
		Class cl1 = str1.getClass();
		Class cl2 = String.class;
		try {
			/**
			 * 反射基本方法
			 */
			 Class cl3 = Class.forName("java.lang.String");
			 System.out.println(cl1==cl2);
			 System.out.println(cl1==cl3);
			 System.out.println(cl2==cl3);
			 System.out.println(int.class == Integer.TYPE );
			 System.out.println(cl1.isPrimitive());
			 System.out.println(cl2.isPrimitive());
			 System.out.println(cl3.isPrimitive());
			 System.out.println(Integer.class.isPrimitive());
			 System.out.println(int.class.isPrimitive());
			 /**
			  * 
			  * 获取 构造方法,并实例新的对象
			  */
			 Constructor constructor = String.class.getConstructor(StringBuffer.class);
			 String str2 = (String)constructor.newInstance(new StringBuffer("abc"));
			 System.out.println(str2.charAt(2));
			 /**
			  * 字段
			  * 获取成员变量的值
			  */
			 FieldReflect f1 = new FieldReflect(2, 5);
			 FieldReflect f2 = new FieldReflect(2, 6);
			 //获取成员变量对象
			 Field ff1 = f1.getClass().getField("y");
			 Field ff2 = f1.getClass().getField("y");
			 //取值
			 System.out.println(ff1.get(f1)+"");//注意获取值的方法
			 System.out.println(ff2.get(f2)+"");
			 
			 /**
			  * 获取私有的成员变量
			  * (暴力反射)
			  */
			 Field ff3 = f1.getClass().getDeclaredField("x");
			 ff3.setAccessible(true);
			 System.out.println(ff3.get(f1)+"");
			 
			 /**
			  * 利用反射改变值
			  * 将f1对象中所有的成员变量中含有的a变为b
			  */
			 Field[] fields = f1.getClass().getFields();
			 
			 for (Field field: fields) {
				 if (field.getType()==String.class) {
					 String priValue = (String) field.get(f1);
					 String newValue = priValue.replace('a', 'b');
					 field.set(f1, newValue);
					
				}
			}
			 System.out.println(f1);
			 
			 /**
			  * 获取方法
			  * 调用方法
			  */
			 //new String().charAt(index)
			 Method methodCharat=String.class.getMethod("charAt", int.class);
			 System.out.println(methodCharat.invoke(str2, 1));
			 //System.out.println(methodCharat.invoke(null, 1));//表明是一个静态方法
			 
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SecurityException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (NoSuchMethodException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IllegalArgumentException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (InstantiationException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (InvocationTargetException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (NoSuchFieldException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		
		
	}
}

利用反射调用其它类的main()方法


class TestArguments{
	public static void main(String[] args){
		for(String arg : args){
			System.out.println(arg);
		}
	}
}


//TestArguments.main(new String[]{"111","222","333"});
		String startingClassName = args[0];
		Method mainMethod = Class.forName(startingClassName).getMethod("main", String[].class);
		//mainMethod.invoke(null, new Object[]{new String[]{"111","222","333"}});
		mainMethod.invoke(null, (Object)new String[]{"111","222","333"});

关于更多反射请看 java 技术提升,复习,总结(二)

你可能感兴趣的:(java,技术,总结,学习)