dev โฉ terms โฉ roles โฉ implementer
ๅฏฆ็พ่ โฑimplementerโฑprovider ็จๅผ้็ผไธญ๏ผๅฏซๅฝๆธใๅๅฅๅฎ็พฉ็ไบบ (ๆ็จๅผ็ขผ)ใ ๐ฏ ้ๅธธๆใๆไธๆฎต็จๅผ็ขผใ
// โญ๏ธ implementer: // ๅจ้่ฃก๏ผ`makeCircle` ๅฐฑๆฏๅฏฆ็พ่ ๏ผไปๆฑบๅฎไบ่ฟๅๅผ็ๅ ท้ซๅๅฅๆฏ `Circle`๏ผ // ้็ถๅฐๅคๅชๆด้ฒ `some Shape`(ไฝฟ็จ่ ๅช็ฅ้่ฟๅๅผๆฏ `some Shape`)ใ func makeCircle(radius: Double) -> some Shape { return Circle(radius: radius) } // โญ๏ธ user: // ๅผๅซ `makeCircle` ็็จๅผ็ขผๅฐฑๆฏไฝฟ็จ่ ๏ผไฝฟ็จ่ ๅช็ฅ้่ฟๅๅๅฅ็ฌฆๅ Shape ๅ่ญฐ๏ผ // ไฝไธ็ฅ้ๅ ท้ซๅๅฅๆฏไป้บผ๏ผๅ ็บ่ฟๅๅผๆฏ some Shape๏ผใ let shape = makeCircle(radius: 5)
// โญ๏ธ implementer: ๅฎ็พฉๅๅฅใๅฝๆธ // ------------------------------ protocol MyShape { func area() -> Double } struct MyCircle: MyShape { var radius: Double func area() -> Double { .pi * radius * radius } } func makeCircle() -> some MyShape { return MyCircle(radius: 10) // โญ๏ธ ๅฏฆ็พ่ ๆฑบๅฎๅ ท้ซๅๅฅ๏ผ`MyCircle` } // โญ๏ธ user: ไฝฟ็จๅๅฅใๅฝๆธ // ------------------------------ let shape = makeCircle() // โญ๏ธ ไฝฟ็จ่ ๅช่ฝ็ฅ้ๅฎๆฏ๏ผ`some MyShape` print(shape.area()) // โญ๏ธ ๅฏไฝฟ็จ `MyShape` ๅ่ญฐ็ๆนๆณ
โ๏ธ someโฑanyโฑgenerics๏ผๅฏฆ็พ่ ่ไฝฟ็จ่ ๅฐๅๅฅๆไธๅ็ๆงๅถๆฌใ
ChatGPT โฉ ๅญธ็ฟ Swift
Last updated 4 months ago
Was this helpful?