簡體   English   中英

如何檢測屬性是否為haxe中的數字類型?

[英]How to detect if a property is numeric type in haxe?

我需要檢測對象的屬性是否為數字(Int或Float),我所擁有的是對象引用,屬性名稱為字符串

這是我的實現, compoent是對象引用,但是沒有用

public function IsNumeric():Bool
    {
        if (Std.is(Type.typeof(Reflect.getProperty(compoent, propertyName)), Int)) return true;
        if (Std.is(Type.typeof(Reflect.getProperty(compoent, propertyName)), Float)) return true;
        return false;
    }

有人可以幫忙嗎?

它比您嘗試的要簡單一些: Std.is( value, type )

class Test {
  static function main(){
    js.Lib.alert( Std.is("string", Int) );
    js.Lib.alert( Std.is(0, Int) );
    js.Lib.alert( Std.is(0.3, Int) );
    js.Lib.alert( Std.is("string", Float) );
    js.Lib.alert( Std.is(0, Float) );
    js.Lib.alert( Std.is(0.3, Float) );
  }
}

參見http://try.haxe.org/#6A9Bd

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM