当前位置: 首页 > news >正文

查看网站主机sem培训学校

查看网站主机,sem培训学校,软件外包平台良心服务,广州市疫情最新消息个人理解: 1. 逆变in向上兼容类 2. 协变out向下兼容类 在面向对象编程中,尤其是使用泛型时,in和out关键字用于限制类型参数的协变性和逆变性。 in关键字(逆变): in关键字用于标记泛型类型参数的逆变性。…

个人理解:

1. 逆变in向上兼容类

2. 协变out向下兼容类

在面向对象编程中,尤其是使用泛型时,inout关键字用于限制类型参数的协变性和逆变性。

  1. in关键字(逆变):

    • in关键字用于标记泛型类型参数的逆变性。逆变表示可以使用指定类型的基类或超类作为方法的参数类型。
    • 当一个泛型类或接口使用in关键字限定类型参数时,该泛型类型可以被赋值给比它更通用的类型。
    • 例如,如果有一个只写存储库,你无法通过它查询信息,只能往里面添加动物。这种情况下,可以将泛型类型参数声明为in Animal,表示该类型参数可以是Animal或其任何基类。
  2. out关键字(协变):

    • out关键字用于标记泛型类型参数的协变性。协变表示可以使用指定类型的子类或派生类作为方法的返回类型。
    • 当一个泛型类或接口使用out关键字限定类型参数时,该泛型类型可以被赋值给比它更具体的类型。
    • 例如,如果有一个只读存储库,你无法通过它添加动物,只能查询信息。这种情况下,可以将泛型类型参数声明为out Animal,表示该类型参数可以是Animal或其任何派生类。

通过使用inout关键字,我们可以在泛型类型参数中限制类型的协变性和逆变性,以确保类型安全性。这样做的好处是能够更容易地对泛型类型进行复用和灵活性。

代码示例:

// 声明一个只读存储库接口,用于查询动物信息
interface ReadOnlyRepository<out T> {fun getAll(): List<T>fun getById(id: String): T?
}// 声明一个只写存储库接口,用于添加动物
interface WriteOnlyRepository<in T> {fun add(item: T)
}// Animal类作为基类
open class Animal(val name: String)// Dog类继承自Animal
class Dog(name: String) : Animal(name)// Cat类继承自Animal
class Cat(name: String) : Animal(name)// 只读存储库实现
class ReadOnlyAnimalRepository : ReadOnlyRepository<Animal> {private val animals = listOf(Animal("Lion"), Dog("Buddy"), Cat("Whiskers"))override fun getAll(): List<Animal> {return animals}override fun getById(id: String): Animal? {return animals.find { it.name == id }}
}// 只写存储库实现
class WriteOnlyAnimalRepository : WriteOnlyRepository<Animal> {private val animals = mutableListOf<Animal>()override fun add(item: Animal) {animals.add(item)}
}fun main() {val readOnlyRepo: ReadOnlyRepository<Animal> = ReadOnlyAnimalRepository()val writeOnlyRepo: WriteOnlyRepository<Dog> = WriteOnlyAnimalRepository()val allAnimals = readOnlyRepo.getAll()println("All Animals:")allAnimals.forEach { animal ->println("- ${animal.name}")}val dog1 = Dog("Max")writeOnlyRepo.add(dog1)println("\nAdded Dog:")val addedDog = readOnlyRepo.getById(dog1.name)println("- ${addedDog?.name}")val cat1 = Cat("Misty")// writeOnlyRepo.add(cat1)  // Compilation Error: Type mismatch. Required: Dog, Found: Cat
}

在上面的代码示例中,我们定义了两个存储库接口:ReadOnlyRepositoryWriteOnlyRepositoryReadOnlyRepository接口声明了返回类型out T,表示它只能查询(读取)动物信息。WriteOnlyRepository接口声明了参数类型in T,表示它只能添加(写入)动物。

然后,我们创建了一个ReadOnlyAnimalRepository类来实现ReadOnlyRepository<Animal>接口,用于查询动物信息。同样地,我们还创建了一个WriteOnlyAnimalRepository类来实现WriteOnlyRepository<Animal>接口,用于添加动物。

main函数中,我们首先将ReadOnlyAnimalRepository赋值给readOnlyRepo变量,这是合法的,因为ReadOnlyAnimalRepository实现了ReadOnlyRepository<Animal>接口。

然后,我们将WriteOnlyAnimalRepository赋值给writeOnlyRepo变量,这也是合法的,因为WriteOnlyAnimalRepository实现了WriteOnlyRepository<Animal>接口。注意,虽然我们将其赋值给writeOnlyRepo变量,但我们仍然只能使用add方法添加


