`

Multiple Inheritance

 
阅读更多
Let's create a multi() function that accepts any number of input objects. You can wrap the loop that copies properties in another loop that goes through all the objects passed as arguments to the function.
function multi() {
var n = {}, stuff, j = 0, len = arguments.length;
for (j = 0; j < len; j++) {
stuff = arguments[j];
for (var i in stuff) {
n[i] = stuff[i];
}
}
return n;
}



Let's test this by creating three objects: shape, twoDee and a third, unnamed object. Creating a triangle object will mean calling multi() and passing all three objects.
var shape = {
name: 'shape',
toString: function() {return this.name;}
};
var twoDee = {
name: '2D shape',
dimensions: 2
};
var triangle = multi(shape, twoDee, {
name: 'Triangle',
getArea: function(){return this.side * this.height / 2;},
side: 5,
height: 10
});

Will it work? Let's see:

>>> triangle.getArea()
25
>>> triangle.dimensions
2
>>> triangle.toString()
"Triangle"

分享到:
评论

相关推荐

    Selected.Topics.in.Cplusplus.15117

    Then it has multiple inheritance, something that’s not part of most languages. It has pointers and references. You need to know how and when you should use them. Then you have virtual functions and ...

    C 程序设计教学课件:CHAPTER 8 INHERITANCE.ppt

    4. **8.4 Multiple Inheritance** - 多重继承允许一个派生类从多个基类继承,但可能导致命名冲突和菱形问题,需要谨慎处理。 5. **8.5 Constructors and Destructors Under Inheritance** - 在继承中,派生类可以...

    java英文笔试

    While Java does not directly support multiple inheritance due to potential complications and ambiguities, you can achieve similar functionality using interfaces, which allow for multiple inheritance ...

    Google C++ Style Guide(Google C++编程规范)高清PDF

    Classes Inheritance Multiple Inheritance Interfaces Operator Overloading Access Control Declaration Order Write Short Functions Google-Specific Magic Smart Pointers cpplint Other C++ Features ...

    A Swift mixin for UITableViewCells and UICollectionViewCells.zip

    A Swift mixin for UITableViewCells and UICollectionViewCells.zip,A Swift mixin for reusing views easily and in a type-safe way (UITableViewCells, UICollectionViewCells, custom UIViews, ViewControllers...

    Learning Object-Oriented Programming(PACKT,2015)

    With a fair understanding of interfaces, multiple inheritance, and composition, you will move on to refactor existing code and to organize your source for easy maintenance and extension. Learning ...

    Fluent Python

    Learn how to write ... •Examine attribute descriptors and when to use them: the key to ORMs, •Explore Pythonic objects: protocols versus interfaces, abstract base classes and multiple inheritance

    fluent python

    Learn how to write ... •Examine attribute descriptors and when to use them: the key to ORMs, •Explore Pythonic objects: protocols versus interfaces, abstract base classes and multiple inheritance

    InheritanceAndPolymorphism

    2. **单一继承与多重继承(Single and Multiple Inheritance)**: Java支持单一继承,即一个子类只能继承一个父类。这有助于避免因多重继承可能导致的二义性问题。然而,通过接口(interfaces),Java可以实现类似...

    C++基础入门,自己整理的C++入门资料,直接在office里生成的word文档,是入门学习的好东西

    C++的关键特性包括类(Class)、继承(Inheritance)、多重继承(Multiple Inheritance)、运算符重载(Operator Overloading)、类模板(Class Templates)和标准库(Standard Library)。这些特性让C++成为一种...

    Scala in Depth

    Scala's OO features—type member inheritance, multiple inheritance, and composition Functional concepts and patterns—immutability, applicative functors, and monads ===================================...

Global site tag (gtag.js) - Google Analytics