欢迎来到虫虫下载站 | 资源下载 资源专辑 关于我们
虫虫下载站

variable.java

PIY(Program It Yourself)是一个基于Java的应用程序开发环境
JAVA
字号:
package piy;

import java.io.Serializable;

/**
* Represents a single variable.  Note that we change the type to the wrapper type if it's
* primitive - serialization moans otherwise (don't know why).  The type is changed back
* though when getType() is called.
* @author David Vivash
* @version 1.0.1, 29/04/01
*/
public class Variable implements Serializable {

	private Class type = null;
	private Object value = null;

	private boolean converted = false;

	public Variable(Class type, Object value) {
		this.value = value;
		setType(type);
	}
	
	/**
	* Get the type of the variable stored 
	* @return the type as set by the call to setType(..)
	*/
	public Class getType() { 
		if (converted) {
			if (type == Integer.class)		return int.class;
			if (type == Boolean.class)		return boolean.class;	
			if (type == Byte.class)			return byte.class;	
			if (type == Character.class)	return char.class;	
			if (type == Short.class)		return short.class;	
			if (type == Long.class)			return long.class;	
			if (type == Float.class)		return float.class;	
			if (type == Double.class)		return double.class;	
			if (type == Void.class)			return void.class;	
		}		
		return type; 	
	}

	/**
	* Set the type of the value stored in this variable.  If the type is primitive, it
	* will be stored internally as the relevant wrapper, but a call to getType() will 
	* return the primitve equivalent.
	* @param type the type to set the value to
	*/	
	public void setType(Class type) { 
		this.type = type; 
		if (type.isPrimitive()) {
			if (type == int.class)		this.type = Integer.class;
			if (type == boolean.class)	this.type = Boolean.class;
			if (type == byte.class)		this.type = Byte.class;
			if (type == char.class)		this.type = Character.class;
			if (type == short.class)	this.type = Short.class;
			if (type == long.class)		this.type = Long.class;
			if (type == float.class)	this.type = Float.class;
			if (type == double.class)	this.type = Double.class;
			if (type == void.class)		this.type = Void.class;
			converted = true;
		} else {
			value = null; //to maintain the integrity of the value's type
			converted = false;
		}
	}

	public Object getValue() { return this.value; }
	public void setValue(Object value) { this.value = value; }

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -