首页 文章 精选 留言 我的

精选列表

搜索[接口管理],共10000篇文章
优秀的个人博客,低调大师

Kotlin 中的接口 Interface : so much better

Interface was introduced in Java as a new programming feature. It describes CAN-BE instead of IS-A relationship. That also enables it to perform multiple inheritance (e.g. something can be many things, but only is a thing). However as we know even up to Java 7 (which once was the main language for native Android Development), Interface does have various drawbacks, making it not as attractive, and at times, some have to resort back to abstract class. With Kotlin in place, let me share with you how Kotlin made Inheritance better. Kotlin made Interface extensible. In Java 7, inheritance function declaration can’t have implementation. Hence those class implements an interface, need to have all it’s function implemented. This is a problem, as this makes interface inextensible. Imagine we have the below Movable interface. interface Movable { int legsCount(); } class Horse implements Movable { @Override public int legsCount() { return 4; } } Then we realize that other than legs, we need to count wings too. So we add wingsCount(). It is unfortunate those that implemented this interface i.e. Horse will also need to change. interface Movable { int legsCount(); int wingsCount(); } class Horse implements Movable { @Override public int legsCount() { return 4; } @Override public int wingsCount() { return 0; } } In Kotlin We initially have interface Movable { fun legsCount(): Int } class Horse : Movable { override fun legsCount() = 4 } Then we could easily extend it. interface Movable { fun legsCount(): Int fun wingsCount(): Int { return 0 } } class Horse : Movable { override fun legsCount() = 4 } Or even more, without need to modify Horse class at all! interface Movable { fun legsCount(): Int { return 0 } fun wingsCount(): Int { return 0 } fun canFly(): Boolean { return wingsCount() > 1 } fun canWalk(): Boolean { return legsCount() > 1 } } class Horse : Movable { override fun legsCount() = 4 } Kotlin made Interface truly override. The definition of override according to Cambridge Dictionary is to decide against or refuse to accept a previous decision, an order, a person, etc. In the Java world, Interface is overriding nothing. But in Kotlin world, look at the example below interface Movable { fun legsCount(): Int { return 0 } fun wingsCount(): Int { return 0 } fun canFly(): Boolean { return wingsCount() > 1 } fun canWalk(): Boolean { return legsCount() > 1 } } class Horse : Movable { var isSick = false override fun legsCount() = 4 override fun canWalk(): Boolean { if (isSick) { return false } return super.canWalk() } } If we set horse.isSick = true, the canWalk() function will return false, regardless of the leg counts. A truly overriding capability. Kotlin made Interface more object like In Java world (I believe including Java 8 and 9), Interface are not allowed to have property other than final constant variable (hmm… constant variable sounds oxymoron, perhaps should be called constant value). At most we could make an accessor function e.g. legCount(). In Kotlin With Kotlin, one could have a property in Interface. Instead of writing interface Movable { fun legsCount(): Int fun canWalk() = legsCount() > 1 } class Horse : Movable { override fun legsCount() = 4 } One could write as interface Movable { val legsCount : Int fun canWalk(): Boolean = legsCount > 1 } class Horse : Movable { override val legsCount = 4 } There’s some limitation for the property in Interface though, as it can’t have backfield property, which means it can’t be change. So it is still stateless. Besides, it also can’t be initialized in the interface itself. Kotlin made Interface a better composition You might have heard Composition over Inheritance principle. Kotlin made this even more simpler Imagine you have Horse and Dog. Both are 4 legs animal. One way to program is as below interface Movable { val legsCount : Int fun canWalk() = legsCount > 1 } class Horse : Movable { override val legsCount = 4 } class Dog : Movable { override val legsCount = 4 } This is so cumbersome as we have to replicate the code override val legsCount = 4 for each of them. If we have more functions to override, or more class object that is 4 legs animal, we’ll have to do the same. If one day we change to 4 to “four”, or add more functionality… It would be a nightmare to change . So inextensible. We can make an class inheritance of that perhaps? interface Movable { val legsCount: Int fun canWalk() = legsCount > 1 } open class FourLegged : Movable { override val legsCount = 4 } class Horse : FourLegged() class Dog : FourLegged() But this violates the Composition over Inheritance principle. Horse and Dogare not only FourLegged, but could be something else, making them very inextensible to other type anymore (e.g. Pet). This is also inextensible ️ So let’s apply Composite over Inheritance (the traditional way) interface Movable { val legsCount: Int fun canWalk() = legsCount > 1 } object FourLegged : Movable { override val legsCount = 4 } class Horse : Movable { private val movable = FourLegged override val legsCount get() = movable.legsCount } class Dog : Movable { private val movable = FourLegged override val legsCount get() = movable.legsCount } I don’t know about you, I dislike this equally, So let’s enhance it better to as below… interface Movable { val legsCount: Int fun canWalk() = legsCount > 1 } object FourLegged : Movable { override val legsCount = 4 } open class MovableImpl(private val movable: Movable) : Movable { override val legsCount get() = movable.legsCount } class Horse : MovableImpl(FourLegged) class Dog : MovableImpl(FourLegged) Now this is better, as it is more extensible, as in the future we have FourLegged or TwoLegged etc, we could easily add to it. But I still dislike it, as I need to have the intermediate class MovableImpl. So let’s check out further what how Kotlin could made our interface better… The Kotlin provided way: By … delegate to composition made easy With the interface in Kotlin, we could use the By keyword to generate the Delegate pattern so easily. Check it out interface Movable { val legsCount: Int fun canWalk() = legsCount > 1 } object FourLegged : Movable { override val legsCount = 4 } class Horse : Movable by FourLegged class Dog : Movable by FourLegged So much nicer! . Hopes you see how good that is. Kotlin 开发者社区 国内第一Kotlin 开发者社区公众号,主要分享、交流 Kotlin 编程语言、Spring Boot、Android、React.js/Node.js、函数式编程、编程思想等相关主题。 开发者社区 QRCode.jpg

