首页 文章 精选 留言 我的

精选列表

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

pyppeteer最为核心类Page的接口方法

重要:因为同步公号的文章格式很难保证,所以后面文章选择性在其他平台同步,欢迎移步公众号(Python之战),每日更新原汁原味! 重要:因为同步公号的文章格式很难保证,所以后面文章选择性在其他平台同步,欢迎移步公众号(Python之战),每日更新原汁原味! Page类是pyppeteer的核心,其价值就犹如selenium的driver,具体的页面操作都在Page类上;Page与driver比较最具优势的是和js的交互,可以修改本地js、css,也可以给页面添加js函数,甚至添加自定义函数到浏览器的windows属性中,也有js拦截相关的设置,更有终端模拟设置,这些功能是比driver更为强大的功能,但是也有一些劣势如页面超时方面比driver弱、选择器不简洁等问题。 页面类:Page 基类:pyee.EventEmitter 此类提供了与

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

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

资源下载

更多资源
Mario

Mario

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

Spring

Spring

Spring框架(Spring Framework)是由Rod Johnson于2002年提出的开源Java企业级应用框架,旨在通过使用JavaBean替代传统EJB实现方式降低企业级编程开发的复杂性。该框架基于简单性、可测试性和松耦合性设计理念,提供核心容器、应用上下文、数据访问集成等模块,支持整合Hibernate、Struts等第三方框架,其适用范围不仅限于服务器端开发,绝大多数Java应用均可从中受益。

Sublime Text

Sublime Text

Sublime Text具有漂亮的用户界面和强大的功能,例如代码缩略图,Python的插件,代码段等。还可自定义键绑定,菜单和工具栏。Sublime Text 的主要功能包括:拼写检查,书签,完整的 Python API , Goto 功能,即时项目切换,多选择,多窗口等等。Sublime Text 是一个跨平台的编辑器,同时支持Windows、Linux、Mac OS X等操作系统。

WebStorm

WebStorm

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

用户登录
用户注册