protocol NoteProtocol { var body: Nsstring? { get set } var createdAt: NSDate? { get set } var entityID: Nsstring? { get set } var modifIEdAt: NSDate? { get set } var Title: Nsstring? { get set } // class methods class func insertNewNoteInManagedobjectContext(managedobjectContext: NSManagedobjectContext!) -> NoteProtocol class func noteFromNoteEntity(noteEntity: NSManagedobject) -> NoteProtocol // instance methods func update(#Title: String,body: String) func deleteInManagedobjectContext(managedobjectContext: NSManagedobjectContext!)}
你好
这是我在GitHub上找到的一段代码.在这个协议中,类方法和实例方法之间的主要区别是什么?它们是如何定义的?
谁能帮我?
实例方法
Instance methods are functions that belong to instances of a particular class,structure,or enumeration. They support the functionality of those instances,either by provIDing ways to access and modify instance propertIEs,or by provIDing functionality related to the instance’s purpose.
即.类的实例必须调用此方法.示例:
var a:classAdoptingNoteProtocol=classAdoptingNoteProtocol()a.update()
分类方法
Instance methods,as described above,are methods that are called on an instance of a particular type. You can also define methods that are called on the type itself. These kinds of methods are called type methods. You indicate type methods for classes by writing the keyword class before the method’s func keyword,and type methods for structures and enumerations by writing the keyword static before the method’s func keyword.
它们在其他语言中被称为静态方法.要使用它们,这就是我要做的:
var b=classAdoptingNoteProtocol.noteFromNoteEntity(...)
这将返回采用NoteProtocol的类的实例.即.您不必创建类的实例来使用它们.
总结以上是内存溢出为你收集整理的Swift中的类方法和实例方法有什么区别?全部内容,希望文章能够帮你解决Swift中的类方法和实例方法有什么区别?所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)