-
Notifications
You must be signed in to change notification settings - Fork 1k
Simplify Chinese translation of Scala Tour: self-types.md #1205
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 1 commit
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
--- | ||
layout: tour | ||
title: Self-types | ||
title: 自类型 | ||
|
||
discourse: false | ||
|
||
|
@@ -13,3 +13,27 @@ language: zh-cn | |
next-page: implicit-parameters | ||
previous-page: compound-types | ||
--- | ||
自类型用于声明一个特质必须混入其他特质,尽管该特质没有直接扩展其他特质。 这使得所依赖的成员可以在没有导入的情况下使用。 | ||
|
||
自类型是一种缩小 `this` 类型或 `this` 别名类型的方法。 语法看起来像普通函数语法,但是意义完全不一样。 | ||
|
||
要在特质中使用自类型,写一个标识符,跟上要混入的另一个特质,以及 `=>`(例如 `someIdentifier: SomeOtherTrait =>`)。 | ||
```tut | ||
trait User { | ||
def username: String | ||
} | ||
trait Tweeter { | ||
this: User => // 重新分配 this 的类型 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 重新赋予 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thanks a lot. |
||
def tweet(tweetText: String) = println(s"$username: $tweetText") | ||
} | ||
class VerifiedTweeter(val username_ : String) extends Tweeter with User { // 我们混入特质 User 因为 Tweeter 需要 | ||
def username = s"real $username_" | ||
} | ||
val realBeyoncé = new VerifiedTweeter("Beyoncé") | ||
realBeyoncé.tweet("Just spilled my glass of lemonade") // 打印出 "real Beyoncé: Just spilled my glass of lemonade" | ||
``` | ||
|
||
因为我们在特质 `trait Tweeter` 中定义了 `this: User =>`,现在变量 `username` 可以在 `tweet` 方法内使用。 这也意味着,由于 `VerifiedTweeter` 继承了 `Tweeter`,它还必须混入 `User`(使用 `with User`)。 |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
缩小
->细化
/强化
? 我感觉强化
更好些,WDYT @realwunanThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this
或this
别名之类型的方法WDYT?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
个人觉得细化更好些,英文原文用的是 narrow ,并且自类型确实限定了应该混入的类型