给出以下示例类:
class Foo:
def aStaticMethod():
return "aStaticMethod"
aVariable = staticmethod(aStaticMethod)
aTuple = (staticmethod(aStaticMethod),)
aList = [staticmethod(aStaticMethod)]
print Foo.aVariable()
print Foo.aTuple[0]()
print Foo.aList[0]()
Run Code Online (Sandbox Code Playgroud)
为什么会调用aVariable正常,但与作品aTuple和aList它返回的错误'staticmethod' object is not callable?
我有一些问题,通过反射调用带有out参数的重载静态方法,并会欣赏一些指针.
我期待动态地创建类型像System.Int32或者System.Decimal,然后调用静态TryParse(string, out x)方法就可以了.
以下代码有两个问题:
t.GetMethod("TryParse", new Type[] { typeof(string), t } ) 无法返回我期望的MethodInfo
mi.Invoke(null, new object[] { value.ToString(), concreteInstance })似乎成功但没有将out参数concreteInstance设置为已解析的值
交织到此函数中,您可以看到一些临时代码,演示如果type参数设置为应该发生什么System.Decimal.
public static object Cast(object value, string type)
{
Type t = Type.GetType(type);
if (t != null)
{
object concreteInstance = Activator.CreateInstance(t);
decimal tempInstance = 0;
List<MethodInfo> l = new List<MethodInfo>(t.GetMethods(BindingFlags.Static | BindingFlags.Public));
MethodInfo mi;
mi = t.GetMethod("TryParse", new Type[] { typeof(string), t } ); //this …Run Code Online (Sandbox Code Playgroud) 和同事讨论过这是不好的做法.现在我在网上找不到这个例子.
我们有很多数据库对象映射器,并且像这样调用它的函数
(示例) - setId方法获取数据库中的行并将其设置为预定义的属性
class Person {
public static function get($id) {
$object = new Person;
$object->setId($id);
return $object;
}
}
Run Code Online (Sandbox Code Playgroud)
像这样使用它我们可以使用这样的简单结构:(我们从例如一个帖子得到id)
$person = Person::get($id);
Run Code Online (Sandbox Code Playgroud)
代替
$person = new Person;
$person->setId($id);
Run Code Online (Sandbox Code Playgroud)
现在,我的直觉告诉我这是不好的做法.但我无法解释.也许有人在这里可以解释为什么这是,或者不是不好的做法
以下是我们如何使用它的一些其他示例.我们主要用它来吸气.(只是名称,而不是代码.几乎所有这些都只运行一个查询,它可以返回1个对象,然后使用结果的id来使用setId方法)
class CatalogArticle {
public static function get($id) { }
public static function getByArticlenumber($articlenumber) {} //$articlenumber is unique in the database
public static function getRandom() {} //Runs a query returning a random row
}
Run Code Online (Sandbox Code Playgroud) 在我的一次采访中,我被问到static修饰符的含义.我回答告诉采访者无法创建静态类的对象和其他有用的点.
但是访问者询问创建这样一个无法创建对象的类的用途是什么.基本上,他们首先要问为什么static需要?
我真的不确定如何回答这个问题.我该说什么?
澄清:这个问题不是关于访问修饰符
确认Bm()和bm()语句都适用于以下代码:
class A {
static void m() { //some code }
}
class B extends A {
}
class Example {
public static void main (String [] args) {
B.m(); // running A's m() static method
}
public void try() {
B b = new B();
b.m(); // running A's m() static method
}
}
Run Code Online (Sandbox Code Playgroud)
我的问题是我们可以说"静态方法是继承的"吗?
如果"inherited"是正确的术语,如果我们向B类添加一个方法,我们将使用相同的静态类签名:
class A {
static void m() { //some code }
}
class B extends A {
static void m() { //some code …Run Code Online (Sandbox Code Playgroud) 我有以下代码(例如,实际上,这是我的真实代码):
<?php
class Foobar
{
public static function foo()
{
exit('foo');
}
}
Run Code Online (Sandbox Code Playgroud)
当我运行$foobar = new FooBar; $foobar->foo()它显示foo.
为什么PHP会尝试在对象上下文中使用静态方法?有办法避免这种情况吗?
好吧,你们没有得到我的问题:我知道静态和非静态方法之间的区别以及如何调用它们.这是我的全部意义,如果我打电话$foobar->foo(),为什么PHP会尝试运行静态方法?
注意:我运行PHP 5.4.4,报告错误E_ALL.
为什么Java 8支持静态方法?下面代码中main方法中两行的区别是什么?
package sample;
public class A {
public static void doSomething()
{
System.out.println("Make A do something!");
}
}
public interface I {
public static void doSomething()
{
System.out.println("Make I do something!");
}
}
public class B {
public static void main(String[] args) {
A.doSomething(); //difference between this
I.doSomething(); //and this
}
}
Run Code Online (Sandbox Code Playgroud)
正如我们在上面看到的那样,我甚至没有在B中实现.当我们可以在另一个类中编写相同的静态方法并调用它时,在接口中使用静态方法的目的是什么?是否为模块化以外的任何其他目的而引入.通过模块化,我的意思是:
public interface Singable {
public void sing();
public static String getDefaultScale()
{
return "A minor";
}
}
Run Code Online (Sandbox Code Playgroud)
只是把类似的方法放在一起.
假设以下类定义:
class A:
def f(self):
return 'this is f'
@staticmethod
def g():
return 'this is g'
a = A()
Run Code Online (Sandbox Code Playgroud)
所以f是常规方法,g是静态方法.
现在,我如何检查ffion对象af和ag是否是静态的?Python中有"isstatic"功能吗?
我必须知道这一点,因为我有包含许多不同函数(方法)对象的列表,并且要调用它们我必须知道它们是否期望"自我"作为参数.
我需要调用类的静态方法,但我只有一个类名,而不是它的一个实例.我是这样做的.
$class = new "ModelName";
$items = $class::model()->findAll();
Run Code Online (Sandbox Code Playgroud)
它可以在我的计算机上运行,但是当我移动到服务器时,它会抛出一个unexpected T_PAAMAYIM_NEKUDOTAYIM,所以我认为它实际上期望模型是变量而不是方法.
PS:如果它有帮助,它是Yii框架,所以如果有另一种方法来调用find()函数,那对我来说没问题.
提前致谢
static-methods ×10
php ×4
oop ×3
c# ×2
java ×2
python ×2
static ×2
class ×1
inheritance ×1
interface ×1
java-8 ×1
polymorphism ×1
reflection ×1
yii ×1