Tengo una clase de padres así:
class SomeClass { public someProperty: number | undefined; constructor(value: number) { this.someProperty = value; } public on(eventType: string, fn: Function) { } } class Parent { protected static getTransform(value: number) { return value + 180; } public transform: SomeClass; constructor(value: number) { this.transform = this.createTransform(value); } protected createTransform(value: number) { const transform = new SomeClass(value); transform.on('rotate', this.rotate); return transform; } protected rotate(event: any) { this.transform.someProperty = Parent.getTransform(event.transform); } }Y necesito implementar una clase secundaria que tenga una lógica diferente de cálculo de propiedades de transformación. Necesito algo, así:
class Child extends Parent { protected static getTransform(value: number) { return value + 90; } constructor(value: number) { super(value); } }No he enfrentado ningún error u otra cosa, pero mi enfoque no funciona, nada cambió. ¿Cómo implementar lo mismo? Patio de juegos
... protegido (que permite el acceso desde la misma clase y sus subclases, pero no objetos de una clase diferente) ... Wiki: OOP
Para borrar el concepto de Member Visibility , aquí hay una demostración de su código (consulte los comentarios):
class SomeClass { public someProperty: number | undefined; constructor(value: number) { this.someProperty = value; } public on(eventType: string, fn: Function) { } } class Parent { protected static getTransform(value: number) { return value + 180; } public transform: SomeClass; constructor(value: number) { this.transform = this.createTransform(value); } protected createTransform(value: number) { const transform = new SomeClass(value); transform.on('rotate', this.rotate); return transform; } protected rotate(event: any) { this.transform.someProperty = Parent.getTransform(event.transform); } } class Child extends Parent { protected static getTransform(value: number) { return value + 90; } constructor(value: number) { super(value); } } class Example extends Child { constructor(value: number) { super(value) console.log(`In Example:`, Child.getTransform(700)) // this line will be called when `new Example()` } public static getTransform(value: number) { return Child.getTransform(600); // call parent method which is protected is valid } } // Example use cases: console.log(`Call Child:`, Child.getTransform(800)) // this will throw error: // Property 'getTransform' is protected and only accessible within class 'Child' and its subclasses. new Example(700) // inheritance can call parent method which is protected console.log(`Call Example:`, Example.getTransform(1000)) // a function call that call parent protected method is ok. TypeScript Playground <- Puede hacer clic en el botón Run para ver el resultado y la pestaña Errors para ver los mensajes de error.
No estoy seguro de por qué desea reasignar una función estática ... en su caso, debe anular la función de rotate() en Child y usar el método estático Child en su lugar:
protected rotate(event: any) { this.transform.someProperty = Child.getTransform(event.transform); }