2016-07-05 120 views
-1

如何在單個類中擴展兩個類 如何擴展兩個類

Class1- Common Class for all the other classes 
Class2-(Where I use Class2 Extends Class1) 
Class3-(Where I use Class3 Extends Class1) 
Class4-(Where I use Class4 Extends Class1) 

Suppose I introduce a ClassA whose methods are common for Class 2,3,4  
then how do I extend two classes 
Class2-Class2 Extends Class1 Extends ClassA 
Class3-Class3 Extends Class1 Extends ClassA 
Class3-Class4 Extends Class1 Extends ClassA 
Note that Class1 and ClassA have no methods in common. 
+0

歡迎堆棧溢出!你試過了什麼,結果如何?請閱讀如何提出一個好問題的幫助主題。您需要研究自己的問題,查找代碼示例等,並編寫自己的代碼來解決問題。如果你做了所有這些,但仍然無法解決問題,那麼請回過頭來編輯你的問題,並從你做的研究,你試過的代碼,結果是什麼......任何錯誤消息等等中添加筆記。 – JeffC

回答

0

基於對象的方法:

function foo() {} 
 
function bar() {} 
 
function bam() {} 
 

 
var one = { foo: foo }; // Common Class for all the other classes 
 
document.write(one.foo, '<br/>'); // 'function foo() {}' 
 
document.write(one.bar, '<br/>'); // 'undefined' 
 
document.write(one.bam, '<br/>'); // 'undefined' 
 

 
var two = Object.create(one, { 
 
    bar: { value: bar }, 
 
}); 
 
document.write(two.foo, '<br/>'); // 'function foo() {}' 
 
document.write(two.bar, '<br/>'); // 'function bar() {}' 
 
document.write(two.bam, '<br/>'); // 'undefined' 
 

 
var three = Object.create(two, { 
 
    bam: { value: bam }, 
 
}); 
 
document.write(three.foo, '<br/>'); // 'function foo() {}' 
 
document.write(three.bar, '<br/>'); // 'function bar() {}' 
 
document.write(three.bam, '<br/>'); // 'function bam() {}'

構造函數爲基礎的方法:

function foo() {} 
 
function bar() {} 
 
function bam() {} 
 

 
function One() {} 
 
One.prototype.foo = foo; 
 

 
var one = new One(); 
 
document.write(one.foo, '<br/>'); // 'function foo() {}' 
 
document.write(one.bar, '<br/>'); // 'undefined' 
 
document.write(one.bam, '<br/>'); // 'undefined' 
 

 
function Two() {} 
 
Two.prototype = new One(); 
 
Two.prototype.bar = bar // ! 
 

 
var two = new Two(); 
 
document.write(two.foo, '<br/>'); // 'function foo() {}' 
 
document.write(two.bar, '<br/>'); // 'function bar() {}' 
 
document.write(two.bam, '<br/>'); // 'undefined' 
 

 
function Three() {} 
 
Three.prototype = new Two(); 
 
Three.prototype.bam = bar // ! 
 

 
var three = new Three(); 
 
document.write(three.foo, '<br/>'); // 'function foo() {}' 
 
document.write(three.bar, '<br/>'); // 'function bar() {}' 
 
document.write(three.bam, '<br/>'); // 'function bam() {}'

基於類的方法(注:JavaScript的「類」只是語法糖):

function foo() {} 
 
function bar() {} 
 
function bam() {} 
 

 
class One { 
 
    foo() {} 
 
} 
 
var one = new One(); 
 
document.write(one.foo, '<br/>'); // 'function foo() {}' 
 
document.write(one.bar, '<br/>'); // 'undefined' 
 
document.write(one.bam, '<br/>'); // 'undefined' 
 

 
class Two extends One { 
 
    bar() {} 
 
} 
 
var two = new Two(); 
 
document.write(two.foo, '<br/>'); // 'function foo() {}' 
 
document.write(two.bar, '<br/>'); // 'function bar() {}' 
 
document.write(two.bam, '<br/>'); // 'undefined' 
 

 
class Three extends Two { 
 
    bam() {} 
 
} 
 
var three = new Three(); 
 
document.write(three.foo, '<br/>'); // 'function foo() {}' 
 
document.write(three.bar, '<br/>'); // 'function bar() {}' 
 
document.write(three.bam, '<br/>'); // 'function bam() {}'

混合式基礎的方法(注意,這直接把結果對象的方法):

var one = { foo: foo }; // Common Class for all the other classes 
 
document.write(one.foo, '<br/>'); // 'function foo() {}' 
 
document.write(one.bar, '<br/>'); // 'undefined' 
 
document.write(one.bam, '<br/>'); // 'undefined' 
 

 
var two = Object.assign({ bar: bar }, one); 
 
document.write(two.foo, '<br/>'); // 'function foo() {}' 
 
document.write(two.bar, '<br/>'); // 'function bar() {}' 
 
document.write(two.bam, '<br/>'); // 'undefined' 
 

 
var three = Object.assign({ bam: bam }, one, two); 
 
document.write(three.foo, '<br/>'); // 'function foo() {}' 
 
document.write(three.bar, '<br/>'); // 'function bar() {}' 
 
document.write(three.bam, '<br/>'); // 'function bam() {}' 
 

 
function foo() {} 
 
function bar() {} 
 
function bam() {}

0

實例在Javascript中,我有四節課

Declaration_Ex_1 (Common class for all classes) 
Browser_Ex_2 Extends Declaration_Ex_1 
Login_Ex_3 Extends Declaration_Ex_1 
Summary_Ex_4 Extends Declaration_Ex_1 

假設我有一個多類

ElementsCountInPage_Ex_5 

,我想打電話給在所有其餘三個班的第1和第5兩個公共類(例如Browser_Ex_2,Login_Ex_3和Summary_Ex_4),那麼我怎樣才能使用其他類的方法。

正如我們不能寫擴展兩類兩次,這是不正確的不是它

Browser_Ex_2 Extends Declaration_Ex_1 
Extends ElementsCountInPage_Ex_5