我试图用构造函数声明类,但是似乎与该语法冲突并声明类成员。
有关这些如何结合在一起的任何建议吗?
class Person (aName:String) {
var name : String
get() = this.name
set(myName) {this.name = myName}
init {
this.name = aName
}
}
答案 0 :(得分:3)
您可以通过set(myName) {this.name = myName}
在设置器中使用setter。这是一个递归调用,而不是你应该做的。而是像这样使用field
accessor:
name: String? = null
set(myName) {
field = myName
}
但实际上,你甚至不需要这个。您可以在主构造函数中声明name
应该是您的类的属性:
class Person(var name: String)
答案 1 :(得分:1)
你不需要任何这些。您只需在构造函数中将Traceback (most recent call last):
File "/tmp/env/lib/python2.7/site-packages/gunicorn/arbiter.py", line 578, in spawn_worker
worker.init_process()
File "/tmp/env/lib/python2.7/site-packages/gunicorn/workers/base.py", line 126, in init_process
self.load_wsgi()
File "/tmp/env/lib/python2.7/site-packages/gunicorn/workers/base.py", line 135, in load_wsgi
self.wsgi = self.app.wsgi()
File "/tmp/env/lib/python2.7/site-packages/gunicorn/app/base.py", line 67, in wsgi
self.callable = self.load()
File "/tmp/env/lib/python2.7/site-packages/gunicorn/app/wsgiapp.py", line 65, in load
return self.load_wsgiapp()
File "/tmp/env/lib/python2.7/site-packages/gunicorn/app/wsgiapp.py", line 52, in load_wsgiapp
return util.import_app(self.app_uri)
File "/tmp/env/lib/python2.7/site-packages/gunicorn/util.py", line 352, in import_app
__import__(module)
ImportError: Import by filename is not supported.
定义为name
,Kotlin就会将其作为属性公开。
var
答案 2 :(得分:0)
只是为了添加其他人所说的内容,如果你想将aName
作为一个单独的构造函数参数,这里有一个更好的语法:
class Person (aName:String) {
var name = aName
}