713 lines
31 KiB
HTML
713 lines
31 KiB
HTML
<!DOCTYPE HTML>
|
||
<html lang="en-US" manifest="../manifest.appcache">
|
||
|
||
<head prefix="og: http://ogp.me/ns# book: http://ogp.me/ns/book#">
|
||
|
||
|
||
<meta charset="UTF-8">
|
||
<title>《The Swift Programming Language》中文版</title>
|
||
|
||
<meta content="text/html; charset=utf-8" http-equiv="Content-Type">
|
||
<meta name="robots" content="index, follow">
|
||
<meta name="author" content="">
|
||
<meta name="description" content="Swift 中文翻译组:364279588(要求对翻译感兴趣)">
|
||
<meta name="keywords" content="gitbook,github" >
|
||
<meta name="generator" content="www.gitbook.io">
|
||
|
||
|
||
<link rel="next" href="../chapter3/09_Summary_of_the_Grammar.html" />
|
||
|
||
|
||
<link rel="prev" href="../chapter3/07_Patterns.html" />
|
||
|
||
|
||
<meta property="og:title" content="泛型参数 | The Swift Programming Language 中文版">
|
||
<meta property="og:site_name" content="The Swift Programming Language 中文版">
|
||
<meta property="og:type" content="book">
|
||
<meta property="og:locale" content="en_US">
|
||
|
||
<meta property="book:author" content="https://github.com/">
|
||
<meta property="book:tag" content="GitBook">
|
||
|
||
<meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
|
||
<meta name="apple-mobile-web-app-capable" content="yes">
|
||
<meta name="apple-mobile-web-app-status-bar-style" content="black">
|
||
|
||
<link rel="shortcut icon" href="../gitbook/images/favicon.ico" type="image/x-icon">
|
||
|
||
|
||
</head>
|
||
<body>
|
||
|
||
|
||
|
||
<link rel="stylesheet" href="../gitbook/style.css">
|
||
|
||
|
||
|
||
|
||
<div class="book" data-level="3.9" data-basepath=".." data-revision="1402556298428">
|
||
<div class="book-header">
|
||
<!-- Actions Left -->
|
||
<a href="#" class="btn pull-left toggle-summary" aria-label="Toggle summary"><i class="fa fa-align-justify"></i></a>
|
||
|
||
<a href="https://github.com/null" target="_blank" class="btn pull-left home-bookmark" aria-label="GitHub home"><i class="fa fa-bookmark-o"></i></a>
|
||
|
||
<a href="#" class="btn pull-left toggle-search" aria-label="Toggle search"><i class="fa fa-search"></i></a>
|
||
<span id="font-settings-wrapper">
|
||
<a href="#" class="btn pull-left toggle-font-settings" aria-label="Toggle font settings"><i class="fa fa-font"></i>
|
||
</a>
|
||
<div class="dropdown-menu font-settings">
|
||
<div class="dropdown-caret">
|
||
<span class="caret-outer"></span>
|
||
<span class="caret-inner"></span>
|
||
</div>
|
||
|
||
<div class="btn-group btn-block">
|
||
<button id="reduce-font-size" class="btn btn-default">A</button>
|
||
<button id="enlarge-font-size" class="btn btn-default">A</button>
|
||
</div>
|
||
|
||
<ul class="list-group font-family-list">
|
||
<li class="list-group-item" data-font="0">Serif</li>
|
||
<li class="list-group-item" data-font="1">Sans</li>
|
||
</ul>
|
||
|
||
<div class="btn-group btn-group-xs btn-block color-theme-list">
|
||
<button type="button" class="btn btn-default" id="color-theme-preview-0" data-theme="0">White</button>
|
||
<button type="button" class="btn btn-default" id="color-theme-preview-1" data-theme="1">Sepia</button>
|
||
<button type="button" class="btn btn-default" id="color-theme-preview-2" data-theme="2">Night</button>
|
||
</div>
|
||
</div>
|
||
|
||
</span>
|
||
|
||
<!-- Actions Right -->
|
||
|
||
<a href="#" target="_blank" class="btn pull-right google-plus-sharing-link sharing-link" data-sharing="google-plus" aria-label="Share on Google Plus"><i class="fa fa-google-plus"></i></a>
|
||
|
||
|
||
<a href="#" target="_blank" class="btn pull-right facebook-sharing-link sharing-link" data-sharing="facebook" aria-label="Share on Facebook"><i class="fa fa-facebook"></i></a>
|
||
|
||
|
||
<a href="#" target="_blank" class="btn pull-right twitter-sharing-link sharing-link" data-sharing="twitter" aria-label="Share on Twitter"><i class="fa fa-twitter"></i></a>
|
||
|
||
|
||
|
||
|
||
<!-- Title -->
|
||
<h1>
|
||
<i class="fa fa-spinner fa-spin"></i>
|
||
<a href="../" >The Swift Programming Language 中文版</a>
|
||
</h1>
|
||
</div>
|
||
|
||
|
||
|
||
<div class="book-summary">
|
||
<div class="book-search">
|
||
<input type="text" placeholder="Search" class="form-control" />
|
||
</div>
|
||
<ul class="summary">
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
<li data-level="0" data-path="index.html">
|
||
<a href="../"><i class="fa fa-check"></i> 序</a>
|
||
</li>
|
||
|
||
|
||
<li class="chapter " data-level="1" data-path="chapter1/chapter1.html">
|
||
|
||
<a href="../chapter1/chapter1.html">
|
||
<i class="fa fa-check"></i> <b>1.</b> 欢迎使用 Swift
|
||
</a>
|
||
|
||
|
||
<ul class="articles">
|
||
|
||
|
||
<li class="chapter " data-level="1.1" data-path="chapter1/01_swift.html">
|
||
|
||
<a href="../chapter1/01_swift.html">
|
||
<i class="fa fa-check"></i> <b>1.1.</b> 关于 Swift
|
||
</a>
|
||
|
||
|
||
</li>
|
||
|
||
<li class="chapter " data-level="1.2" data-path="chapter1/02_a_swift_tour.html">
|
||
|
||
<a href="../chapter1/02_a_swift_tour.html">
|
||
<i class="fa fa-check"></i> <b>1.2.</b> Swift 初见
|
||
</a>
|
||
|
||
|
||
</li>
|
||
|
||
|
||
</ul>
|
||
|
||
</li>
|
||
|
||
<li class="chapter " data-level="2" data-path="chapter2/chapter2.html">
|
||
|
||
<a href="../chapter2/chapter2.html">
|
||
<i class="fa fa-check"></i> <b>2.</b> Swift 教程
|
||
</a>
|
||
|
||
|
||
<ul class="articles">
|
||
|
||
|
||
<li class="chapter " data-level="2.1" data-path="chapter2/01_The_Basics.html">
|
||
|
||
<a href="../chapter2/01_The_Basics.html">
|
||
<i class="fa fa-check"></i> <b>2.1.</b> 基础部分
|
||
</a>
|
||
|
||
|
||
</li>
|
||
|
||
<li class="chapter " data-level="2.2" data-path="chapter2/02_Basic_Operators.html">
|
||
|
||
<a href="../chapter2/02_Basic_Operators.html">
|
||
<i class="fa fa-check"></i> <b>2.2.</b> 基本运算符
|
||
</a>
|
||
|
||
|
||
</li>
|
||
|
||
<li class="chapter " data-level="2.3" data-path="chapter2/03_Strings_and_Characters.html">
|
||
|
||
<a href="../chapter2/03_Strings_and_Characters.html">
|
||
<i class="fa fa-check"></i> <b>2.3.</b> 字符串和字符
|
||
</a>
|
||
|
||
|
||
</li>
|
||
|
||
<li class="chapter " data-level="2.4" data-path="chapter2/04_Collection_Types.html">
|
||
|
||
<a href="../chapter2/04_Collection_Types.html">
|
||
<i class="fa fa-check"></i> <b>2.4.</b> 集合类型
|
||
</a>
|
||
|
||
|
||
</li>
|
||
|
||
<li class="chapter " data-level="2.5" data-path="chapter2/05_Control_Flow.html">
|
||
|
||
<a href="../chapter2/05_Control_Flow.html">
|
||
<i class="fa fa-check"></i> <b>2.5.</b> 控制流
|
||
</a>
|
||
|
||
|
||
</li>
|
||
|
||
<li class="chapter " data-level="2.6" data-path="chapter2/06_Functions.html">
|
||
|
||
<a href="../chapter2/06_Functions.html">
|
||
<i class="fa fa-check"></i> <b>2.6.</b> 函数
|
||
</a>
|
||
|
||
|
||
</li>
|
||
|
||
<li class="chapter " data-level="2.7" data-path="chapter2/07_Closures.html">
|
||
|
||
<a href="../chapter2/07_Closures.html">
|
||
<i class="fa fa-check"></i> <b>2.7.</b> 闭包
|
||
</a>
|
||
|
||
|
||
</li>
|
||
|
||
<li class="chapter " data-level="2.8" data-path="chapter2/08_Enumerations.html">
|
||
|
||
<a href="../chapter2/08_Enumerations.html">
|
||
<i class="fa fa-check"></i> <b>2.8.</b> 枚举
|
||
</a>
|
||
|
||
|
||
</li>
|
||
|
||
<li class="chapter " data-level="2.9" data-path="chapter2/09_Classes_and_Structures.html">
|
||
|
||
<a href="../chapter2/09_Classes_and_Structures.html">
|
||
<i class="fa fa-check"></i> <b>2.9.</b> 类和结构体
|
||
</a>
|
||
|
||
|
||
</li>
|
||
|
||
<li class="chapter " data-level="2.10" data-path="chapter2/10_Properties.html">
|
||
|
||
<a href="../chapter2/10_Properties.html">
|
||
<i class="fa fa-check"></i> <b>2.10.</b> 属性
|
||
</a>
|
||
|
||
|
||
</li>
|
||
|
||
<li class="chapter " data-level="2.11" data-path="chapter2/11_Methods.html">
|
||
|
||
<a href="../chapter2/11_Methods.html">
|
||
<i class="fa fa-check"></i> <b>2.11.</b> 方法
|
||
</a>
|
||
|
||
|
||
</li>
|
||
|
||
<li class="chapter " data-level="2.12" data-path="chapter2/12_Subscripts.html">
|
||
|
||
<a href="../chapter2/12_Subscripts.html">
|
||
<i class="fa fa-check"></i> <b>2.12.</b> 附属脚本
|
||
</a>
|
||
|
||
|
||
</li>
|
||
|
||
<li class="chapter " data-level="2.13" data-path="chapter2/13_Inheritance.html">
|
||
|
||
<a href="../chapter2/13_Inheritance.html">
|
||
<i class="fa fa-check"></i> <b>2.13.</b> 继承
|
||
</a>
|
||
|
||
|
||
</li>
|
||
|
||
<li class="chapter " data-level="2.14" data-path="chapter2/14_Initialization.html">
|
||
|
||
<a href="../chapter2/14_Initialization.html">
|
||
<i class="fa fa-check"></i> <b>2.14.</b> 构造过程
|
||
</a>
|
||
|
||
|
||
</li>
|
||
|
||
<li class="chapter " data-level="2.15" data-path="chapter2/15_Deinitialization.html">
|
||
|
||
<a href="../chapter2/15_Deinitialization.html">
|
||
<i class="fa fa-check"></i> <b>2.15.</b> 析构过程
|
||
</a>
|
||
|
||
|
||
</li>
|
||
|
||
<li class="chapter " data-level="2.16" data-path="chapter2/16_Automatic_Reference_Counting.html">
|
||
|
||
<a href="../chapter2/16_Automatic_Reference_Counting.html">
|
||
<i class="fa fa-check"></i> <b>2.16.</b> 自动引用计数
|
||
</a>
|
||
|
||
|
||
</li>
|
||
|
||
<li class="chapter " data-level="2.17" data-path="chapter2/17_Optional_Chaining.html">
|
||
|
||
<a href="../chapter2/17_Optional_Chaining.html">
|
||
<i class="fa fa-check"></i> <b>2.17.</b> 可选链
|
||
</a>
|
||
|
||
|
||
</li>
|
||
|
||
<li class="chapter " data-level="2.18" data-path="chapter2/18_Type_Casting.html">
|
||
|
||
<a href="../chapter2/18_Type_Casting.html">
|
||
<i class="fa fa-check"></i> <b>2.18.</b> 类型检查
|
||
</a>
|
||
|
||
|
||
</li>
|
||
|
||
<li class="chapter " data-level="2.19" data-path="chapter2/19_Nested_Types.html">
|
||
|
||
<a href="../chapter2/19_Nested_Types.html">
|
||
<i class="fa fa-check"></i> <b>2.19.</b> 类型嵌套
|
||
</a>
|
||
|
||
|
||
</li>
|
||
|
||
<li class="chapter " data-level="2.20" data-path="chapter2/20_Extensions.html">
|
||
|
||
<a href="../chapter2/20_Extensions.html">
|
||
<i class="fa fa-check"></i> <b>2.20.</b> 扩展
|
||
</a>
|
||
|
||
|
||
</li>
|
||
|
||
<li class="chapter " data-level="2.21" data-path="chapter2/21_Protocols.html">
|
||
|
||
<a href="../chapter2/21_Protocols.html">
|
||
<i class="fa fa-check"></i> <b>2.21.</b> 协议
|
||
</a>
|
||
|
||
|
||
</li>
|
||
|
||
<li class="chapter " data-level="2.22" data-path="chapter2/22_Generics.html">
|
||
|
||
<a href="../chapter2/22_Generics.html">
|
||
<i class="fa fa-check"></i> <b>2.22.</b> 泛型
|
||
</a>
|
||
|
||
|
||
</li>
|
||
|
||
<li class="chapter " data-level="2.23" data-path="chapter2/23_Advanced_Operators.html">
|
||
|
||
<a href="../chapter2/23_Advanced_Operators.html">
|
||
<i class="fa fa-check"></i> <b>2.23.</b> 高级操作符
|
||
</a>
|
||
|
||
|
||
</li>
|
||
|
||
|
||
</ul>
|
||
|
||
</li>
|
||
|
||
<li class="chapter " data-level="3" data-path="chapter3/chapter3.html">
|
||
|
||
<a href="../chapter3/chapter3.html">
|
||
<i class="fa fa-check"></i> <b>3.</b> 语言参考
|
||
</a>
|
||
|
||
|
||
<ul class="articles">
|
||
|
||
|
||
<li class="chapter " data-level="3.1" data-path="chapter3/01_About_the_Language_Reference.html">
|
||
|
||
<a href="../chapter3/01_About_the_Language_Reference.html">
|
||
<i class="fa fa-check"></i> <b>3.1.</b> 关于语言参考
|
||
</a>
|
||
|
||
|
||
</li>
|
||
|
||
<li class="chapter " data-level="3.2" data-path="chapter3/02_Lexical_Structure.html">
|
||
|
||
<a href="../chapter3/02_Lexical_Structure.html">
|
||
<i class="fa fa-check"></i> <b>3.2.</b> 词法结构
|
||
</a>
|
||
|
||
|
||
</li>
|
||
|
||
<li class="chapter " data-level="3.3" data-path="chapter3/03_Types.html">
|
||
|
||
<a href="../chapter3/03_Types.html">
|
||
<i class="fa fa-check"></i> <b>3.3.</b> 类型
|
||
</a>
|
||
|
||
|
||
</li>
|
||
|
||
<li class="chapter " data-level="3.4" data-path="chapter3/04_Expressions.html">
|
||
|
||
<a href="../chapter3/04_Expressions.html">
|
||
<i class="fa fa-check"></i> <b>3.4.</b> 表达式
|
||
</a>
|
||
|
||
|
||
</li>
|
||
|
||
<li class="chapter " data-level="3.5" data-path="chapter3/10_Statements.html">
|
||
|
||
<a href="../chapter3/10_Statements.html">
|
||
<i class="fa fa-check"></i> <b>3.5.</b> 语句
|
||
</a>
|
||
|
||
|
||
</li>
|
||
|
||
<li class="chapter " data-level="3.6" data-path="chapter3/05_Declarations.html">
|
||
|
||
<a href="../chapter3/05_Declarations.html">
|
||
<i class="fa fa-check"></i> <b>3.6.</b> 声明
|
||
</a>
|
||
|
||
|
||
</li>
|
||
|
||
<li class="chapter " data-level="3.7" data-path="chapter3/06_Attributes.html">
|
||
|
||
<a href="../chapter3/06_Attributes.html">
|
||
<i class="fa fa-check"></i> <b>3.7.</b> 特性
|
||
</a>
|
||
|
||
|
||
</li>
|
||
|
||
<li class="chapter " data-level="3.8" data-path="chapter3/07_Patterns.html">
|
||
|
||
<a href="../chapter3/07_Patterns.html">
|
||
<i class="fa fa-check"></i> <b>3.8.</b> 模式
|
||
</a>
|
||
|
||
|
||
</li>
|
||
|
||
<li class="chapter " data-level="3.9" data-path="chapter3/08_Generic_Parameters_and_Arguments.html">
|
||
|
||
<a href="../chapter3/08_Generic_Parameters_and_Arguments.html">
|
||
<i class="fa fa-check"></i> <b>3.9.</b> 泛型参数
|
||
</a>
|
||
|
||
|
||
</li>
|
||
|
||
<li class="chapter " data-level="3.10" data-path="chapter3/09_Summary_of_the_Grammar.html">
|
||
|
||
<a href="../chapter3/09_Summary_of_the_Grammar.html">
|
||
<i class="fa fa-check"></i> <b>3.10.</b> 语法总结
|
||
</a>
|
||
|
||
|
||
</li>
|
||
|
||
|
||
</ul>
|
||
|
||
</li>
|
||
|
||
|
||
|
||
|
||
<li class="divider"></li>
|
||
<li>
|
||
<a href="http://www.gitbook.io/" target="blank" class="gitbook-link">Generated using GitBook</a>
|
||
</li>
|
||
<li style="margin-left:15%;"> <iframe src="http://ghbtns.com/github-btn.html?user=numbbbbb&repo=the-swift-programming-language-in-chinese&type=watch&count=true&size=large"
|
||
allowtransparency="true" frameborder="0" scrolling="0" width="170" height="30"></iframe></li>
|
||
</ul>
|
||
</div>
|
||
|
||
<div class="book-body">
|
||
<div class="body-inner">
|
||
<div class="page-wrapper" tabindex="-1">
|
||
<div class="book-progress">
|
||
<div class="bar">
|
||
<div class="inner" style="width: 100%;min-width: 97.36842105263158%;"></div>
|
||
</div>
|
||
<div class="chapters">
|
||
|
||
<a href="../index.html" title="Introduction" class="chapter done new-chapter" data-progress="0" style="left: 0%;"></a>
|
||
|
||
<a href="../chapter1/chapter1.html" title="欢迎使用 Swift" class="chapter done new-chapter" data-progress="1" style="left: 2.6315789473684212%;"></a>
|
||
|
||
<a href="../chapter1/01_swift.html" title="关于 Swift" class="chapter done " data-progress="1.1" style="left: 5.2631578947368425%;"></a>
|
||
|
||
<a href="../chapter1/02_a_swift_tour.html" title="Swift 初见" class="chapter done " data-progress="1.2" style="left: 7.894736842105263%;"></a>
|
||
|
||
<a href="../chapter2/chapter2.html" title="Swift 教程" class="chapter done new-chapter" data-progress="2" style="left: 10.526315789473685%;"></a>
|
||
|
||
<a href="../chapter2/01_The_Basics.html" title="基础部分" class="chapter done " data-progress="2.1" style="left: 13.157894736842104%;"></a>
|
||
|
||
<a href="../chapter2/10_Properties.html" title="属性" class="chapter done " data-progress="2.10" style="left: 15.789473684210526%;"></a>
|
||
|
||
<a href="../chapter2/11_Methods.html" title="方法" class="chapter done " data-progress="2.11" style="left: 18.42105263157895%;"></a>
|
||
|
||
<a href="../chapter2/12_Subscripts.html" title="附属脚本" class="chapter done " data-progress="2.12" style="left: 21.05263157894737%;"></a>
|
||
|
||
<a href="../chapter2/13_Inheritance.html" title="继承" class="chapter done " data-progress="2.13" style="left: 23.68421052631579%;"></a>
|
||
|
||
<a href="../chapter2/14_Initialization.html" title="构造过程" class="chapter done " data-progress="2.14" style="left: 26.31578947368421%;"></a>
|
||
|
||
<a href="../chapter2/15_Deinitialization.html" title="析构过程" class="chapter done " data-progress="2.15" style="left: 28.94736842105263%;"></a>
|
||
|
||
<a href="../chapter2/16_Automatic_Reference_Counting.html" title="自动引用计数" class="chapter done " data-progress="2.16" style="left: 31.57894736842105%;"></a>
|
||
|
||
<a href="../chapter2/17_Optional_Chaining.html" title="可选链" class="chapter done " data-progress="2.17" style="left: 34.21052631578947%;"></a>
|
||
|
||
<a href="../chapter2/18_Type_Casting.html" title="类型检查" class="chapter done " data-progress="2.18" style="left: 36.8421052631579%;"></a>
|
||
|
||
<a href="../chapter2/19_Nested_Types.html" title="类型嵌套" class="chapter done " data-progress="2.19" style="left: 39.473684210526315%;"></a>
|
||
|
||
<a href="../chapter2/02_Basic_Operators.html" title="基本运算符" class="chapter done " data-progress="2.2" style="left: 42.10526315789474%;"></a>
|
||
|
||
<a href="../chapter2/20_Extensions.html" title="扩展" class="chapter done " data-progress="2.20" style="left: 44.73684210526316%;"></a>
|
||
|
||
<a href="../chapter2/21_Protocols.html" title="协议" class="chapter done " data-progress="2.21" style="left: 47.36842105263158%;"></a>
|
||
|
||
<a href="../chapter2/22_Generics.html" title="泛型" class="chapter done " data-progress="2.22" style="left: 50%;"></a>
|
||
|
||
<a href="../chapter2/23_Advanced_Operators.html" title="高级操作符" class="chapter done " data-progress="2.23" style="left: 52.63157894736842%;"></a>
|
||
|
||
<a href="../chapter2/03_Strings_and_Characters.html" title="字符串和字符" class="chapter done " data-progress="2.3" style="left: 55.26315789473684%;"></a>
|
||
|
||
<a href="../chapter2/04_Collection_Types.html" title="集合类型" class="chapter done " data-progress="2.4" style="left: 57.89473684210526%;"></a>
|
||
|
||
<a href="../chapter2/05_Control_Flow.html" title="控制流" class="chapter done " data-progress="2.5" style="left: 60.526315789473685%;"></a>
|
||
|
||
<a href="../chapter2/06_Functions.html" title="函数" class="chapter done " data-progress="2.6" style="left: 63.1578947368421%;"></a>
|
||
|
||
<a href="../chapter2/07_Closures.html" title="闭包" class="chapter done " data-progress="2.7" style="left: 65.78947368421052%;"></a>
|
||
|
||
<a href="../chapter2/08_Enumerations.html" title="枚举" class="chapter done " data-progress="2.8" style="left: 68.42105263157895%;"></a>
|
||
|
||
<a href="../chapter2/09_Classes_and_Structures.html" title="类和结构体" class="chapter done " data-progress="2.9" style="left: 71.05263157894737%;"></a>
|
||
|
||
<a href="../chapter3/chapter3.html" title="语言参考" class="chapter done new-chapter" data-progress="3" style="left: 73.6842105263158%;"></a>
|
||
|
||
<a href="../chapter3/01_About_the_Language_Reference.html" title="关于语言参考" class="chapter done " data-progress="3.1" style="left: 76.3157894736842%;"></a>
|
||
|
||
<a href="../chapter3/09_Summary_of_the_Grammar.html" title="语法总结" class="chapter done " data-progress="3.10" style="left: 78.94736842105263%;"></a>
|
||
|
||
<a href="../chapter3/02_Lexical_Structure.html" title="词法结构" class="chapter done " data-progress="3.2" style="left: 81.57894736842105%;"></a>
|
||
|
||
<a href="../chapter3/03_Types.html" title="类型" class="chapter done " data-progress="3.3" style="left: 84.21052631578948%;"></a>
|
||
|
||
<a href="../chapter3/04_Expressions.html" title="表达式" class="chapter done " data-progress="3.4" style="left: 86.84210526315789%;"></a>
|
||
|
||
<a href="../chapter3/10_Statements.html" title="语句" class="chapter done " data-progress="3.5" style="left: 89.47368421052632%;"></a>
|
||
|
||
<a href="../chapter3/05_Declarations.html" title="声明" class="chapter done " data-progress="3.6" style="left: 92.10526315789474%;"></a>
|
||
|
||
<a href="../chapter3/06_Attributes.html" title="特性" class="chapter done " data-progress="3.7" style="left: 94.73684210526316%;"></a>
|
||
|
||
<a href="../chapter3/07_Patterns.html" title="模式" class="chapter done " data-progress="3.8" style="left: 97.36842105263158%;"></a>
|
||
|
||
<a href="../chapter3/08_Generic_Parameters_and_Arguments.html" title="泛型参数" class="chapter done " data-progress="3.9" style="left: 100%;"></a>
|
||
|
||
</div>
|
||
</div>
|
||
|
||
<div class="page-inner">
|
||
|
||
<section class="normal" id="section-gitbook_179">
|
||
|
||
<blockquote>
|
||
<p>翻译:fd5788</p>
|
||
<p>校对:yankuangshi</p>
|
||
</blockquote>
|
||
<h1 id="-">泛型参数</h1>
|
||
<hr>
|
||
<p>本页包含内容:</p>
|
||
<ul>
|
||
<li><a href="#generic_parameter">泛型形参语句</a></li>
|
||
<li><a href="#generic_argument">泛型实参语句</a></li>
|
||
</ul>
|
||
<p>本节涉及泛型类型、泛型函数以及泛型构造器的参数,包括形参和实参。声明泛型类型、函数或构造器时,须指定相应的类型参数。类型参数相当于一个占位符,当实例化泛型类型、调用泛型函数或泛型构造器时,就用具体的类型实参替代之。</p>
|
||
<p>关于 Swift 语言的泛型概述,见<a href="../charpter2/22_Generics.html">泛型</a>(第二部分第22章)。</p>
|
||
<h2 id="-a-name-generic_parameter-a-"><a name="generic_parameter">泛型形参语句</a></h2>
|
||
<p>泛型形参语句指定泛型类型或函数的类型形参,以及这些参数的关联约束和要求。泛型形参语句用尖括号(<>)包住,并且有以下两种形式:</p>
|
||
<pre><code><generic parameter list>
|
||
<generic parameter list where requirements >
|
||
</code></pre><p>泛型形参列表中泛型形参用逗号分开,每一个采用以下形式:</p>
|
||
<pre><code>type parameter : constrain
|
||
</code></pre><p>泛型形参由两部分组成:类型形参及其后的可选约束。类型形参只是占位符类型(如T,U,V,KeyType,ValueType等)的名字而已。你可以在泛型类型、函数的其余部分或者构造器声明,以及函数或构造器的签名中使用它。</p>
|
||
<p>约束用于指明该类型形参继承自某个类或者遵守某个协议或协议的一部分。例如,在下面的泛型中,泛型形参<code>T: Comparable</code>表示任何用于替代类型形参<code>T</code>的类型实参必须满足<code>Comparable</code>协议。</p>
|
||
<pre><code>func simpleMin<T: COmparable>(x: T, y: T) -> T {
|
||
if x < y {
|
||
return y
|
||
}
|
||
return x
|
||
}
|
||
</code></pre><p>如,<code>Int</code>和<code>Double</code>均满足<code>Comparable</code>协议,该函数接受任何一种类型。与泛型类型相反,调用泛型函数或构造器时不需要指定泛型实参语句。类型实参由传递给函数或构造器的实参推断而出。</p>
|
||
<pre><code>simpleMin(17, 42) // T is inferred to be Int
|
||
simpleMin(3.14159, 2.71828) // T is inferred to be Double
|
||
</code></pre><h2 id="where-">Where语句</h2>
|
||
<p>要想对类型形参及其关联类型指定额外要求,可以在泛型形参列表之后添加<code>where</code>语句。<code>where</code>语句由关键字<code>where</code>及其后的用逗号分割的多个要求组成。</p>
|
||
<p><code>where</code>语句中的要求用于指明该类型形参继承自某个类或遵守某个协议或协议的一部分。尽管<code>where</code>语句有助于表达类型形参上的简单约束(如<code>T: Comparable</code>等同于<code>T where T: Comparable</code>,等等),但是依然可以用来对类型形参及其关联约束提供更复杂的约束。如,<code><T where T: C, T: P></code>表示泛型类型<code>T</code>继承自类<code>C</code>且遵守协议<code>P</code>。</p>
|
||
<p>如上所述,可以强制约束类型形参的关联类型遵守某个协议。<code><T: Generator where T.Element: Equatable></code>表示<code>T</code>遵守<code>Generator</code>协议,而且<code>T</code>的关联类型<code>T.Element</code>遵守<code>Eauatable</code>协议(<code>T</code>有关联类型是因为<code>Generator</code>声明了<code>Element</code>,而<code>T</code>遵守<code>Generator</code>协议)。</p>
|
||
<p>也可以用操作符<code>==</code>来指定两个类型等效的要求。例如,有这样一个约束:<code>T</code>和<code>U</code>遵守<code>Generator</code>协议,同时要求它们的关联类型等同,可以这样来表达:<code><T: Generator, U: Generator where T.Element == U.Element></code>。</p>
|
||
<p>当然,替代类型形参的类型实参必须满足所有类型形参所要求的约束和要求。</p>
|
||
<p>泛型函数或构造器可以重载,但在泛型形参语句中的类型形参必须有不同的约束或要求,抑或二者皆不同。当调用重载的泛型函数或构造器时,编译器会用这些约束来决定调用哪个重载函数或构造器。</p>
|
||
<p>泛型类可以生成一个子类,但是这个子类也必须是泛型类。</p>
|
||
<blockquote>
|
||
<p>Grammar of a generic parameter clause</p>
|
||
<p>parameter-clause → <generic-parameter-listrequirement-clause ></p>
|
||
<p>generic-parameter-list → generic-parameter generic-parameter,generic-parameter-list </p>
|
||
<p>generic-parameter → type-name</p>
|
||
<p>generic-parameter → type-name:type-identifier</p>
|
||
<p>generic-parameter → type-name:protocol-composition-type</p>
|
||
<p>requirement-clause → <code>where</code>requirement-list</p>
|
||
<p>requirement-list → requirement requirement,requirement-list</p>
|
||
<p>requirement → conformance-requirement same-type-requirement</p>
|
||
<p>conformance-requirement → type-identifier:type-identifier</p>
|
||
<p>conformance-requirement → type-identifier:protocol-composition-type</p>
|
||
<p>same-type-requirement → type-identifier==type-identifier</p>
|
||
</blockquote>
|
||
<h2 id="-a-name-generic_argument-a-"><a name="generic_argument">泛型实参语句</a></h2>
|
||
<p>泛型实参语句指定<em>泛型类型</em>的类型实参。泛型实参语句用尖括号(<>)包住,形式如下:</p>
|
||
<pre><code>< generic argument list >
|
||
</code></pre><p>泛型实参列表中类型实参有逗号分开。类型实参是实际具体类型的名字,用来替代泛型类型的泛型形参语句中的相应的类型形参。从而得到泛型类型的一个特化版本。如,Swift标准库的泛型字典类型定义如下:</p>
|
||
<pre><code> struct Dictionary<KeyTypel: Hashable, ValueType>: Collection,
|
||
DictionaryLiteralConvertible {
|
||
/* .. */
|
||
}
|
||
</code></pre><p>泛型<code>Dictionary</code>类型的特化版本,<code>Dictionary<String, Int></code>就是用具体的<code>String</code>和<code>Int</code>类型替代泛型类型<code>KeyType: Hashable</code>和<code>ValueType</code>产生的。每一个类型实参必须满足它所替代的泛型形参的所有约束,包括任何<code>where</code>语句所指定的额外的要求。上面的例子中,类型形参<code>KeyType</code>要求满足<code>Hashable</code>协议,因此<code>String</code>也必须满足<code>Hashable</code>协议。</p>
|
||
<p>可以用本身就是泛型类型的特化版本的类型实参替代类型形参(假设已满足合适的约束和要求)。例如,为了生成一个元素类型是整型数组的数组,可以用数组的特化版本<code>Array<Int></code>替代泛型类型<code>Array<T></code>的类型形参<code>T</code>来实现。</p>
|
||
<pre><code>let arrayOfArrays: Array<Array<Int>> = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
|
||
</code></pre><p>如<a href="#generic_parameter">泛型形参语句</a>所述,不能用泛型实参语句来指定泛型函数或构造器的类型实参。</p>
|
||
<blockquote>
|
||
<p>Grammar of a generic argument clause</p>
|
||
<p>generic-argument-clause → <generic-argument-list></p>
|
||
<p>generic-argument-list → generic-argument generic-argument,generic-argument-list</p>
|
||
<p>generic-argument → type</p>
|
||
</blockquote>
|
||
<p>==============================================
|
||
上篇:<a href="07_Patterns.html">模式</a></p>
|
||
<p>下篇:<a href="09_Summary_of_the_Grammar.html">语法总结</a></p>
|
||
|
||
|
||
</section>
|
||
|
||
</div>
|
||
</div>
|
||
</div>
|
||
|
||
|
||
<a href="../chapter3/07_Patterns.html" class="navigation navigation-prev " aria-label="Previous page: 模式"><i class="fa fa-angle-left"></i></a>
|
||
|
||
|
||
<a href="../chapter3/09_Summary_of_the_Grammar.html" class="navigation navigation-next " aria-label="Next page: 语法总结"><i class="fa fa-angle-right"></i></a>
|
||
|
||
</div>
|
||
</div>
|
||
|
||
|
||
|
||
<script src="http://cdn.bootcss.com/ace/1.1.3/ace.js"></script>
|
||
<script src="http://cdn.bootcss.com/ace/1.1.3/mode-javascript.js"></script>
|
||
<script src="../gitbook/jsrepl/jsrepl.js" id="jsrepl-script"></script>
|
||
<script src="../gitbook/app.js"></script>
|
||
|
||
|
||
|
||
<script src="../gitbook/plugins/gitbook-plugin-mixpanel/plugin.js"></script>
|
||
|
||
|
||
|
||
<script src="http://cdn.mathjax.org/mathjax/2.0-latest/MathJax.js?config=TeX-AMS-MML_HTMLorMML"></script>
|
||
|
||
|
||
|
||
<script src="../gitbook/plugins/gitbook-plugin-mathjax/plugin.js"></script>
|
||
|
||
|
||
<script>
|
||
require(["gitbook"], function(gitbook) {
|
||
var config = {};
|
||
gitbook.start(config);
|
||
});
|
||
</script>
|
||
|
||
|
||
<script src="http://s19.cnzz.com/z_stat.php?id=1000480711&web_id=1000480711" language="JavaScript"></script></body>
|
||
|
||
</html>
|