简体   繁体   English

Ext继承-无法从基类/抽象类获取静态值

[英]Ext inheritance - unable to get a static value from a base/abstract class

I have here two classes, Shape and Rectangle - where Rectangle inherits from Shape. 我这里有两个类, ShapeRectangle -Rectangle从Shape继承。
Here is the parent class: 这是父类:

Ext.define('Shape', {
    id: 1,
    name: 'ShapeX',
    static: {
        drawnShapes: ['Shape1', 'shape2'],
        getDrawnShapesCount: function () {
            console.log('the number of drawn shapes is :' 
                       + this.drawnShapes.length );
        }
    },
    draw: function (newShape) {
        debugger;
        var newShape;
        this.static.drawnShapes.push(newShape);
        console.log( newShape 
                   + ' is drawn.... \n the number of drawnShapes is ' 
                   + this.static.drawnShapes.length 
                   + '" \n Shape class defined!' );
    }
});

This is my class which inherits from the parent: 这是我从父类继承的类:

Ext.define('Rectangle', {
    extend: 'Shape',
    draw: function (Arrlenght,base.newShape) {
        var Arrlenght = inheritableStatics.drawnShapes.length;
        alert(Arrlenght); // I need this array value?

        if (Arrlenght <= 10) {
            this.callParent();
        } else {
            console.log('Too many shapes drawn!');
        }
        console.log('Drawing a Rectangle...');
    }
});

My problem is in the Rectangle class inside the draw method - I want to get the length of the drawShapes array from the parent class - then if the length is <= 10 call the parent which will add the new shape, otherwise return the message: "Too many shapes drawn!" 我的问题是在draw方法内的Rectangle类中-我想从父类中获取drawShapes数组的长度-然后,如果长度<= 10请调用父类,它将添加新形状,否则返回消息: “绘制的形状太多了!” .

How do I reference the static array? 如何引用静态数组?

Two mistakes to start with: 首先要犯两个错误:

  • static should be statics (plural) - not that you need it because... static应该是statics (复数)-不是您需要它,因为...
  • inheritableStatics is a configuration property rather than a run-time accessor which should be defined on Shape if you want to utilise these variables in sub-classes. 如果要在子类中使用这些变量,则inheritableStatics是一个配置属性,而不是运行时访问器,应在Shape上定义它。
Ext.define('Shape', {
    // ...
    inheritableStatics: {
        drawnShapes: ['Shape1', 'Shape2']
    }
});

Then if you want to refer to static variables, you can use the self property which exists on every object instance - which is basically a reference to it's prototype / class: 然后,如果要引用静态变量,则可以使用每个对象实例上都存在的self属性-本质上是对其原型/类的引用:

Ext.define('Rectangle', {
    extend: 'Shape',
    // ...
    draw: function(/* args */){
        console.log(this.self.drawnShapes); // do stuff
    }
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM