类的继承和扩展有什么区别
What is a difference between inherits and extends of classes?
我一直在寻找asnwer,我发现它有点复杂。类的继承和扩展有什么区别?
我的问题是在阅读这本电子书后产生的,我正在使用extends
语法,所以这让我感到好奇。
扩展类
class A {
a = 2;
constructor(x) {
this.a = x;
}
}
class B extends A {
}
类继承
class A {
a = 4;
A(x) {
a = x;
}
drive() {
output( "A" )
}
}
class B inherits A {
drive() {
inherited:drive()
output( "B" )
}
}
我可以在inherits
类时使用constructor
吗?或者在扩展类时name constructor
?
使用super
或inherited
时有什么区别?
扩展类时可以使用inherited
语法吗?我读到super
子类的构造函数引用其父类的构造函数的直接方法。
inherits
不是ES6 中的关键字。在class
声明的那个位置,只有extends
有效,你有一个语法错误。
既没有a = 4;
在类机构中,也没有inherited:drive()
.你在书中发现这一点的部分甚至明确指出"考虑这个松散的伪代码(发明的语法)用于继承的类"。
继承是指派生类(子类)和基类(父类)之间的关系。派生类可以根据可访问性级别使用基类中的某些方法和字段
扩展可以与继承互换,通常在java中使用(因为java中的继承语法是关键字extend。在 C# 中,它是冒号:
相关文章:
- 全局变量和全局对象的属性之间有什么区别吗
- 在ember/handlers中使用value和valueBinding有什么区别
- 这两个关于 JavaScript 作用域链的例子有什么区别?
- Math.min()和Math.max()之间有什么区别?在Javascript中
- angularjs中的ng网格和ui网格有什么区别
- webpack开发模式和生产构建模式之间有什么区别
- 缓存!saveLocations()和addLocation()函数有什么区别
- 关于承诺/A+规范,术语“当时可”和“承诺”之间有什么区别
- 以下两者在javascript中有什么区别
- jquery.js和jquery.lite.js有什么区别
- 通过Nuget安装Angular JS和Angular JS核心有什么区别
- XMLHttpRequest对象的open()和send()方法之间有什么区别
- 这两种不同的创建对象文字的方法有什么区别
- jQuery中这两个词有什么区别
- 在Javascript语法中:和::和:::有什么区别
- getElementById 和 getElementsByClassName 有什么区别?
- (typeof variable === “function”) 和 jQuery.isFunction() 有什么区别
- 在使用JS的Ajax调用中,ajaxComplete和beforesend之间有什么区别
- $(e.target).find和template.find('input').value之间有什么区别
- 在javascript中,在变量名之前使用var有什么区别/优势吗