【问题标题】:Multiple Inheritance ES6多重继承 ES6
【发布时间】:2019-01-25 18:59:41
【问题描述】:

我正在创建一个游戏,为此我创建了几个可用于服务器端和客户端的基类,例如部队和步兵(扩展部队)。 infantry 类实现了 isInfantry 等方法。

为了创建客户端渲染,我创建了一个名为 TroopClient 的类,它扩展了 Troop 并向其属性添加了一个精灵和一个移动此类精灵的方法。之后,我创建了扩展 TroopClient 的 InfantryClient。现在这个 InfantryClient 有 sprites 属性和方法,但没有实现 Infantry 方法。

我研究过混合插件,但我没有找到一篇关于 ES6 类的好文章,我不知道我的方法是否正确。

我正在寻找有关此问题的一些见解以及如何使用 mix ins(如果这是一个好方法)来做到这一点(有一些解释)。

【问题讨论】:

  • 对于诸如使对象可渲染的特征/混合或组合之类的事情是比继承更好的解决方案。无论如何,TroopClientTroop 的关系是有问题的。 JS 的一个好处是现有的类和对象可以在没有继承的情况下进行扩展。
  • 使用组合而不是继承,如this fun-fun video.中所述
  • 为什么继承关系有问题?在我看来这很自然,因为我想扩展父类的行为并使其更加具体。
  • 是的,该视频展示了为什么继承不起作用的常见混乱示例。但我真的不喜欢使用组合,因为在我看来,代码的可读性和可理解性会降低。
  • @gazdagergo fun-fun 得到了完全、完全错误的构图,而实际的构图从未出现在该视频中。 reddit.com/r/programming/comments/5dxq6i/…

标签: javascript ecmascript-6 multiple-inheritance


【解决方案1】:

ES6 或任何其他 ES/JS 版本不支持多重继承或混合。由于它是一种动态类型的语言,您可以模仿这种行为,但这会有一些限制和缺点。您可能需要查看一些 examples online 以了解解决方法。

【讨论】:

    【解决方案2】:

    这是我用来在 JS 中允许多重继承的解决方法。虽然你的里程可能会有所不同。

    var custom = function() {
    	
    	"use strict";
    	
    	return {
    		class: function(constructor,prototype) {
    			constructor.prototype = prototype;
    			return constructor;
    		},
    		
    		class_extends: function(bases,constructor,prototype) {
    			for (var i = 0; i < bases.length; ++i) {
    				var base = bases[i];
    				
    				for (var property in base.prototype) {
    					if (!prototype[property]) {
    						prototype[property] = base.prototype[property];
    					}
    				}
    			}
    			
    			function bundledConstructor() {
    				for (var i = 0; i < bases.length; ++i) {
    					bases[i].apply(this,arguments);
    				}
    				
    				constructor.apply(this,arguments);
    			}
    			
    			constructor.prototype = prototype;
    			bundledConstructor.prototype = prototype;
    			
    			return bundledConstructor;
    		}
    	};
    	
    }();
    
    void function() {
    	
    	"use strict";
    	
    	var Base_1 = custom.class(
    		// Constructor function
    		function() {
    			
    		},
    		
    		// Prototype, shared between class instances
    		// use for constant values & functions
    		{
    			overloadedFunction: function() {
    				console.log("This is from base 1");
    			},
    			
    			base_1_function: function() {
    				console.log("This is from base 1");
    			}
    		}
    	);
    	
    	var Base_2 = custom.class(
    		function() {
    			
    		},{
    			base_2_function: function() {
    				console.log("This is from base 2");
    			}
    		}
    	);
    	
    	/*
    		Derrived inherits properties, constants & functions from base_1 & base_2
    		if anything is redeclared in Derrived, it is considered overloaded
    		
    		(When a new Derrived object is made, the constructors for base_1 & base_2 are called first)
    	*/
    	var Derrived = custom.class_extends([Base_1,Base_2],
    		function() {
    			
    		},{
    			overloadedFunction: function() {
    				console.log("This is from derrived");
    			}
    		}
    	);
    	
    	onload = function() {
    		var derrived = new Derrived();
    		
    		derrived.base_1_function();
    		derrived.base_2_function();
    		derrived.overloadedFunction();
    	}
    	
    }();

    【讨论】:

      猜你喜欢
      • 2016-05-25
      • 1970-01-01
      • 2017-11-25
      • 1970-01-01
      • 2010-09-27
      • 2012-03-07
      • 1970-01-01
      相关资源
      最近更新 更多