比较 Swift 中的协议与 Java 中的接口
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/30859334/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Compare Protocol in Swift vs Interface in Java
提问by Bun
I'm going through the iOS tutorial from Apple developer page.
我正在阅读Apple 开发者页面上的 iOS 教程。
It seems to me that protocol
and interface
almost have the same functionality.
在我看来,这protocol
和interface
几乎具有相同的功能。
Are there any differences between the two?
the different usage in the project?
两者之间有什么区别吗?
项目中的不同用法?
Updated
更新
Yes, I did read the link above and I'm still not sure what the differences and usage between protocol
and interface
. When I ask a question like this, I would like to see a simple explanation about the topic. Sometime it could be tough to get everything from the documentation.
是的,我看过上面的链接,我仍然不知道什么区别之间使用protocol
和interface
。当我提出这样的问题时,我希望看到有关该主题的简单解释。有时可能很难从文档中获取所有内容。
采纳答案by Thomas Schar
Essentially protocols are very similar to Java interfaces except for:
本质上,协议与 Java 接口非常相似,除了:
- Swift protocols can also specify properties that must be implemented (i.e. fields)
- Swift protocols need to deal with value/reference through the use of the mutatingkeyword (because protocols can be implemented by structs and classes)
- you can combine protocols at any point with the protocol<> keyword. For example, declaring a function parameter that must adhere to protocol A and B as:
- Swift 协议还可以指定必须实现的属性(即字段)
- Swift 协议需要通过使用mutating关键字来处理值/引用(因为协议可以通过结构和类来实现)
- 您可以随时使用 protocol<> 关键字组合协议。例如,将必须遵守协议 A 和 B 的函数参数声明为:
.
.
func foo ( var1 : protocol<A, B> ){}
These are the immediately apparent differences for a Java developer (or at least what I've spotted so far).
对于 Java 开发人员来说,这些是显而易见的差异(或者至少我目前发现的差异)。
回答by Jeremy Chone
Complementing @Thomas Schar's answer. The Swift protocol magic comes from the extension.
补充@Thomas Schar 的回答。Swift 协议的魔法来自于扩展。
- Swift protocols can get implementations via the extension (Swift
2). Java 8 interface can have default implementations, but it cannot be done "retroactively." - In Swift, you can "retroactively" add protocol requirements (and
its implementations if needed) to any class or structure. - Swift protocols do not follow the generic (i.e <..>) customization pattern,
but a typealias scheme (i.e. Associated Types). Can be confusing at the start, but can avoid
"angle bracket blindness" in some cases. - Swift has an advanced type pattern matching, allowing to be very specific on where and how protocol requirements and extensions get applied. It can be confusing when coming from Java, but it has a lot of power.
- A swift protocol can be composed for a property/param (i.e. celebrator: protocol)
- Swift 协议可以通过扩展 (Swift
2)获得实现。Java 8 接口可以有默认实现,但不能“追溯”完成。 - 在 Swift 中,您可以“追溯”
向任何类或结构添加协议要求(及其实现,如果需要)。 - Swift 协议不遵循通用(即 <..>)定制模式,而是遵循类型别名方案(即关联类型)。一开始可能会令人困惑,但
在某些情况下可以避免“尖括号盲目”。 - Swift 具有先进的类型模式匹配,允许非常具体地说明协议要求和扩展在何处以及如何应用。来自 Java 时可能会令人困惑,但它具有强大的功能。
- 可以为属性/参数组成一个 swift 协议(即 celebrator: 协议)
One thing that got me scratching my head for a couple of hours is that not all protocols can be used as property types. For example, if you have a protocol with typealias, you cannot directly use it as a type of property (it makes sense when you think about it, but coming from Java we really want to have a property like userDao: IDao).
让我挠头几个小时的一件事是,并非所有协议都可以用作属性类型。例如,如果您有一个带有 typealias 的协议,则不能直接将其用作一种属性(仔细想想是有道理的,但是从 Java 中我们真的希望拥有一个像 userDao: IDao 这样的属性)。