13.8.1 staticmethod()和 classmethod()内建函数

13.8.1 staticmethod()和 classmethod()内建函数

现在让我们看一下在经典中创建静态方法和类方法的一些例子(你也可以把它们用在新式类中):

class TestStaticMethod:
  def foo():
    print 'calling static method foo()'
  # foo=staticmethod(foo)
print TestStaticMethod.foo()


C:\Python27\python.exe C:/Users/TLCB/PycharmProjects/untitled/eeeee/a5.py
Traceback (most recent call last):
  File "C:/Users/TLCB/PycharmProjects/untitled/eeeee/a5.py", line 5, in <module>
    print TestStaticMethod.foo()
TypeError: unbound method foo() must be called with TestStaticMethod instance as first argument (got nothing instead)

class TestStaticMethod:
  def foo():
    print 'calling static method foo()'
  foo=staticmethod(foo)
print TestStaticMethod.foo()

C:\Python27\python.exe C:/Users/TLCB/PycharmProjects/untitled/eeeee/a5.py
calling static method foo()
None

类方法:
class TestClassMethod:
  def foo(cls):
      print cls
      print 'calling class method foo()'
      print 'foo() is part of class:', cls.__name__
  foo = classmethod(foo)

print TestClassMethod.foo()


C:\Python27\python.exe C:/Users/TLCB/PycharmProjects/untitled/eeeee/a5.py
__main__.TestClassMethod
calling class method foo()
foo() is part of class: TestClassMethod
None

实例方法:
class TestClassMethod:
  def foo(self):
      print self


a=TestClassMethod()
print a.foo()


C:\Python27\python.exe C:/Users/TLCB/PycharmProjects/untitled/eeeee/a5.py
<__main__.TestClassMethod instance at 0x025AF490>
None


风云的《银光志3.0》_书中第十三章的例子(13.8.1)出错请教

05-08

public MainPage()rn rn InitializeComponent();rn DataBind();rn rnrn //数据绑定rn private void DataBind()rn rn //添加事件rn WCFService.DGServiceClient sc = new Sample.WCFService.DGServiceClient();rn sc.GetUsersCompleted += new EventHandler(sc_GetUsersCompleted);rn rn //调用GetUsers方法rn sc.GetUsersAsync();rn rnrn //WCF调用完成事件rn void sc_GetUsersCompleted(object sender, GetUsersCompletedEventArgs e)rn rn //取得返回的用户数据rn List users = new List(e.Result);rn //为DataGrid控件设置数据源rn myDataGrid.ItemsSource = users;rn rnWCFService.DGServiceClient sc = new Sample.WCFService.DGServiceClient(); rnrn这里出错。rnrn+ $exception System.Collections.Generic.KeyNotFoundException: 给定关键字不在字典中。rn 位于 System.ThrowHelper.ThrowKeyNotFoundException()rn 位于 System.Collections.Generic.Dictionary`2.get_Item(TKey key)rn 位于 System.ServiceModel.Configuration.ServiceModelSectionGroup.get_Client()rn 位于 System.ServiceModel.Description.ConfigLoader.LookupChannel(String configurationName, String contractName, Boolean wildcard)rn 位于 System.ServiceModel.Description.ConfigLoader.LoadChannelBehaviors(ServiceEndpoint serviceEndpoint, String configurationName)rn 位于 System.ServiceModel.ChannelFactory.ApplyConfiguration(String configurationName)rn 位于 System.ServiceModel.ChannelFactory.InitializeEndpoint(String configurationName, EndpointAddress address)rn 位于 System.ServiceModel.ChannelFactory`1..ctor(String endpointConfigurationName, EndpointAddress remoteAddress)rn 位于 System.ServiceModel.ChannelFactory`1..ctor(String endpointConfigurationName)rn 位于 System.ServiceModel.EndpointTrait`1.CreateSimplexFactory()rn 位于 System.ServiceModel.EndpointTrait`1.CreateChannelFactory()rn 位于 System.ServiceModel.ClientBase`1.CreateChannelFactoryRef(EndpointTrait`1 endpointTrait)rn 位于 System.ServiceModel.ClientBase`1.InitializeChannelFactoryRef()rn 位于 System.ServiceModel.ClientBase`1..ctor()rn 位于 Sample.WCFService.DGServiceClient..ctor()rn 位于 Sample.MainPage.DataBind()rn 位于 Sample.MainPage..ctor()rn 位于 Sample.App.Application_Startup(Object sender, StartupEventArgs e)rn 位于 System.Windows.CoreInvokeHandler.InvokeEventHandler(Int32 typeIndex, Delegate handlerDelegate, Object sender, Object args)rn 位于 MS.Internal.JoltHelper.FireEvent(IntPtr unmanagedObj, IntPtr unmanagedObjArgs, Int32 argsTypeIndex, String eventName) System.Exception System.Collections.Generic.KeyNotFoundExceptionrnrnrnWCFService.DGServiceClient sc = new Sample.WCFService.DGServiceClient(); rn此处代码有误rn并显示警告:rn警告 2 自定义工具警告: 没有找到与 Silverlight 3 兼容的端点。生成的客户端类将不可用,rn除非通过构造函数提供端点信息。 ..\Sample\Service References\WCFService\Reference.svcmap rnrnrn尝试不停引入删除更新WCF服务,没用rnrn等解决方案,谢谢rn

没有更多推荐了,返回首页

私密
私密原因:
请选择设置私密原因
  • 广告
  • 抄袭
  • 版权
  • 政治
  • 色情
  • 无意义
  • 其他
其他原因:
120
出错啦
系统繁忙,请稍后再试