`

Video-No.01 张孝祥_Java高新技术

 
阅读更多

1、静态导入(import static)

使用静态导入,可以使被导入的静态变量静态方法在当前类中直接使用,而无需用类名引用

import static java.lang.Math.*;
import static java.lang.System.*;
/**
 * 静态导入
 * @author Administrator
 *
 */
public class TestImportStaticApp {

	public static void main(String[] args) {
		//静态导入的使用
		out.println(max(4, 10));
		out.println(PI);
		System.out.println(Math.abs(4-7));
		
	}
}

 

2、可变参数

/**
 * 可变参数: ...
 * 以数组的形式存放
 * @author Administrator
 *
 */
public class VarableParameter {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		
		System.out.println(add(1, 4));
		System.out.println(add(1, 4, 3));
	}
	
	/**
	 * 可变参数,在JVM中默认以数组的形式存放
	 * @param args
	 * @return
	 */
	public static int add(int x, int... args) {
		int result = x;
		for(int tmp : args) {
			result += tmp;
		}
		return result;
	}

}

 

3、自动装箱、自动拆箱

/**
 * 自动装箱、拆箱
 * @author Administrator
 *
 * 享元模式:将多个小当对象,某个属性相同,则将他们设计成为一个对象,而不同的属性设计成为方法的参数,
 * 称之为对象的外部状态,相同的属性称之为对象的内部状态
 */
public class AutoBox {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		
		Integer i1 = 3; //自动装箱
		System.out.println(i1+10); //自动拆箱
		
		/**
		 * 当Integer取值范围(-128-127)之间的时候,
		 * Integer保存为相同的对象,其他的为创建新的对象
		 * 
		 */
		Integer i2 = 10;
		Integer i3 = 10;
		Integer i4 = 128;
		Integer i5 = 128;
		System.out.println(i2==i3);
		System.out.println(i4==i5);
		

	}

}

 4、枚举类型

      1)创建普通类模仿枚举类型

/**
 * 创建普通类来模仿枚举类型实现星期一到星期日
 * 将WeekDay设计为抽象类,并将nextDay设计成为抽象方法,则在创建每一个WeekDay时自动创建下一个WeekDay
 * @author Administrator
 *
 */
public abstract class WeekDay {
	
	//构造方法设计为私有,防止在其他类中被创建
	private WeekDay() {
		
	}
	
	public abstract WeekDay nextDay();
	
	//周一
	public static final WeekDay MON = new WeekDay() {

		@Override
		public WeekDay nextDay() {
			return TUE;
		}

		@Override
		public String toString() {
			return "MON";
		}
		
	};
	
	public static final WeekDay TUE = new WeekDay() {

		@Override
		public WeekDay nextDay() {
			// TODO Auto-generated method stub
			return WED;
		}
		
		@Override
		public String toString() {
			return "TUE";
		}
		
	};
	
	public static final WeekDay WED = new WeekDay() {

		@Override
		public WeekDay nextDay() {
			// TODO Auto-generated method stub
			return THU;
		}
		
		@Override
		public String toString() {
			return "WED";
		}
		
	};
	
	public static final WeekDay THU = new WeekDay() {

		@Override
		public WeekDay nextDay() {
			// TODO Auto-generated method stub
			return FRI;
		}
		
		@Override
		public String toString() {
			return "THU";
		}
		
	};
	
	public static final WeekDay FRI = new WeekDay() {

		@Override
		public WeekDay nextDay() {
			// TODO Auto-generated method stub
			return SAT;
		}
		
		@Override
		public String toString() {
			return "FRI";
		}
		
	};
	
	public static final WeekDay SAT = new WeekDay() {

		@Override
		public WeekDay nextDay() {
			// TODO Auto-generated method stub
			return SUN;
		}
		
		@Override
		public String toString() {
			return "SAT";
		}
	};
	
	public static final WeekDay SUN = new WeekDay() {

		@Override
		public WeekDay nextDay() {
			// TODO Auto-generated method stub
			return MON;
		}
		
		@Override
		public String toString() {
			return "SUN";
		}
		
	};
}

      2)枚举类型enum

 

/**
 * 创建枚举类型
 * 1) 枚举类型的元素必须放在最前面
 * 2) 枚举类型的构造方法必须为私有的
 * 3) 如果枚举尤其仅含有一个元素,则表示创建了一个单例类
 * @author Administrator
 *
 */
public class TestEnumApp {

	public static void main(String[] args) {
		WeekDay weekDay = WeekDay.SAT;
		System.out.println(weekDay.nextDay());
		weekDay = WeekDay.SUN;
		System.out.println(weekDay.nextDay());
		
		EnumWeekDay currWeekDay = EnumWeekDay.SUN;
		System.out.println(currWeekDay.nextDay());
		System.out.println(currWeekDay.name());
		System.out.println(currWeekDay.ordinal());
		System.out.println(EnumWeekDay.valueOf("WED").toString());
		System.out.println(EnumWeekDay.values().length);
	}
	
	public enum EnumWeekDay {
		
		//元素列表必须放在最前面
		MON, TUE(1), WED, THU, FRI, SAT, SUN;
		
		private int currDay;
		
		//枚举类型的构造方法必须为私有的
		private EnumWeekDay() {
			
		}
		
		private EnumWeekDay(int day) {
			currDay = day;
		}
		
		public EnumWeekDay nextDay() {
			EnumWeekDay result = null;
			switch(this) {
				case MON :
					result = TUE;
					break;
				case TUE :
					result = WED;
					break;
				case WED :
					result = THU;
					break;
				case THU :
					result = FRI;
					break;
				case FRI :
					result = SAT;
					break;
				case SAT :
					result = SUN;
					break;
				case SUN :
					result = MON;
					break;
			}
			
		return result;
		}
		
		
	}
	
	//枚举类型创建交通信号灯
	public enum TrafficLamp {
		
		RED(30) {

			@Override
			public TrafficLamp nextLamp() {
				return GREEN;
			}
			
		}, 
		
		GREEN(30) {

			@Override
			public TrafficLamp nextLamp() {
				// TODO Auto-generated method stub
				return YELLOW;
			}
			
		}, 
		YELLOW(5) {

			@Override
			public TrafficLamp nextLamp() {
				// TODO Auto-generated method stub
				return RED;
			}
			
		};
		
		//交通信号灯等待时间
		private int time;
		private TrafficLamp() {
			
		}
		
		private TrafficLamp(int time) {
			this.time = time;
		}
		
		//下一个信号灯类型
		public abstract TrafficLamp nextLamp();
	}
}

 5、反射机制

       1)

import java.sql.Date;


/**
 * 使用Class加载类
 * Class类:Java类的抽象共性,各个Java类都是属于同一个事物,描述这一类的名字为Class,Class类代表类的字节码
 * 类加载机制:
 * 加载类的过程:当需要某个类创建对象的时候,如果已经加载该类,则会在内存里面直接通过其字节码创建对象,
 * 				如果是初次加载,则在硬盘中拷贝转换为字节码存储在内存中,并通过字节码创建对象
 * @author Administrator
 *
 */
public class TestClassApp {

	public static void main(String[] args) throws ClassNotFoundException {
		//Class加载的三种方式
		Class cls1 = Date.class;
		Class cls2 = new Date(0).getClass();
		Class cls3 = Class.forName("java.lang.String");
		
		//类加载字节码在内存中只保留一份,通过该字节码实例可以创建多个对象
		String str1 = "abc";
		Class cls4 = str1.getClass();
		Class cls5 = String.class;
		Class cls6 = Class.forName("java.lang.String");
		
		System.out.println(cls4 == cls5); //true
		System.out.println(cls4 == cls6); //true

		/**
		 * 9个基本类型:int、char、float、double、long、short、boolean、void、byte
		 * 在源代码中出现的类,都包含class对象
		 */
		System.out.println(cls2.isPrimitive()); //判断是否为基本类型, false
		System.out.println(int.class.isPrimitive()); //true
		System.out.println(int.class == Integer.class); // false
		System.out.println(int.class == Integer.TYPE); //获取基本类型的字节码对象 true
		System.out.println(int[].class.isPrimitive()); //数组类型 false
		System.out.println(int[].class.isArray()); //判断是否为数组类型 true
	}
}

    2)

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics