1-是真的吗?特定类的所有对象都有自己的数据成员,但是共享成员函数,而内存中只有一个副本?
2- 以及为什么init在此代码中的地址类似:
class c:
def __init__(self,color):
print (f"id of self in __init__ on class is {id(self)}")
def test(self):
print("hello")
print (f"id of __init__ on class is {id(__init__)
我试图了解如何在Python中创建对象的过程,即__init__方法。
我需要理解这些东西,才能编写和调试利用元类的代码。
请考虑以下通过的代码,我不明白为什么。
# define a class, with an __init__ method
class A(object):
def __init__(self, value):
self.value = value
# instantiate regularly
a1 = A(1)
# or using the class's __call__
a2 = A.__call__(2)
assert a1
让我们考虑下面的虚拟示例:
class A:
def __init__(self, a):
self.a = a
self.backup_a = a
def reset_a(self):
self.a = self.backup_a
print ('Stop touching my stuff!')
class B(A):
def __init__(self, a, b):
super().__init__(a)
self.
我在c#中创建了一个函数如下:
public void input_fields(int init_xcor, int init_ycor, char init_pos, string input)
{
char curr_position = 'n';
foreach (char c in input)
{
if (c == 'm')
{
Move mv = new Move();
if (curr_positio
创建一个具有两个父级的son类:
class mon():
def __new__(cls):
print("i am in mon's new")
return super().__new__(cls)
def __init__(self):
print("i am in mon's init")
super().__init__()
class far():
def __new__(cls):
print("i am in fa
struct IA
{
virtual void Init() = 0;
.....
};
struct A : public IA
{
void Init() {};
.....
};
struct B : public A
{
int Init() { return 1; };
};
通过这样的设计,我得到了错误C2555:'B::Init':重写虚拟函数返回类型.
我能不能对A隐瞒一下,我不想隐瞒其他A的功能。A类来自其他地方作为A类使用,而不仅仅是通过B类。
编辑:我需要在层次结构中有两个Init函数,只有返回类型不同。我不
我试图了解python中超级程序是如何工作的,并尝试了以下示例:
class A(object):
def __init__(self):
print "in A's init"
class B(object):
def __init__(self):
print "in B's init"
class C(A,B):
def __init__(self):
super(C,self).__init__()
print "In C"
if
我有一个包含类的文件,我想使用这些类来存储API端点。我之所以要使用类,是因为我可以通过键入api.level2.resources来访问端点。
下面是文件的样子,API是主类,SubEntries是“子”:
class API(object):
"""
A class for logging to stdout and/or a file. Supports color output for different log kinds.
"""
def __init__(self):
"
大家好,我有一个问题,是关于我在研究python中钻石形状继承时意识到的一些事情。
A
/ \
B C
\ /
D
我第一次创造了这个
class A:
def __init__(self):
print('a')
class B(A):
def __init__(self):
print('b')
A.__init__(self)
class C(A):
def __init__(self):
print('c')
我使用PHP7.1.0。
假设我们有一个特性,我们在类中使用它并重命名导入的方法:
trait T
{
public function A() {
echo ".";
}
}
class C
{
use T {
A as B;
}
}
$c = new C();
$c->B();
$c->A(); // Why does it work?
为什么PHP仍然允许我使用旧的方法名(在本例中是A)?
这真的很痛苦,因为在更复杂的例子中,您不能依赖于方法重命名--因此您可以意外地收到“不兼容声明”错误:
c
编译前:
function Foo(){
this.init = function(){
var bar = new Bar();
bar.init();
};
}
function Bar(){
this.init = function(){
console.log('Hello');
};
}
$(window).load(function(){
var foo;
foo = new Foo();
foo.init();
});
运行闭包编译器和格式化后:
funct
我想要做的是从当前类的父类调用每个方法"init“。
我这样做是为了避免程序员每次在新控制器中创建init方法时都必须调用init方法(parent::init())。
示例:
class Aspic\Controller { } // main controller
class ControllerA extends Aspic\Controller { public function init() {/* do something 1 */}
class ControllerB extends ControllerA {}
class ControllerC extends C
我正在尝试使用python类型()方法动态创建一个类。
所以,假设我有一个基类A
>>> class A:
def __init__(self):
print("I am in init of A..")
现在我使用type方法创建了一个子类“C”
>>> C = type('C',(A,),{})
当我创建一个对象
>>> c = C()
I am in init of A..
基类的init也被正确调用。
现在我想在init方法中做一些事情,我编写了一个自定义init方法。
>
我查看了和其他几个网站,但我仍然不确定我得到了这个问题的答案。如果您有一个具有虚拟函数Init()的父类,那么我是否可以在派生类中拥有一个覆盖函数Init(int num),或者您是否只需为创建的每个派生类创建一个新函数?如果我没记错的话,后者会导致您在Child类中有两个Init函数,对吗?我的意思是:
public class Parent {
protected int a;
public Parent() {
a=1;
}
public virtual void Init() {
我是objective c的新手。我知道-init()是一个instance method并返回一个对象,例如myObj=[myObj init];将返回一个对象myObj.
但是,如果self =[super init];通常是super refer to parent类,例如NSObject,它是一个class, not instance。
所以,Is -init() instance method or class method for super init?
谢谢
我有一个关于使用init的实践的问题。如果我有一个具有多个子类的父类,并且它们在init函数中都有相同的内容,那么最好将该内容放在父init类中,然后简单地这样做:
public function init(){
parent::init();
}
这似乎是合乎逻辑的,但我读到的每一个地方,不幸的是zend特定的,他们说把父类中的init方法留空以避免复杂性。
有人能澄清一下吗?
问题
我有以下要测试的javascript文件:
function myLocalHouse() {
this.init = function() {
$.post(//Ajax call using jQuery);
};
this.buyHouse(money, date) {
//code I want to test with mocha
};
this.init();
};
正如您所看到的,init方法使用jQuery,当我在控制台中执行mocha来测试它时,它会失败,因为它找不到$对象。
所以我的解