鱼C论坛

 找回密码
 立即注册
查看: 2055|回复: 4

[技术交流] static class

[复制链接]
发表于 2020-6-30 09:20:52 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
本帖最后由 永恒的蓝色梦想 于 2020-6-30 18:18 编辑

简介:
模仿 C# 的静态类。
经由这个装饰器装饰的类将无法被继承,且无法实例化,而且无法包含普通方法。


示例:
  1. >>> @staticclass
  2. class p:
  3.         ...

  4. >>> p()
  5. Traceback (most recent call last):
  6.   File "<pyshell#7>", line 1, in <module>
  7.     p()
  8.   File "<pyshell#1>", line 12, in __new__
  9.     raise TypeError("Can't instantiate abstract class "+cls.__name__)
  10. TypeError: Can't instantiate abstract class p

  11. >>> class b(p):
  12.         pass

  13. Traceback (most recent call last):
  14.   File "<pyshell#10>", line 1, in <module>
  15.     class b(p):
  16.   File "<pyshell#1>", line 8, in __init_subclass__
  17.     raise TypeError("type '"+cls.__name__ +
  18. TypeError: type 'p' is not an acceptable base type

  19. >>> @staticclass
  20. class f:
  21.         def function(self):
  22.                 return 0

  23.        
  24. Traceback (most recent call last):
  25.   File "<pyshell#15>", line 2, in <module>
  26.     class f:
  27.   File "<pyshell#1>", line 4, in staticclass
  28.     raise TypeError(
  29. TypeError: Can't define non-static members in static class f

  30. >>> @staticclass
  31. class n:
  32.         @staticmethod
  33.         def fads():
  34.                 return 6
  35.        
  36.         @classmethod
  37.         def ewfds(cls):
  38.                 return 8
  39.        
  40.         list=[]
  41.        
  42.         tuple=()
  43.        
  44.         string=''
  45.        
  46.         set=set()
  47.        
  48.         dict={}
  49.        
  50.         class t:
  51.                 pass

  52.        
  53. >>> n.fads
  54. <function n.fads at 0x000001583E9A89D0>
  55. >>> n.ewfds
  56. <bound method n.ewfds of <class '__main__.n'>>
  57. >>> n.list
  58. []
  59. >>> n.tuple
  60. ()
  61. >>> n.string
  62. ''
  63. >>> n.set
  64. set()
  65. >>> n.dict
  66. {}
  67. >>> n.t
  68. <class '__main__.n.t'>
复制代码



代码:
  1. function = (lambda: None).__class__


  2. def staticclass(cls: type, / ) -> type:
  3.     for i in vars(cls).values():
  4.         if i.__class__ is function:
  5.             raise TypeError(
  6.                 "Can't define non-static members in static class "+cls.__name__)

  7.     def __init_subclass__(ncls: type, / ) -> None:
  8.         raise TypeError("type '"+cls.__name__ +
  9.                         "' is not an acceptable base type")

  10.     def __new__(ncls: type, / , *args, **kwargs) -> None:
  11.         raise TypeError("Can't instantiate abstract class "+cls.__name__)

  12.     cls.__init_subclass__ = classmethod(__init_subclass__)
  13.     cls.__new__ = staticmethod(__new__)

  14.     return cls
复制代码

评分

参与人数 1荣誉 +3 鱼币 +3 收起 理由
heidern0612 + 3 + 3 鱼C有你更精彩^_^

查看全部评分

小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2020-6-30 09:39:29 | 显示全部楼层
其实经过装饰的类还可以用作命名空间
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-6-30 10:03:46 | 显示全部楼层

回帖奖励 +25 鱼币

额,新名词‘静态类’首次接触,无法被继承,且无法实例化,而且无法包含普通方法?
那具体有什么可以去做使用的地方呢?
给某些数据赋值初始值?如果代码量躲起来,某些数据值可能在变动,平常是通过自己赋值初始值,但如果需要赋值的数据很多时可以用静态类?
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2020-6-30 10:11:05 | 显示全部楼层
yhhpf 发表于 2020-6-30 10:03
额,新名词‘静态类’首次接触,无法被继承,且无法实例化,而且无法包含普通方法?
那具体有什么可以去做 ...

WOC 我鱼币设错了

主要是为了实现类似命名空间的一个作用。
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-6-30 10:19:55 | 显示全部楼层
永恒的蓝色梦想 发表于 2020-6-30 10:11
WOC 我鱼币设错了

主要是为了实现类似命名空间的一个作用。

哇,还有鱼币奖励,我赚到了

我先百度下命名空间,静态类哈哈,了解下先~
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-7-12 21:51

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表