2016-07-05 119 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