/
Class.java
112 lines (90 loc) · 3.07 KB
/
Class.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
/* Class.java
Purpose:
Description:
History:
Tue Dec 15 16:59:23 TST 2009, Created by tomyeh
Copyright (C) 2009 Potix Corporation. All Rights Reserved.
*/
package zk;
/**
Represents a class. It is the metainfo about a class. The class of each object can be accessed by zk.Object#$class.
All static members can be retrieve from this class.
For example,
<pre><code>
MyClass = zk.$extends{zk.Object, {
},{
find: function (name) {
}
});
foo = new MyClass();
</code></pre>
where the following two statement are equivalent.
<pre><code>
foo.$class.find('abc');
MyClass.find('abc');
</code></pre>
<h3>Like and Unlike Java</h3>
<p>Like Java, all classes objects have #isAssignableFrom and #isInstance. Unlike Java, all static methods are available as a method of the class object, including the superclass's static methods.
<pre><code>
MyClass = zk.$extends(zk.Object, {}, {
static0: function () {}
});
MyDerive = zk.$extends(zk.Object, {}, {
static1: function () {}
});
MyDerive.static0(); //OK
MyDerive.static1(); //OK
</code></pre>
<p>Unlike Java, you cannot access the static methods by an object. Rather, you have to go thru the class object
<pre><code>
var md = new MyDerive();
md.static0(); //Fail
md.static1(); //Fail
md.$class.static0(); //OK
MyDerive.static0(); //OK
</code></pre>
<p>Unlike Java, the class can by accessed directly, such as o.$instanceof(MyClass). In addition, the class objects are not instances of a particular class (Class in Java).
<pre><code>
o.$class.$instanceof(zk.Class); //wrong! no zk.Class
</code></pre>
<p>Unlike Java, if a static method of the class has the same name of a static method of the superclass, it 'overrides' it.
<pre><code>
MyClass = zk.$extends(zk.Object, {}, {
static0: function () {}
});
MyDerive = zk.$extends(zk.Object, {}, {
static0: function () {}
});
var mc = new MyClass(), md = new MyDerive();
mc.static0(); //invoke MyClass.static0
md.static0(); //invoke MyDerive.static0
</code></pre>
In additions, the static members are placed in different scope from the non-static members. Thus, it is OK that a static member has the same name as a non-static member, though it is not a good practice (due to confusion to users).
*
* <p>Like Java's <code>Class.forName</code>, you can use {@link _global_.zk#$import}
* to resolve the class from the class name.
*
* @author tomyeh
*/
public class Class extends zk.Object {
private Class() {}
/** Determines if the class by this Class object is either the same as, or is a superclass of, the class represented by the specified Class parameter.
* Example:
<pre><code>
if (klass1.isAssignableFrom(klass2)) {
}
</code></pre>
* @param klass the Class object to be checked
*/
public boolean isAssignableFrom(Class klass);
/** Determines if the specified Object is assignment-compatible with this Class. This method is equivalent to [[zk.Object#$instanceof].
* Example:
<pre><code>
if (klass.isInstance(obj)) {
}
</code></pre>
* @param obj the object to be checked
* @see #isAssignableFrom
*/
public boolean isInstance(Object obj);
}