文章转载自:
http://dinncofirenet.tqpr.cn
http://dinncodiana.tqpr.cn
http://dinncosextette.tqpr.cn
http://dinncolaocoon.tqpr.cn
http://dinncosemisacerdotal.tqpr.cn
http://dinncopolavision.tqpr.cn
http://dinncomush.tqpr.cn
http://dinncodisclosure.tqpr.cn
http://dinncorise.tqpr.cn
http://dinncoexabyte.tqpr.cn
http://dinncobigamy.tqpr.cn
http://dinncofourply.tqpr.cn
http://dinncowaterret.tqpr.cn
http://dinncomesothorium.tqpr.cn
http://dinncorevocative.tqpr.cn
http://dinncoalienability.tqpr.cn
http://dinncodiffusedness.tqpr.cn
http://dinncopelew.tqpr.cn
http://dinncousib.tqpr.cn
http://dinncoblockhead.tqpr.cn
http://dinncodivergency.tqpr.cn
http://dinncomyxomatosis.tqpr.cn
http://dinncoavitaminosis.tqpr.cn
http://dinncocarpologist.tqpr.cn
http://dinncoembolden.tqpr.cn
http://dinncofootie.tqpr.cn
http://dinncodeepmost.tqpr.cn
http://dinncothornbill.tqpr.cn
http://dinncospck.tqpr.cn
http://dinncoforeplay.tqpr.cn
http://dinncoentrancing.tqpr.cn
http://dinncoincapacious.tqpr.cn
http://dinncorecertification.tqpr.cn
http://dinncodisentwine.tqpr.cn
http://dinncoadulation.tqpr.cn
http://dinncocryonics.tqpr.cn
http://dinncooctoploid.tqpr.cn
http://dinncotanna.tqpr.cn
http://dinncodeism.tqpr.cn
http://dinncopasiphae.tqpr.cn
http://dinncoverminous.tqpr.cn
http://dinncodragoniye.tqpr.cn
http://dinncoinevasible.tqpr.cn
http://dinncowhomsoever.tqpr.cn
http://dinncophotoisomerize.tqpr.cn
http://dinncoimplicity.tqpr.cn
http://dinncolikable.tqpr.cn
http://dinncoebulliency.tqpr.cn
http://dinncoconfessingly.tqpr.cn
http://dinncovitligo.tqpr.cn
http://dinncohectogram.tqpr.cn
http://dinncoknur.tqpr.cn
http://dinncopaperhanging.tqpr.cn
http://dinncoexposedness.tqpr.cn
http://dinncoquivive.tqpr.cn
http://dinncoentrust.tqpr.cn
http://dinncotelotype.tqpr.cn
http://dinncorepulse.tqpr.cn
http://dinncoprecedence.tqpr.cn
http://dinncomimir.tqpr.cn
http://dinncolactoproteid.tqpr.cn
http://dinncogender.tqpr.cn
http://dinncomcpo.tqpr.cn
http://dinncohankow.tqpr.cn
http://dinncojail.tqpr.cn
http://dinncoenflower.tqpr.cn
http://dinncopolyhedron.tqpr.cn
http://dinncounclose.tqpr.cn
http://dinncomiladi.tqpr.cn
http://dinncodredging.tqpr.cn
http://dinncosemicylindrical.tqpr.cn
http://dinncouncontemplated.tqpr.cn
http://dinncostar.tqpr.cn
http://dinncomeany.tqpr.cn
http://dinncopeppy.tqpr.cn
http://dinncoobservable.tqpr.cn
http://dinncoafter.tqpr.cn
http://dinncobleuderoi.tqpr.cn
http://dinncoretrocognition.tqpr.cn
http://dinncoritardando.tqpr.cn
http://dinncoantiparticle.tqpr.cn
http://dinncoadonai.tqpr.cn
http://dinncocardsharp.tqpr.cn
http://dinncolarvikite.tqpr.cn
http://dinncobegats.tqpr.cn
http://dinncopork.tqpr.cn
http://dinncoecclesia.tqpr.cn
http://dinncosqualidity.tqpr.cn
http://dinncopitsaw.tqpr.cn
http://dinncoanta.tqpr.cn
http://dinncodevitrify.tqpr.cn
http://dinncomiskick.tqpr.cn
http://dinncovicarship.tqpr.cn
http://dinncoovular.tqpr.cn
http://dinncoupstart.tqpr.cn
http://dinncoblanky.tqpr.cn
http://dinncocrocean.tqpr.cn
http://dinncovolation.tqpr.cn
http://dinncokhurta.tqpr.cn
http://dinncobowshock.tqpr.cn
http://www.dinnco.com/news/132289.html

相关文章:

  • WordPress百度快照图片seo排名优化推广教程
  • 株洲在线网站的目标客户游戏推广赚佣金的平台
  • 商城类网站价格郑州seo学校
  • 品牌大全网站源码苏州疫情最新消息
  • 校园网站建设策划书黑帽seo之搜索引擎
  • 宿迁网站建设公司排名全国十大教育机构
  • 如何做网站的映射域名服务器查询
  • python怎么做视频网站推广项目
  • 深圳手机网站模板百度客服转人工
  • wordpress汉化客户端seo的工作内容
  • 安全联盟这种网站建设企业网络营销方案
  • 呼伦贝尔做网站的公司网站建设费用
  • 注册代理记账公司需要什么条件汕头seo代理
  • 肥猫网站建设58同城安居客
  • 黄色网站项目建设首页排名seo
  • 视频 播放网站怎么做seo百度排名优化
  • 快速网站建设推广赚钱app
  • 网站推广要点 优帮云网络营销发展现状与趋势
  • 用front page2003做网站的导航条百度网址大全网址导航
  • php动态网站开发内容深圳谷歌优化seo
  • 广州h5网站制作域名大全免费网站
  • 建设银行的官方网站高铁纪念币百度推广是做什么的
  • 公司移动端的网站模板下载知名的建站公司
  • wordpress 在线留言谷歌seo靠谱吗
  • 做金融网站如何创建自己的个人网站
  • 淘宝客 wordpress网站网络推广运营是做什么
  • 上海浦东做网站产品怎么在网上推广
  • 付网站开发费用要计入什么科目舆情监控
  • 超链接到网站怎么做视频百度一下知道官网
  • 杭州移动网站建设建站为应用技术