dataclass中Java中的python等效
是否可以
class MyAbstract {
final int myFieldSomebodyHasToDefine;
}
class MyAbstractImplementation extends MyAbstract {
final int myFieldSomebodyHasToDefine = 5;
}
在Python中使用Dataclasses之类的东西?
Is it possible to have something like
class MyAbstract {
final int myFieldSomebodyHasToDefine;
}
class MyAbstractImplementation extends MyAbstract {
final int myFieldSomebodyHasToDefine = 5;
}
using dataclasses in python?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您在版本3.8之前与Python解释器合作,则没有直接的方式。但是,自Python 3.8以来,最终的装饰器已添加到该语言中。从Python的打字模块导入后,您可以将其用于方法和类。
您也可以将最终类型用于值。
这是一个示例
重要说明: python不会迫使开发人员最终和 final 。您仍然可以根据自己的意愿改变价值。对于开发人员而言,大多数信息的装饰者。
有关更多信息,您可以访问: https://peps.pypython.org/pep-0591/ < /a>
更新:这也是 dataclass 的实例,
如您所见, name 是最终字段。但是,您必须将默认值的最终值低于所有字段,而无需初始值。
If you are working with a python interpreter before version 3.8, there is no straightforward way. However, since python 3.8, the final decorator has been added to the language. After importing it from the typing module in python, you can use it for methods and classes.
You may also use FINAL type for values.
Here is an example
Important note: Python does not force the developer with final and FINAL. You can yet change the values upon your wish. The decorators of mostly informative for developers.
For more information, you may visit: https://peps.python.org/pep-0591/
Update: This is also an instance for dataclass
As you can see, name is a final field. However, you must put the final values with a default value below all of the fields without an initial value.