Typeof
typeof是编程语言的一种运算符,用于确定变量的数据类型。这在程序结构可以接受多种数据类型且无需显式指出具体类型时非常有用。 在支持类型多态和类型转换的编程语言中,typeof运算符应用于对象时,有2种不同的语义。Visual Basic语言中typeof返回对象的动态类型。[1]即返回运行时类型信息而不考虑任何类型转换。在C#或D等语言,[2] or [3]以及C2x中,[4][5]typeof运算符返回操作数的静态类型。这些语言通常有其他方法可以获取动态类型信息,如typeid。 例子C语言的GNU扩展语法下,typeof用于定义一些宏: #define max(a,b) ({ typeof (a) _a = (a); typeof (b) _b = (b); _a > _b ? _a : _b; })
C++中的typeid是由C++标准库提供,定义于<typeinfo>头文件,用于判断某个变量的类型。typeof由编译器提供(目前仅gcc编译器支持),用于返回某个变量或表达式的类型。C++11标准新增的decltype是typeof的升级版本。 在C#: // Given an object, returns if it is an integer.
// The "is" operator can be also used to determine this.
public static bool IsInteger(object o) {
return ( o.GetType() == typeof(int) );
}
VB.NET语言中, C#的"typeof"对应于VB.NET的GetType方法。VB.NET的TypeOf关键字用于比较变量引用的对象与一个数据类型兼容。 Dim refInteger As Object = 2
MsgBox("TypeOf Object[Integer] Is Integer? " & TypeOf refInteger Is Integer)
MsgBox("TypeOf Object[Integer] Is Double? " & TypeOf refInteger Is Double)
Dim refForm As Object = New System.Windows.Forms.Form
MsgBox("TypeOf Object[Form] Is Form? " & TypeOf refForm Is System.Windows.Forms.Form)
MsgBox("TypeOf Object[Form] Is Label? " & TypeOf refForm Is System.Windows.Forms.Label)
MsgBox("TypeOf Object[Form] Is Control? " & TypeOf refForm Is System.Windows.Forms.Control)
MsgBox("TypeOf Object[Form] Is IComponent? " & TypeOf refForm Is System.ComponentModel.IComponent)
In JavaScript: function isNumber(n)
{
return ( typeof n === 'number' );
}
function (param: typeof existingObject) { ... }
let newObject: typeof existingObject;
参见参考文献
|
Index:
pl ar de en es fr it arz nl ja pt ceb sv uk vi war zh ru af ast az bg zh-min-nan bn be ca cs cy da et el eo eu fa gl ko hi hr id he ka la lv lt hu mk ms min no nn ce uz kk ro simple sk sl sr sh fi ta tt th tg azb tr ur zh-yue hy my ace als am an hyw ban bjn map-bms ba be-tarask bcl bpy bar bs br cv nv eml hif fo fy ga gd gu hak ha hsb io ig ilo ia ie os is jv kn ht ku ckb ky mrj lb lij li lmo mai mg ml zh-classical mr xmf mzn cdo mn nap new ne frr oc mhr or as pa pnb ps pms nds crh qu sa sah sco sq scn si sd szl su sw tl shn te bug vec vo wa wuu yi yo diq bat-smg zu lad kbd ang smn ab roa-rup frp arc gn av ay bh bi bo bxr cbk-zam co za dag ary se pdc dv dsb myv ext fur gv gag inh ki glk gan guw xal haw rw kbp pam csb kw km kv koi kg gom ks gcr lo lbe ltg lez nia ln jbo lg mt mi tw mwl mdf mnw nqo fj nah na nds-nl nrm nov om pi pag pap pfl pcd krc kaa ksh rm rue sm sat sc trv stq nso sn cu so srn kab roa-tara tet tpi to chr tum tk tyv udm ug vep fiu-vro vls wo xh zea ty ak bm ch ny ee ff got iu ik kl mad cr pih ami pwn pnt dz rmy rn sg st tn ss ti din chy ts kcg ve