println("Step 1: How to create a wrapper String class which will extend the String type") class DonutString(s: String) { def isFavoriteDonut: Boolean = s == "Glazed Donut" } println("\nStep 2: How to create an implicit function to convert a String to the wrapper String class") object DonutConverstions { implicit def stringTodonutString(s: String) = new DonutString(s) } println("\nStep 3: How to import the String conversion so that it is in scope") import DonutConverstions._ println("\nStep 4: How to create String values") val glazedDonut = "Glazed Donut" val vanillaDonut = "Vanilla Donut" println("\nStep 5: How to access the custom String function called isFavaoriteDonut") println(s"Is Glazed Donut my favorite Donut = ${glazedDonut.isFavoriteDonut}") println(s"Is Vanilla Donut my favorite Donut = ${vanillaDonut.isFavoriteDonut}")
result
Step 1: How to create a wrapper String class which will extend the String type Step 2: How to create an implicit function to convert a String to the wrapper String class Step 3: How to import the String conversion so that it is in scope Step 4: How to create String values Step 5: How to access the custom String function called isFavaoriteDonut Is Glazed Donut my favorite Donut = true Is Vanilla Donut my favorite Donut = false
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。