标签: class-method

继承中类方法的 self 与类名

在此代码中:

class Dog
  def self.bark
    print "woof"
  end
end

class Little_dog < Dog
end

Little_dog.bark
Run Code Online (Sandbox Code Playgroud)

该方法继承自引用 的通用类self。但是下一个代码补丁:

class Dog
  def Dog.bark
    print "woof"
  end
end

class Little_dog < Dog
end

Little_dog.bark
Run Code Online (Sandbox Code Playgroud)

也有效。我原以为它会给我一个错误,但它没有。

类继承下如何self引用类方法?为什么在第二个示例中该类little_dog有一个类方法bark,而我只将其定义为 的类方法Dog

ruby inheritance class-method

3
推荐指数
1
解决办法
2592
查看次数

什么是“可继承的替代构造函数”?

我在这个答案中偶然发现了术语“可继承的替代构造函数”:/sf/answers/116866711/

该链接指向一个classmethod进行解释的地方。

其他编程语言也有这个功能吗?

python inheritance class-method

3
推荐指数
1
解决办法
1055
查看次数

在 Python 中定义类变量时如何引用类方法?

我有以下类和类变量:

class MyClass:
    class_var_1 = "a"
    class_var_2 = run_class_method()

    @classmethod
    def run_class_method(cls):
        return "ran class method"
Run Code Online (Sandbox Code Playgroud)

但是,解释器说这run_class_method没有定义。使用MyClass.run_class_method()也不行。来自 java 背景,我不明白为什么这不起作用。那么,我该如何解决呢?

此外,我发现如果我在类的末尾定义类变量,这会起作用。这在python中被认为是不好的做法吗?

python class class-method python-3.x

3
推荐指数
2
解决办法
2287
查看次数

泛型类的类方法

我尝试在泛型类上调用类方法:

from typing import List, Union, TypeVar, Generic
from enum import IntEnum

class Gender(IntEnum):
    MALE = 1
    FEMALE = 2
    DIVERS = 3


T = TypeVar('T')

