ES6:从父类访问继承的类的属性和方法 [英] ES6: access to inherited class'es properties and methods from the parent class
问题描述
ES6没有抽象方法或属性,但是我可以从继承的类中获取父类中的某些方法或属性吗?
class ParentClass {
constructor(){
ParentClass.checkClildPropertyAccessibility();
ParentClass.checkClildMethodAccessibility();
ParentClass.checkClildStaticPropertyAccessibility();
ParentClass.checkClildStaticMethodAccessibility();
}
static checkClildPropertyAccessibility() {
console.log(ParentClass.childProperty);
}
static checkClildMethodAccessibility(){
ParentClass.childMethod()
}
static checkClildStaticPropertyAccessibility(){
console.log(ParentClass.childStaticProperty);
}
static checkClildStaticMethodAccessibility(){
ParentClass.clildStaticMethod()
}
}
class ChildClass extends ParentClass {
constructor(){
super();
ChildClass.childProperty = 'child\'s Property: OK';
}
childMethod(){
console.log('child\'s method OK');
}
// static property emulation is ES6
static get childStaticProperty() { return 'Child\'s static property: OK even ES6' }
static clildStaticMethod (){
console.log('Child\'s static method: OK');
}
}
let childClassInstance = new ChildClass();
概念是我们必须在子类中定义一些属性和方法,但是父类需要它们已经在构造函数中使用".
是,可以调用仅在ES2015类的子类中定义的方法.
class ParentClass {
constructor() {
this.childMethod();
this.initialize();
console.log(this.childProperty1);
console.log(this.childProperty2);
}
}
class ChildClass extends ParentClass {
constructor() {
super();
this.childProperty1 = 'child\'s Property: OK';
}
initialize() {
this.childProperty2 = 'child\'s Property 2: OK';
}
childMethod() {
console.log('Child\'s overriden method: OK');
}
}
let childClassInstance = new ChildClass();
通知initialize()
用于为childProperty2
分配初始值.父构造函数将始终在子类构造函数中的任何其他代码之前运行,这就是为什么在进行控制台调用时未初始化childProperty1
的原因.在父类中,this
将指向带有子类prototype
的对象. 在注释部分,Bergi
指出应该避免在父构造函数中调用重写方法的做法,因为重写方法可能取决于子构造函数尚未设置的状态. >
否,它不适用于 static 方法. 即使将静态方法复制到子类中,,当您引用ParentClass
时,也将在此定义方法.那里没有原型链.
编辑:评论部分的说明.
ES6 has not abstract methods or properties, but can I get some methods or properties in the parent class from inherited class?
class ParentClass {
constructor(){
ParentClass.checkClildPropertyAccessibility();
ParentClass.checkClildMethodAccessibility();
ParentClass.checkClildStaticPropertyAccessibility();
ParentClass.checkClildStaticMethodAccessibility();
}
static checkClildPropertyAccessibility() {
console.log(ParentClass.childProperty);
}
static checkClildMethodAccessibility(){
ParentClass.childMethod()
}
static checkClildStaticPropertyAccessibility(){
console.log(ParentClass.childStaticProperty);
}
static checkClildStaticMethodAccessibility(){
ParentClass.clildStaticMethod()
}
}
class ChildClass extends ParentClass {
constructor(){
super();
ChildClass.childProperty = 'child\'s Property: OK';
}
childMethod(){
console.log('child\'s method OK');
}
// static property emulation is ES6
static get childStaticProperty() { return 'Child\'s static property: OK even ES6' }
static clildStaticMethod (){
console.log('Child\'s static method: OK');
}
}
let childClassInstance = new ChildClass();
The concept is "We must to define some properties and methods in child class, however the parent class needs them to use already in constructor".
Yes, it's possible to call methods only defined in a subclass of an ES2015 class.
class ParentClass {
constructor() {
this.childMethod();
this.initialize();
console.log(this.childProperty1);
console.log(this.childProperty2);
}
}
class ChildClass extends ParentClass {
constructor() {
super();
this.childProperty1 = 'child\'s Property: OK';
}
initialize() {
this.childProperty2 = 'child\'s Property 2: OK';
}
childMethod() {
console.log('Child\'s overriden method: OK');
}
}
let childClassInstance = new ChildClass();
Notice initialize()
is used to assign an initial value to childProperty2
. Parent constructor will always run before any other code in a subclass constructor, that's why childProperty1
isn't initialized when the console call was made. In a parent class, this
will point to an object with the prototype
of the subclass. In the comments section, Bergi
points out the practice of calling an overridden method in a parent constructor should be avoided, given the overriden method may depend on state not yet setup by the child constructor.
And no, it doesn't work with static methods. Even though static methods are copied to subclasses, when you refer to ParentClass
, you'll get the method defined there. There's no prototype chain to follow there.
EDIT: clarification from comments section.
这篇关于ES6:从父类访问继承的类的属性和方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!