How can I create a new subclass extends a ButtonElement

亡梦爱人 提交于 2019-12-24 10:39:14

问题


  1. import dart:html , I can create a new subclass MyButton of ButtonElement with factory constructor ,and add some new function such as getButtonName(){} ...
  2. when it's running ,I get a instance

    MyButton btn=new MyButton()
    
  3. But the instance "btn" runtime type is still ButtonElement, and can't call the getButtonName() function. If I use btn as MyButton then I get this error

    Uncaught CastError: Casting value of type ButtonElement to incompatible type MyButton

Here is the code

    class MyButton extends ButtonElement {   
       factory MyButton(){
        return new ButtonElement();   
       }

       String getButtonName(){
        return "ButtonName";   
       } 
    }

回答1:


Just because this line

return new ButtonElement(); 

is within a factory constructor or MyButton doesn't mean it has any relation to MyButton it still returns new ButtonElement().

This question extendTag in Dart custom element shows how to create a custom element without Polymer.
See also this discussion https://groups.google.com/a/dartlang.org/forum/#!topic/misc/-z_8sVp_uPY



来源:https://stackoverflow.com/questions/28470066/how-can-i-create-a-new-subclass-extends-a-buttonelement

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!