class EnumAggregate(Generic[T]):
    def __init__(self, value: Union[int, str, List[T]]) -> None:
        if value == '':
            raise ValueError(f'Parameter "value" cannot be empty!')

        if isinstance(value, list):
            self._value = ''.join([str(x.value) for x in value])
        else:
            self._value = str(value)

    def __contains__(self, item: T) -> bool:
        return item in self.to_list

    @property
    def to_list(self) -> List[T]:
        return [T(int(character)) for character in …
Run Code Online (Sandbox Code Playgroud)

python generics class-method python-3.x

3
推荐指数
1
解决办法
4904
查看次数

使用超类方法作为实例方法

我目前正在阅读 Luciano Ramalho 的优秀著作Fluent Python。在关于接口和继承的一章中,我们构建了一个列表的子类(原始代码请参见github),但我对定义其中一个实例方法的方式感到困惑。对于一个简化的例子,我的困惑是由以下情况引起的:

class ListWithLoadMethod(list):
    load = list.extend
Run Code Online (Sandbox Code Playgroud)

它生成一个新的 list 子类,该子类具有extend方法的别名as load。我们可以通过编写来测试这个类

loaded_list = ListWithLoadMethod(range(4))
print(loaded_list)
loaded_list.extend(range(3))
print(loaded_list)
loaded_list.load(range(3))
print(loaded_list) 
Run Code Online (Sandbox Code Playgroud)

正如预期的那样,它产生:

[0, 1, 2, 3]
[0, 1, 2, 3, 0, 1, 2]
[0, 1, 2, 3, 0, 1, 2, 0, 1, 2]
Run Code Online (Sandbox Code Playgroud)

我的困惑源于类方法和静态方法的区别。当新实例ListWithLoadedMethod被创建,它的子类list,但是当我们初始化我们点了实例loadlist.extend; Python怎么知道list.extend我们不是说load应该指向类的类方法,list而是实际上(显然?)继承超类列表的实例方法?

python inheritance class-method instance-methods

3
推荐指数
1
解决办法
55
查看次数

类方法和"在线分配的对象的潜在泄漏......"

我有这种情况:

- (void) foo {
    NSLog(@"Print this: %@", [MyObject classString]);
}

// So in MyObject.m I do
@implementation MyObject

+ (NSString *) classString {
     return [OtherObject otherClassString];   //The Warning "Potential leak..." is for this line
}
@end

// Finally in OtherObject
@implementation OtherObject

+ (NSString *) otherClassString {
    NSString *result = [[NSString alloc] initWithString:@"Hello World"];
    return result;
}
@end
Run Code Online (Sandbox Code Playgroud)

一开始,我对这项工作有一个警告otherClassString,classString但是otherClassString这样做.

现在我的问题是classStringMyObject.我尝试了很多东西,但这个警告总是显示出来.我不能在类方法中调用类方法吗?

memory-leaks class objective-c class-method

2
推荐指数
1
解决办法
529
查看次数

获取目标c中所有类的实例?

我有一个UIView,它有很多实例,每个实例都有一个UIRecognizer.

当点击它们时,我想删除其他人的所有识别器.

我想要它来获取类的所有实例并删除它们的识别.

我知道ManagedObjects有 [Entity allObjects];

如何创建"所有对象"类方法?

objective-c class-method ios

2
推荐指数
1
解决办法
2817
查看次数

main()是类方法吗?(Java)

看到了一个详细介绍了简单课程的问题。该类具有基本(非静态)方法。它也有一个主要方法。

问的问题:此类是否具有类方法?

可以public static void main(String[] args){}算是一类方法吗?

java program-entry-point class-method

2
推荐指数
1
解决办法
562
查看次数

Ruby中创建类方法的不同方法

例1:

class Dog
  def self.class_method
    :another_way_to_write_class_methods
  end
end

def test_you_can_use_self_instead_of_an_explicit_reference_to_dog
  assert_equal :another_way_to_write_class_methods, Dog.class_method
end
Run Code Online (Sandbox Code Playgroud)

例2:

class Dog
  class << self
    def another_class_method
      :still_another_way
    end
  end
end

def test_heres_still_another_way_to_write_class_methods
  assert_equal :still_another_way, Dog.another_class_method
end
Run Code Online (Sandbox Code Playgroud)

我可以知道在Ruby中编写类方法的哪种方式是首选的,为什么?是否存在一种优先于另一种情况的情况?

ruby methods syntax class class-method

2
推荐指数
1
解决办法
536
查看次数

python classmethod get_by_user返回None

我在使用类方法示例时需要帮助get_by_user接下来是我对类方法的改编.

class Pages(ndb.Model):
    user_id = ndb.StringProperty()
    name = ndb.StringProperty()

    @classmethod
    def get_by_user(cls, user):
        return cls.query().filter(cls.user_id == user.user_id()).get()
Run Code Online (Sandbox Code Playgroud)

view.py代码是下一个.

class MainPage(BaseHandler):

    def get(self):
    user = users.get_current_user()
    logging.info("MainPageGet: %s " % user)
    if user: #offer user options 
        isUser = True
        user_ID = user
        for p in Pages.query():
        logging.info("MainPageGet p: %s " % p.user_id)
        logging.info("MainPageGet user_ID: %s " % user_ID)
        page = Pages.get_by_user( user_ID) 
        logging.info("MainPageGet page: %s " % page )
Run Code Online (Sandbox Code Playgroud)

我的日志结果如下.

<pre>
INFO     2018-02-10 04:28:21,509 views.py:192] MainPageGet: test@example.com 
INFO     2018-02-10 …
Run Code Online (Sandbox Code Playgroud)

python google-app-engine class-method

2
推荐指数
1
解决办法
71
查看次数