优秀的个人博客,低调大师

Java 常用类库 之 比较接口 Comparator

http://www.verejava.com/?id=169931036202101 /** 知识点: 比较类 Comparator 题目: 将某班学生按数学成绩从小到大排序 思路: 1. 抽象出类: 1.1 班级(ClassSet) 1.2 学生(Student) 2. 找出类关系: 2.1 学生 属于 班级 Student -> ClassSet(多对1) 3. 找出类属性: 3.1 ClassSet(班级名称,班级人数) 3.2 Student(学生名称,数学成绩) 4. 找出类方法: 4.1 学生添加到班级 ClassSet{addStudent(Student s)} 4.2 学生成绩从小到大排序 ClassSet{sortByScore()} */ import java.util.Arrays; import java.util.Comparator; public class TestComparator { public static void main(String[] args) { //实例化4G班级 ClassSet c = new ClassSet("4G", 4); //添加学生 c.addStudent(new Student("李明", 90)); c.addStudent(new Student("李浩", 80)); c.addStudent(new Student("王涛", 95)); c.addStudent(new Student("张胜", 70)); //获得4G班级学生数组集合 Student[] students = c.getStudents(); //输出学生信息 for (Student s : students) { if (s != null) System.out.println(s.getName() + "," + s.getMathScore()); } System.out.println("\n根据学生成绩升序排序"); Arrays.sort(students, new StudentAscComparator()); for (Student s : students) { if (s != null) System.out.println(s.getName() + "," + s.getMathScore()); } System.out.println("\n根据学生成绩降序排序"); Arrays.sort(students, new StudentDescComparator()); for (Student s : students) { if (s != null) System.out.println(s.getName() + "," + s.getMathScore()); } } } class ClassSet { private String className;//班级名称 private int maxSize;//班级学生人数 private int currentSize;//当前多少学生 private Student[] students;//所有学生的数组 public ClassSet(String className, int maxSize) { this.className = className; this.maxSize = maxSize; students = new Student[maxSize]; } public Student[] getStudents() { return this.students; } /** 添加学生 */ public void addStudent(Student s) { for (int i = 0; i < students.length; i++) { if (students[i] == null) { students[i] = s; currentSize++; break; } } } } class Student { private String name;//学生姓名 private int mathScore;//数学成绩 public Student(String name, int mathScore) { this.name = name; this.mathScore = mathScore; } public String getName() { return this.name; } public void setName(String name) { this.name = name; } public int getMathScore() { return this.mathScore; } public void setMathScore(int mathScore) { this.mathScore = mathScore; } } /** 学生升序排列 */ class StudentAscComparator implements Comparator { public int compare(Object o1, Object o2) { if ((o1 instanceof Student) && (o2 instanceof Student)) { Student s1 = (Student) o1; Student s2 = (Student) o2; if (s1.getMathScore() > s2.getMathScore()) return 1; if (s1.getMathScore() < s2.getMathScore()) return -1; } return 0; } } /** 学生降序排列 */ class StudentDescComparator implements Comparator { public int compare(Object o1, Object o2) { if ((o1 instanceof Student) && (o2 instanceof Student)) { Student s1 = (Student) o1; Student s2 = (Student) o2; if (s1.getMathScore() > s2.getMathScore()) return -1; if (s1.getMathScore() < s2.getMathScore()) return 1; } return 0; } } http://www.verejava.com/?id=169931036202101

优秀的个人博客,低调大师

Go中的接口多态及嵌入类

我感觉个还好理解, 至少比当初入JAVA时更容易理解, 可能是以前的学习为现在打下了基础吧。 又或许是最近三年的程序编写,长了见识吧~ package main import ( "fmt" ) type notifier interface { notify() } type user struct { name string email string } type admin struct { user level string } func (u user) notify() { fmt.Printf("Sending User Email To %s<%s>\n", u.name, u.email) } func (u admin) notify() { fmt.Printf("Sending Admin Email To %s<%s>\n", u.name, u.email) } func (u *user) changeEmail(email string) { u.email = email } func main() { bill := user{"Bill", "bill@email.com"} sendNotification(bill) lisa := admin{ user: user{ name: "Lisa", email: "lisa@email.com", }, level: "super", } sendNotification(lisa) } func sendNotification(n notifier) { n.notify() }

