我错误地注意到这项工作:
在MyTest.h接口上
- (void):(int)idIndex;
Run Code Online (Sandbox Code Playgroud)
关于实现MyTest.m
- (void):(int)idIndex {}
Run Code Online (Sandbox Code Playgroud)
我甚至可以称之为
[self :2 ];
Run Code Online (Sandbox Code Playgroud)
那么为什么我允许使用没有名字的方法?
对不起,如果是一个奇怪的问题,在我看来只是非常奇怪的行为,欢呼
这些天我的代码中发生了一些奇怪的事情.每次我必须构建一个新的成员函数,或者每次我回到之前定义的成员函数时,我认为:
"嘿,无论这个类的哪个实例调用它,这都是完全相同的过程,所以让它变得静止!"
而且我觉得它会减少内存消耗,因为每个实例都不必"携带"一个函数的实现,这是正确的吗?
所以不要像以下那样:
class Foo
{
int _attributes;
double methods(int parameters)
{
// do stuff using..
_attributes;
};
};
Run Code Online (Sandbox Code Playgroud)
我最终得到的结果如下:
class Foo
{
int _attributes;
static double methods(int parameters, Foo* instance)
{
// do stuff using..
instance->_attributes;
};
};
Run Code Online (Sandbox Code Playgroud)
而且我看不到任何不会以这种方式改变的功能.我的所有功能都转向静态,我觉得某处出了点问题.
我错过了什么?再次使用非静态方法有什么用?XD
我有一个类似于下面的LINQ函数:
public static Func<Contact, bool> activeContactsFilter = c =>
!c.suspended &&
!c.expired &&
(c.status_id == 1 || c.status_id == 7);
Run Code Online (Sandbox Code Playgroud)
当我需要找出一个联系人中的哪些联系人IEnumerable是"活动的" 时,这很有用:
var activeContacts = allContacts.Where(activeContactsFilter);
Run Code Online (Sandbox Code Playgroud)
但是如果我想检查一个特定联系人是否"有效"怎么办?如何在单个对象上使用相同的函数进行测试?
我可以理解Python要求self作为第一个参数,而不是什么。然而,我对 PyCharm 告诉我方法“hello”需要 self 作为第一个参数(当它甚至没有被使用时)感到困惑。
举个例子:
class Example:
def hello():
return "hello world"
Run Code Online (Sandbox Code Playgroud)
这会给我一个错误,说“方法必须有第一个参数,通常称为 self”
即使函数/方法没有使用它,我是否仍然应该将 self 作为参数包含在内,还是应该忽略此错误/警告?这是因为该方法位于类内部吗?
我有一点问题要理解为什么python不会将浮点数转换为int.以下是代码段.
import time
now = time.time()
print type(now)
int(now)
print type(now)
Run Code Online (Sandbox Code Playgroud)
这是我得到的结果,我无法弄清楚为什么.有任何想法吗?提前致谢
<type 'float'>
<type 'float'>
Run Code Online (Sandbox Code Playgroud) 作为一个例子:math.sqrt()被认为是一个函数但是,"Lower".upper()被称为.upper() 方法.
为什么是math.sqrt功能?点访问不是指示方法吗?
下面是一个简单的类,其中有一个方法/函数
class Test():
def f(self):
return "function or method"
Run Code Online (Sandbox Code Playgroud)
我应该在这里调用什么f()- 方法还是函数?
python ×4
function ×2
c# ×1
c++ ×1
casting ×1
instance ×1
ios ×1
linq ×1
member ×1
objective-c ×1
python-2.7 ×1
python-3.x ×1
self ×1
static ×1
warnings ×1