有什么区别:
class Child(SomeBaseClass):
def __init__(self):
super(Child, self).__init__()
Run Code Online (Sandbox Code Playgroud)
和:
class Child(SomeBaseClass):
def __init__(self):
SomeBaseClass.__init__(self)
Run Code Online (Sandbox Code Playgroud)
我已经看到super在只有单继承的类中使用了很多.我可以看到为什么你在多重继承中使用它,但不清楚在这种情况下使用它的优点是什么.
以下两个声明之间有什么区别?
Class.method = function () { /* code */ }
Class.prototype.method = function () { /* code using this.values */ }
Run Code Online (Sandbox Code Playgroud)
是否可以将第一个语句视为静态方法的声明,将第二个语句视为实例方法的声明?
我想知道__init__和__call__方法之间的区别.
例如:
class test:
def __init__(self):
self.a = 10
def __call__(self):
b = 20
Run Code Online (Sandbox Code Playgroud) 在我的一次访谈中,我被要求解释Interface和Abstract类之间的区别.
这是我的回答:
Java接口的方法是隐式抽象的,不能有实现.Java抽象类可以具有实现默认行为的实例方法.
在Java接口中声明的变量默认为final.抽象类可能包含非最终变量.
默认情况下,Java接口的成员是公共的.Java抽象类可以具有类似私有,受保护等类通常的类成员.
应使用关键字"implements"实现Java接口; 应使用关键字"extends"扩展Java抽象类.
接口只能扩展另一个Java接口,抽象类可以扩展另一个Java类并实现多个Java接口.
Java类可以实现多个接口,但它只能扩展一个抽象类.
然而,面试官并不满意,并告诉我这个描述代表了" 书本知识 ".
他告诉我一个更实际的回答,解释我何时会使用实际例子在界面上选择一个抽象类.
我哪里做错了?
要使用公共方法创建JavaScript类,我会执行以下操作:
function Restaurant() {}
Restaurant.prototype.buy_food = function(){
// something here
}
Restaurant.prototype.use_restroom = function(){
// something here
}
Run Code Online (Sandbox Code Playgroud)
这样我班级的用户可以:
var restaurant = new Restaurant();
restaurant.buy_food();
restaurant.use_restroom();
Run Code Online (Sandbox Code Playgroud)
如何创建一个可由buy_food和use_restroom方法调用的私有方法,但不能由类的用户创建外部方法?
换句话说,我希望我的方法实现能够:
Restaurant.prototype.use_restroom = function() {
this.private_stuff();
}
Run Code Online (Sandbox Code Playgroud)
但这不应该奏效:
var r = new Restaurant();
r.private_stuff();
Run Code Online (Sandbox Code Playgroud)
我如何定义private_stuff为私有方法,所以这两个都适用?
我已经阅读了Doug Crockford的几次写法,但看起来似乎不能通过公共方法调用"私有"方法,并且可以在外部调用"特权"方法.
在JavaScript中,我想创建一个对象实例(通过new运算符),但是将任意数量的参数传递给构造函数.这可能吗?
我想做的是这样的事情(但下面的代码不起作用):
function Something(){
// init stuff
}
function createSomething(){
return new Something.apply(null, arguments);
}
var s = createSomething(a,b,c); // 's' is an instance of Something
Run Code Online (Sandbox Code Playgroud)
答案
从这里的回复中可以清楚地看出,没有内置的方式.apply()与new运营商通话.然而,人们提出了一些非常有趣的解决方案.
我首选的解决方案是Matthew Crumley的这个解决方案(我已将其修改为通过该arguments属性):
var createSomething = (function() {
function F(args) {
return Something.apply(this, args);
}
F.prototype = Something.prototype;
return function() {
return new F(arguments);
}
})();
Run Code Online (Sandbox Code Playgroud) 内聚和耦合有什么区别?
耦合和内聚如何导致软件设计的好坏?
有哪些例子可以概述两者之间的差异,以及它们对整体代码质量的影响?
类方法和实例方法有什么区别?
实例方法是访问器(getter和setter),而类方法几乎是其他所有方法吗?
为什么C#这样设计?
据我所知,接口只描述行为,并且用于描述实现某些行为的接口的类的合同义务.
如果类希望在共享方法中实现该行为,为什么不应该这样做呢?
这是我想到的一个例子:
// These items will be displayed in a list on the screen.
public interface IListItem {
string ScreenName();
...
}
public class Animal: IListItem {
// All animals will be called "Animal".
public static string ScreenName() {
return "Animal";
}
....
}
public class Person: IListItem {
private string name;
// All persons will be called by their individual names.
public string ScreenName() {
return name;
}
....
}
Run Code Online (Sandbox Code Playgroud) oop ×10
inheritance ×3
javascript ×3
class ×2
object ×2
python ×2
c ×1
c# ×1
class-method ×1
constructor ×1
interface ×1
java ×1
methods ×1
objective-c ×1
ooad ×1
super ×1