所以,我只想知道是否有人知道JavaScript中init()语句的特殊含义和用法,从来没有真正知道,有点像newb.
我有一个有很多__init__参数的基类:
def BaseClass(object):
def __init__(self, a, b, c, d, e, f, ...):
self._a=a+b
self._b=b if b else a
...
Run Code Online (Sandbox Code Playgroud)
所有继承类都应该运行__init__基类的方法.
我可以__init__()在每个将调用超类的继承类中编写一个方法__init__,但这将是一个严重的代码重复:
def A(BaseClass):
def __init__(self, a, b, c, d, e, f, ...):
super(A, self).__init__(a, b, c, d, e, f, ...)
def B(BaseClass):
def __init__(self, a, b, c, d, e, f, ...):
super(A, self).__init__(a, b, c, d, e, f, ...)
def C(BaseClass):
def __init__(self, a, b, c, d, e, f, ...):
super(A, self).__init__(a, …Run Code Online (Sandbox Code Playgroud) 我正在与Git挣扎,我似乎无法添加我的文件.我跑来ls显示文件在当前目录中,然后运行git add .然后git status显示"没有提交".
JJ-Computer:first_app JJ$ git init
Reinitialized existing Git repository in /Users/JJ/rails_projects/first_app/.git/
JJ-Computer:first_app JJ$ ls
Diary.txt README.rdoc config.ru log tmp
Gemfile Rakefile db public vendor
Gemfile.lock app doc script
README config lib test
JJ-Computer:first_app JJ$ git add .
JJ-Computer:first_app Jenn$ git status
# On branch master
nothing to commit (working directory clean)
JJ-Computer:first_app JJ$
Run Code Online (Sandbox Code Playgroud) 当我为项目发出“pod init”命令时,我遇到此错误。终端设置为“使用 Rosetta 打开”。
图片:
感谢您的帮助。
(base) airpc@192 InstaClone % pod init
/Library/Ruby/Gems/2.6.0/gems/cocoapods-1.11.3/lib/cocoapods/user_interface/error_report.rb:34:in `force_encoding': can't modify frozen String (FrozenError)
from /Library/Ruby/Gems/2.6.0/gems/cocoapods-1.11.3/lib/cocoapods/user_interface/error_report.rb:34:in `report'
from /Library/Ruby/Gems/2.6.0/gems/cocoapods-1.11.3/lib/cocoapods/command.rb:66:in `report_error'
from /Library/Ruby/Gems/2.6.0/gems/claide-1.1.0/lib/claide/command.rb:396:in `handle_exception'
from /Library/Ruby/Gems/2.6.0/gems/claide-1.1.0/lib/claide/command.rb:337:in `rescue in run'
from /Library/Ruby/Gems/2.6.0/gems/claide-1.1.0/lib/claide/command.rb:324:in `run'
from /Library/Ruby/Gems/2.6.0/gems/cocoapods-1.11.3/lib/cocoapods/command.rb:52:in `run'
from /Library/Ruby/Gems/2.6.0/gems/cocoapods-1.11.3/bin/pod:55:in `<top (required)>'
from /usr/local/bin/pod:23:in `load'
from /usr/local/bin/pod:23:in `<main>'
/Library/Ruby/Gems/2.6.0/gems/xcodeproj-1.21.0/lib/xcodeproj/project.rb:228:in `initialize_from_file': [Xcodeproj] Unknown object version (56). (RuntimeError)
from /Library/Ruby/Gems/2.6.0/gems/xcodeproj-1.21.0/lib/xcodeproj/project.rb:113:in `open'
from /Library/Ruby/Gems/2.6.0/gems/cocoapods-1.11.3/lib/cocoapods/command/init.rb:41:in `validate!'
from /Library/Ruby/Gems/2.6.0/gems/claide-1.1.0/lib/claide/command.rb:333:in `run'
from /Library/Ruby/Gems/2.6.0/gems/cocoapods-1.11.3/lib/cocoapods/command.rb:52:in `run'
from /Library/Ruby/Gems/2.6.0/gems/cocoapods-1.11.3/bin/pod:55:in `<top (required)>'
from /usr/local/bin/pod:23:in `load'
from …Run Code Online (Sandbox Code Playgroud) 我正试图从其内部访问模块的数据__main__.py.
结构如下:
mymod/
__init__.py
__main__.py
Run Code Online (Sandbox Code Playgroud)
现在,如果我__init__.py像这样公开一个变量:
__all__ = ['foo']
foo = {'bar': 'baz'}
Run Code Online (Sandbox Code Playgroud)
如何访问foo从__main__.py?
我总是设置类似这样的元类:
class SomeMetaClass(type):
def __new__(cls, name, bases, dict):
#do stuff here
Run Code Online (Sandbox Code Playgroud)
但我刚刚遇到了一个定义如下的元类:
class SomeMetaClass(type):
def __init__(self, name, bases, dict):
#do stuff here
Run Code Online (Sandbox Code Playgroud)
有什么理由比较喜欢一个吗?
更新:请记住,我正在询问有关使用__new__和__init__在元类中的问题.我已经理解了另一个班级中他们之间的区别.但是在元类中,我不能__new__用来实现缓存,因为__new__只在元类中创建类时调用它.
在C#/ Winforms中,如何在应用程序启动时设置默认焦点?
让我们看看简单的源代码:
import SwiftUI
struct MyView: View {
@State var mapState: Int
init(inputMapState: Int)
{
mapState = inputMapState //Error: 'self' used before all stored properties are initialized
} //Error: Return from initializer without initializing all stored properties
var body: some View {
Text("Hello World!")
}
}
Run Code Online (Sandbox Code Playgroud)
这里需要init函数,因为我想在这里做一些数据加载,但是有一个问题,@State这里不能初始化变量!我该怎么办?也许这是一个非常简单的问题,但我不知道该怎么做。非常感谢!
在设计类层次结构时,有时子类添加了一个新initWithSomeNewParam方法,并且希望禁用对init从超类继承的旧方法的调用.
首先,我在这里阅读了这个问题,其中提出的替代方法是覆盖init在运行时抛出异常,或覆盖并设置属性的默认值.在我的情况下,我不想提供默认值,我想清楚地表明不应该调用旧方法,而应该使用带参数的新方法.
因此运行时异常很好,但除非代码被调试,否则团队中的其他程序员无法注意到旧方法不再用于使用.
如果我是正确的,那么就没有办法将方法标记为"私有".所以,除了添加评论之外,有没有办法做到这一点?
提前致谢.
我意识到NSDecimalNumber继承自NSNumber.
但是,我正在努力弄清楚如何从NSNumber初始化NSDecimalNumber或如何将NSNumber转换为NSDecimalNumber.
我试过了
NSDecimalNumber* d = [aNumber copy];
Run Code Online (Sandbox Code Playgroud)
aNumberNSNumber对象在哪里无济于事.
我错过了什么吗?
谢谢.
init ×10
python ×3
constructor ×2
objective-c ×2
add ×1
c# ×1
cocoa-touch ×1
controls ×1
focus ×1
git ×1
inheritance ×1
ios ×1
iphone ×1
javascript ×1
metaclass ×1
new-operator ×1
nsnumber ×1
state ×1
swiftui ×1
winforms ×1