优秀的个人博客,低调大师

接口测试之Fiddler+APP抓包

Fiddler简介: Fiddler是强大且好用的Web调试工具之一,它能记录客户端和服务器的http和https请求,允许你监视,设置断点,甚至修改输入输出数 据。 Fiddler 的运行机制其实就是本机上监听8888端口的HTTP代理。 对于PC端Fiddler启动的时候默认IE的代理设为了127.0.0.1:8888,而其他浏览器是需要手动设置的,所以如果需要监听PC端 Chrome网络请求,将其代理改为127.0.0.1:8888就可以监听数据了,手机端按照下面的设置即可完成整个系统的http代理。 一、下载神器Fiddler,下载链接:fiddler2.com/get-fiddler下载完成之后,傻瓜式的安装一下OK了! Fiddler桌面快捷方式 二、设置Fiddler 打开Fiddler, Tools-> Fiddler Options (配置完后记得要重启Fiddler) 选中"Decrpt HTTPS traffic", Fiddler就可以截获HTTPS请求 配置HTTPS请求 选中"Allow remote computers to connect". 是允许别的机器把HTTP/HTTPS请求发送到Fiddler上来。记住这个端口号是:8888 配置HTTP/HTTPS请求权限 三、设置Android手机 首先获取PC的ip地址:命令行中输入:ipconfig,获取ip地址。这时候我就拿到了电脑IP地址和端口号了 PC端IP地址 下面来对Android手机进行代理设置,本人使用的是小米5S 首先,要确定一下手机和PC是连接在同一个局域网中 进入手机的设置->点击进入WLAN设置->选择连接到的无线网,点右边的箭头(有的手机是长按弹出选项框):如图所示: 设置代理 代理这一行,选择:手动 选择修改网络配置: 1)配置主机名:与主机电脑IP地址保持一致 2)端口号:8888 设置代理 点击保存。这样就将我们的手机设置成功了。 四、打开要测试的APP,然后打开电脑上的fiddler界面,本人在手机端打开的是“简书APP”。如下图 抓简书APP 这样就抓取Android移动端的数据包成功了,这个对于我们后面进行网络数据请求的调试有很大的帮助,我们可以通过这个方法来判断我们请求网络是否成功! 注:一般http请求,不用安装安全证书;如果是https的请求,设置代理后,会无法访问,这时候就需要安装安全证书了 五、下载Fiddler的安全证书 使用手机的浏览器打开:http://192.168.2.204:8080, 点"FiddlerRoot certificate" 然后安装证书,如图: 下载安全证书 六、截取HTTPS请求 1.下载安装证书生成器 证书地址:http://www.telerik.com/docs/default-source/fiddler/addons/fiddlercertmaker.exe?sfvrsn=2 2.生成证书保存至电脑桌面(Tools->Options->HTTPS->Actions->Export Root Certificate to Desktop) 生成证书 FiddlerRoot.cer 证书 3.将桌面的FiddlerRoot.cer(生成的证书)放到手机中安装(通过USB数据线或者SD卡导入) 安装证书 4.查看抓取的请求类型(结果) 请求结果 好了,小伙伴们,下一步就该学习学习该怎么分析抓到的数据了。。。

资源下载

更多资源
Mario

Mario

马里奥是站在游戏界顶峰的超人气多面角色。马里奥靠吃蘑菇成长,特征是大鼻子、头戴帽子、身穿背带裤,还留着胡子。与他的双胞胎兄弟路易基一起,长年担任任天堂的招牌角色。

腾讯云软件源

腾讯云软件源

为解决软件依赖安装时官方源访问速度慢的问题,腾讯云为一些软件搭建了缓存服务。您可以通过使用腾讯云软件源站来提升依赖包的安装速度。为了方便用户自由搭建服务架构,目前腾讯云软件源站支持公网访问和内网访问。

Nacos

Nacos

Nacos /nɑ:kəʊs/ 是 Dynamic Naming and Configuration Service 的首字母简称,一个易于构建 AI Agent 应用的动态服务发现、配置管理和AI智能体管理平台。Nacos 致力于帮助您发现、配置和管理微服务及AI智能体应用。Nacos 提供了一组简单易用的特性集,帮助您快速实现动态服务发现、服务配置、服务元数据、流量管理。Nacos 帮助您更敏捷和容易地构建、交付和管理微服务平台。

WebStorm

WebStorm

WebStorm 是jetbrains公司旗下一款JavaScript 开发工具。目前已经被广大中国JS开发者誉为“Web前端开发神器”、“最强大的HTML5编辑器”、“最智能的JavaScript IDE”等。与IntelliJ IDEA同源,继承了IntelliJ IDEA强大的JS部分的功能。

用户登录
用户注册