Android-Kotlin-接口与多态的表现
上一篇博客介绍了 Android-Kotlin-抽象类与多态的表现 ;, 而这一篇博客专门介绍下 接口与多态的表现
选择包名,然后右键:
选择Class类型,会有class:
选择File类型,不会自动有class:
选择interface,是创建接口:
目录结构:
定义手机充电接口标准规范 InterfacePhone:
- package cn.kotlin.kotlin_oop06
- /**
- * 定义描述手机的Interface
- */
- interface InterfacePhone {
- /**
- * 定义此功能的标准,只要是符合typeC接口的充电器就能充电,否则就无法充电,接口标准必须是Type-C
- * (接口关注的是功能)
- */
- fun typeC()
- }
描述 红色的TypeC手机充电器,(符合手机充电接口标准规范 InterfacePhone)
- package cn.kotlin.kotlin_oop06
- /**
- * 描述 红色的TypeC手机充电器
- */
- class RedChargerPhoneTypeC : InterfacePhone{
- /**
- * 实现了InterfacePhone接口,就必须要用override重写父类接口的标准,因为这是父类规定的规则,必须是Type-C接口才能充电
- */
- override fun typeC() {
- println("红色充电器,正常的给手机充电了--> 充电中....")
- }
- }
描述 蓝色的TypeC手机充电器,(符合手机充电接口标准规范 InterfacePhone)
- package cn.kotlin.kotlin_oop06
- /**
- * 描述 蓝色的TypeC手机充电器
- */
- class BlueChargerPhoneTypeC : InterfacePhone{
- /**
- * 实现了InterfacePhone接口,就必须要用override重写父类接口的标准,因为这是父类规定的规则,必须是Type-C接口才能充电
- */
- override fun typeC() {
- println("蓝色充电器,正常的给手机充电了--> 充电中....")
- }
- }
描述 黑色的TypeC手机充电器,(符合手机充电接口标准规范 InterfacePhone)
- package cn.kotlin.kotlin_oop06
- /**
- * 描述 白色的TypeC手机充电器
- */
- class BlackChargerPhoneTypeC : InterfacePhone{
- /**
- * 实现了InterfacePhone接口,就必须要用override重写父类接口的标准,因为这是父类规定的规则,必须是Type-C接口才能充电
- */
- override fun typeC() {
- println("白色充电器,正常的给手机充电了--> 充电中....")
- }
- }
main测试方法,多态的表现:
- package cn.kotlin.kotlin_oop06
- fun main(args: Array<String>) {
- // 父类 personChildClass = new 子类()
- var interfacePhone:InterfacePhone = RedChargerPhoneTypeC()
- interfacePhone.typeC()
- // 父类 personChildClass = new 子类()
- interfacePhone = BlackChargerPhoneTypeC()
- interfacePhone.typeC()
- // 父类 personChildClass = new 子类()
- interfacePhone = BlueChargerPhoneTypeC()
- interfacePhone.typeC()
- // 父类 personChildClass = new N个子类()
- // .......
- }
执行结果:
mian测试方法,多态的表现:
- package cn.kotlin.kotlin_oop06
- fun main(args: Array<String>) {
- // listOf<InterfacePhone> 传递的是InterfacePhone父类接口,所以可以增加所有 InterfacePhone接口>子类
- var list = listOf<InterfacePhone>(RedChargerPhoneTypeC(), BlackChargerPhoneTypeC(), BlueChargerPhoneTypeC())
- for (l in list)
- l.typeC()
- }
执行结果:
Android-Kotlin-接口与多态的表现的更多相关文章
- 谁说接口不能有代码?—— Kotlin接口简介(KAD 26)
作者:Antonio Leiva 时间:Jun 6, 2017 原文链接:https://antonioleiva.com/interfaces-kotlin/ 与Java相比,Kotlin接口允许你 ...
- C#中的接口实现多态
我们都知道虚方法实现多态,抽象方法实现多态等,我们今天来看看如何使用接口实现多态 1.首先我们先要来了解了解什么是接口,它存在的意识 01.接口就是为了约束方法的格式(参数和返回值类型)而存在的 02 ...
- C#面向对象(三)接口实现多态
一.如何用接口实现多态? 1.定义一个接口. using System; using System.Collections.Generic; using System.Linq; using Syst ...
- (转)Android之接口回调机制
开发中,接口回调是我们经常用到的. 接口回调的意思即,注册之后并不立马执行,而在某个时机触发执行. 举个例子: A有一个问题不会,他去问B,B暂时解决不出来,B说,等我(B)解决了再告诉你(A)此时A ...
- No2_4.接口继承多态_Java学习笔记_经典案例
import java.lang.reflect.Field; import java.util.ArrayList; import java.util.Collections; import jav ...
- ShiWangMeSDK Android版接口文档 0.2.0 版
# ShiWangMeSDK Android版接口文档 0.2.0 版 android 总共有 14 个接口,分别涉及到初始化和对界面的一些细节的控制.下面详细介绍接口,如果没有特殊说明,接口都在 S ...
- Java基础学习笔记八 Java基础语法之接口和多态
接口 接口概念 接口是功能的集合,同样可看做是一种数据类型,是比抽象类更为抽象的”类”.接口只描述所应该具备的方法,并没有具体实现,具体的实现由接口的实现类(相当于接口的子类)来完成.这样将功能的定义 ...
- java接口,接口的特性,接口实现多态,面向接口编程
package cn.zy.cellphone; /**接口是一种引用数据类型.使用interface声明接口,形式 * 形式:public interface 接口名称{} * 接口不能拥有构造方法 ...
- Kotlin 接口
Kotlin 接口与 Java 8 类似,使用 interface 关键字定义接口,允许方法有默认实现: interface MyInterface { fun bar() // 未实现 fun fo ...
随机推荐
- Python generator 的yield (enumerate)
生成杨辉三角 1 1 1 1 2 1 1 3 3 1 1 4 6 4 1 1 5 10 10 5 1 def triangles(max): L = [1,] while len(L) - 1 < ...
- linux学习第三天 (Linux就该这么学)
今天是学习的第三天,讲了很多命令,又赶上双11,网络经常波动,我经常掉线,没有听到多少,回头再看一下录播.我也写一下讲的命令吧,也加深一下命令的印象.第三章老师讲完了. ifconfig命令:输出信息 ...
- [Jmeter] Jmeter Plugins
Plugins: Plugins Manager: https://jmeter-plugins.org/wiki/PluginsManager/ Custom Thread Groups: http ...
- 【jdbcTemplate】baseDao书写规范
今天加班,为了下个月的北京之行,希望父亲身体安康,一切顺利: 老大今天发出来同事的代码,并标记了jdbcTemplate的书写规范,此处查询数据库之前声明对象时,不用new出来,因为在底层源码中已经给 ...
- Linq去重 不用实现IEqualityComparer接口的方法超级简单
RskFactorRelation.Instance.GetCache<RskFactorRelation>(true).Where(x => !string.IsNullOrEmp ...
- Permutation Sequence LT60
The set [1,2,3,...,n] contains a total of n! unique permutations. By listing and labeling all of the ...
- python网络socket编程
一.服务端 #!/usr/bin/python # -*- coding: UTF-8 -*- import socket import sys from thread import * HOST = ...
- C# 遇到 which has a higher version than referenced assembly
当C#遇到这种提示: which has a higher version than referenced assembly, 说明有两个或多个工程引用的dll的版本有出现不一样, 如: A工程引用l ...
- mysql主从配置思路
记录一下 原文:http://www.rjfw.com.cn/qamain/prevView.action?id=40482017200000031 mysql主从配置(清晰的思路) mysql主从配 ...
- Python开课复习-10/16
import random # random 随机数模块 # print(random.random()) #----float 大于0且小于1之间的小数# print(random.choice([ ...