/* * 有泛型的情况 * 使得程序具有更好的可读性和安全性 */ private static void test1() throws Exception { // 代码更具有可读性. Generic<String> bc = new Generic(); String s = "OK"; File file = new File("e:/1.txt"); // 问题:在添加元素时,会检查错误,只可以向数组列表中添加指定类的对象. bc.add(s); // 由于加入的元素不是String类型,故报错. // bc.add(file);//error The method add(String) in the type Generic<String> is not applicable // for the arguments (File) /* * 不用强制转换 * 擦除get的返回类型后将返回Object类型.编译器自动插入String的强制类型转换. * 也就说,编译器把这个方法调用翻译为两条虚拟机指令: * 1.对原始方法get的调用. * 2.将返回的Object类型强制转换为String类型. */ String s1 = bc.get(0); }
/** * An <b>opaque</b> class representing members of its enclosing <b>opaque</b> type. * * <p>Note that, this class is <b>opaque</b> but its constructors are all <b>internal</b>. This * class and the subclasses of this class are used to declare either <b>opaque</b> <tt>public</tt> * instance fields of the opaque type {@link js.Var.Member} or the <b>opaque</b> <tt>public</tt> * static fields of other <b>opaque</b> types while their constructors are used to define the * fields inside <b>opaque</b> classes. Under either circumstance, the field names must be exactly * same as the member names, as the <b>opaque</b> fields of <b>opaque</b> types are resolved by * re-compilers directly based on the field names. * * @author <a href="mailto:[email protected]">J.J.Liu (Jianjun Liu)</a> at <a * href="http://www.jscripter.org" target="_blank">http://www.jscripter.org</a> * @javascript <b>Opaque</b> types can be resolved but no class objects for them can be created in * the target codes. Re-compilers must exit with error on operations accessing that kind of * class objects. Re-compilers must resolve an <b>opaque</b> instance field declared by this * class in {@link js.Var.Member} or its subclasses to the JavaScript identifier: * <pre>q.m</pre> * where <tt>m</tt> is the identifier of the field name and <tt>q</tt> is the identifier * resolved from the instance of the enclosing member. Re-compilers must resolve an * <b>opaque</b> static field declared by this class in <b>opaque</b> types other than {@link * js.Var.Member} and its subclasses to the JavaScript identifier: * <pre>m</pre> * where <tt>m</tt> is the identifier of the field name. And re-compilers must report error on * the access to <b>opaque</b> fields declared by this class under any other circumstances. */ public static class Member extends JsGlobal.RangeError.Prototype.Member { /** * Internally constructs a member based on a qualifying member. * * <p>This constructor is <b>internal</b> and only called inside of <b>opaque</b> or * <b>internal</b> classes or class members. * * <p>Note that, this constructor is <b>internal</b> but its declaring class is <b>opaque</b>. * This constructor is used to define <b>opaque</b> instance fields declared in the declaring * class of this constructor itself or its subclasses. Under this circumstance, the field names * must be exactly same as the member names, as the <b>opaque</b> instance fields of the * <b>opaque</b> type {@link js.Var.Member} or its subclasses are resolved by re-compilers * directly to their names appending to the name resolved from the specified qualifying member * with a dot in between. * * @param q A qualifying member * @param mid The ID of the member to construct * @since 1.0 * @javascript Re-compilers must report error on the invocation to an <b>internal</b> * constructor. */ public Member(JsObject.Member q, Mid mid) { super(q, mid); } /** * Internally constructs a member without a qualifying member. * * <p>This constructor is <b>internal</b> and only called inside of <b>opaque</b> or * <b>internal</b> classes or class members. * * <p>Note that, this constructor is <b>internal</b> but its declaring class is <b>opaque</b>. * This constructor is used to define <b>opaque</b> static fields, declared in <b>opaque</b> * types other than the declaring class of this constructor itself and its subclasses. Under * this circumstance, the field names must be exactly same as the member names, as the * <b>opaque</b> static fields of <b>opaque</b> types are generally resolved by re-compilers * directly to identifiers of their names. * * @param mid The ID of the member to construct * @since 1.0 * @javascript Re-compilers must report error on the invocation to an <b>internal</b> * constructor. */ public Member(Mid mid) { super(mid); } /** * Evaluates the property, represented by the current member instance, of the argument object. * * @param o The argument object * @return The value of the current member based on the object argument. * @since 1.0 * @javascript Re-compilers must convert the instance invocation of this method into the * JavaScript expression: * <pre>o.m</pre> * where <tt>m</tt> is the identifier name resolved from the current member instance of the * invocation. */ @Override public JsRangeError with(ObjectLike o) { return new JsRangeError(super.with(o)); } /** * An <b>opaque</b> instance field defining a sub-member that is named by the name of this * field, qualified by the current member instance of the field, and to access the property of * the name on an object. * * <p>The <tt>constructor</tt> property of an object is a reference to the function that was the * constructor for that object. * * <p>A common use of the <tt>constructor</tt> property is to determine the type of unknown * objects. Given an unknown value, you can use the {@link js.Js#typeof(Object)} operation to * determine whether it is a primitive value or an object. If it is an object, you can use the * <tt>constructor</tt> property to determine what type of object it is. * * <p>Note, however, that while this technique works for the objects built into core JavaScript, * it is not guaranteed to work with host objects such as the <tt>window</tt> object of * client-side JavaScript. The default implementation of the <tt>toString()</tt> method provides * another way to determine the type of an unknown object. * * @since 1.0 * @javascript Re-compilers must resolve the member of this instance field to the identifier of * the field name appending to the identifier resolved from its qualifying member with a dot * in between. */ public final JsFunction.Member<JsRangeError> constructor = new JsFunction.Member<JsRangeError>( this, Members.constructor, Generic.get(JsRangeError.class)); }
/** * An <b>opaque</b> class representing JavaScript <tt>RangeError</tt>. * * <p>An instance of the this class is thrown when a numeric value is not in its legal range. For * example, setting the length of an array to a negative number causes a <tt>RangeError</tt> to be * thrown. * * <p>In JS, to throw a value or object as a <tt>RuntimeException</tt>, see {@link * js.Js#err(Object)}. * * @author <a href="mailto:[email protected]">J.J.Liu (Jianjun Liu)</a> at <a * href="http://www.jscripter.org" target="_blank">http://www.jscripter.org</a> * @see js.Js#err(Object) * @javascript <b>Opaque</b> types can be resolved but no class objects for them can be generated * into the target codes. Re-compilers must exit with error on the operations of accessing that * kind of class objects. The <tt>checkcast</tt> operation to the class literal of this * interface must be ignored and <tt>instanceof</tt> to it always <tt>true</tt>. */ public class JsRangeError extends JsGlobal.RangeError.Prototype { /** * An <b>opaque</b> class representing members of its enclosing <b>opaque</b> type. * * <p>Note that, this class is <b>opaque</b> but its constructors are all <b>internal</b>. This * class and the subclasses of this class are used to declare either <b>opaque</b> <tt>public</tt> * instance fields of the opaque type {@link js.Var.Member} or the <b>opaque</b> <tt>public</tt> * static fields of other <b>opaque</b> types while their constructors are used to define the * fields inside <b>opaque</b> classes. Under either circumstance, the field names must be exactly * same as the member names, as the <b>opaque</b> fields of <b>opaque</b> types are resolved by * re-compilers directly based on the field names. * * @author <a href="mailto:[email protected]">J.J.Liu (Jianjun Liu)</a> at <a * href="http://www.jscripter.org" target="_blank">http://www.jscripter.org</a> * @javascript <b>Opaque</b> types can be resolved but no class objects for them can be created in * the target codes. Re-compilers must exit with error on operations accessing that kind of * class objects. Re-compilers must resolve an <b>opaque</b> instance field declared by this * class in {@link js.Var.Member} or its subclasses to the JavaScript identifier: * <pre>q.m</pre> * where <tt>m</tt> is the identifier of the field name and <tt>q</tt> is the identifier * resolved from the instance of the enclosing member. Re-compilers must resolve an * <b>opaque</b> static field declared by this class in <b>opaque</b> types other than {@link * js.Var.Member} and its subclasses to the JavaScript identifier: * <pre>m</pre> * where <tt>m</tt> is the identifier of the field name. And re-compilers must report error on * the access to <b>opaque</b> fields declared by this class under any other circumstances. */ public static class Member extends JsGlobal.RangeError.Prototype.Member { /** * Internally constructs a member based on a qualifying member. * * <p>This constructor is <b>internal</b> and only called inside of <b>opaque</b> or * <b>internal</b> classes or class members. * * <p>Note that, this constructor is <b>internal</b> but its declaring class is <b>opaque</b>. * This constructor is used to define <b>opaque</b> instance fields declared in the declaring * class of this constructor itself or its subclasses. Under this circumstance, the field names * must be exactly same as the member names, as the <b>opaque</b> instance fields of the * <b>opaque</b> type {@link js.Var.Member} or its subclasses are resolved by re-compilers * directly to their names appending to the name resolved from the specified qualifying member * with a dot in between. * * @param q A qualifying member * @param mid The ID of the member to construct * @since 1.0 * @javascript Re-compilers must report error on the invocation to an <b>internal</b> * constructor. */ public Member(JsObject.Member q, Mid mid) { super(q, mid); } /** * Internally constructs a member without a qualifying member. * * <p>This constructor is <b>internal</b> and only called inside of <b>opaque</b> or * <b>internal</b> classes or class members. * * <p>Note that, this constructor is <b>internal</b> but its declaring class is <b>opaque</b>. * This constructor is used to define <b>opaque</b> static fields, declared in <b>opaque</b> * types other than the declaring class of this constructor itself and its subclasses. Under * this circumstance, the field names must be exactly same as the member names, as the * <b>opaque</b> static fields of <b>opaque</b> types are generally resolved by re-compilers * directly to identifiers of their names. * * @param mid The ID of the member to construct * @since 1.0 * @javascript Re-compilers must report error on the invocation to an <b>internal</b> * constructor. */ public Member(Mid mid) { super(mid); } /** * Evaluates the property, represented by the current member instance, of the argument object. * * @param o The argument object * @return The value of the current member based on the object argument. * @since 1.0 * @javascript Re-compilers must convert the instance invocation of this method into the * JavaScript expression: * <pre>o.m</pre> * where <tt>m</tt> is the identifier name resolved from the current member instance of the * invocation. */ @Override public JsRangeError with(ObjectLike o) { return new JsRangeError(super.with(o)); } /** * An <b>opaque</b> instance field defining a sub-member that is named by the name of this * field, qualified by the current member instance of the field, and to access the property of * the name on an object. * * <p>The <tt>constructor</tt> property of an object is a reference to the function that was the * constructor for that object. * * <p>A common use of the <tt>constructor</tt> property is to determine the type of unknown * objects. Given an unknown value, you can use the {@link js.Js#typeof(Object)} operation to * determine whether it is a primitive value or an object. If it is an object, you can use the * <tt>constructor</tt> property to determine what type of object it is. * * <p>Note, however, that while this technique works for the objects built into core JavaScript, * it is not guaranteed to work with host objects such as the <tt>window</tt> object of * client-side JavaScript. The default implementation of the <tt>toString()</tt> method provides * another way to determine the type of an unknown object. * * @since 1.0 * @javascript Re-compilers must resolve the member of this instance field to the identifier of * the field name appending to the identifier resolved from its qualifying member with a dot * in between. */ public final JsFunction.Member<JsRangeError> constructor = new JsFunction.Member<JsRangeError>( this, Members.constructor, Generic.get(JsRangeError.class)); } /** * Casts an <b>opaque</b> object to the current <b>opaque</b> type by wrapping it with the * wrapping constructor. * * @param var The argument of an <b>opaque</b> object. * @since 1.0 * @javascript Re-compilers must ignore the construction operation of this constructor, that is, * replacing it with its only argument. */ public JsRangeError(JsObject var) { super(var); } /** * An <b>opaque</b> static field defining a member that is named by the field name without a * qualifying member and to access the property of the name on an object. * * <p>The <tt>constructor</tt> property of an object is a reference to the function that was the * constructor for that object. * * <p>A common use of the <tt>constructor</tt> property is to determine the type of unknown * objects. Given an unknown value, you can use the {@link js.Js#typeof(Object)} operation to * determine whether it is a primitive value or an object. If it is an object, you can use the * <tt>constructor</tt> property to determine what type of object it is. * * <p>Note, however, that while this technique works for the objects built into core JavaScript, * it is not guaranteed to work with host objects such as the <tt>window</tt> object of * client-side JavaScript. The default implementation of the <tt>toString()</tt> method provides * another way to determine the type of an unknown object. * * @since 1.0 * @javascript Re-compilers must resolve the member of this static field to the identifier of the * field name. */ public static final JsFunction.Member<JsRangeError> constructor = new JsFunction.Member<JsRangeError>(Members.constructor, Generic.get(JsRangeError.class)); /** * Returns the primitive value associated with the current instance, if there is one. This * invocation simply returns the instance itself for the current instance is an object and there * is no primitive value for it. * * @return The current object itself. * @since 1.0 * @javascript Re-compilers must convert the instance invocation of this method directly into a * JavaScript invocation on its current object instance without changing the method name, but * expanding variable arguments, if any, into comma-separated values. */ @Override public JsRangeError valueOf() { return new JsRangeError((JsObject) var().valueOf()); } }