interface的default方法和static方法

  • 接口中可以定义static方法,可通过接口名称.方法名()调用,实现类不能继承static方法;
  • 接口中可以定义default方法,default修饰的方法有方法体,表示这个方法的默认实现,子类可以直接调用,可以选择重写或者不重写;
  • 当实现类实现的多个接口中,有方法签名相同的default方法时,必须重写该方法;

  接口一:

package com.skd.interfacemethod;

/**
* @Description
* @Author virgosnail
* @Date 2018/12/12
*/ public interface Human { /**
* 必须被重写
*/
void oldMethod(); /**
* 实现类可以选择重写,也可以不重写
*/
default void hi(){
System.out.println("interface default method");
} /**
* 不能被实现类重写
*/
static void hello(){
System.out.println("interface static method");
}
}

  接口二:

package com.skd.interfacemethod;

/**
* @Description
* @Author virgosnail
* @Date 2018/12/12 20:59
*/ public interface Humen { default void hi(String aa){
System.out.println("interface default method");
}
}

  实现类:

package com.skd.interfacemethod;

/**
* @Description
* @Author virgosnail
* @Date 2018/12/12
*/ public class Person implements Human,Humen { @Override
public void oldMethod() { } /**
* 实现的多个接口中有方法签名相同的default 方法时,实现类必须重写该方法
*/
@Override
public void hi() { }
/**
* 实现类不能继承接口的static 方法
*/
}

  测试类:

package com.skd.interfacemethod;

/**
* @Description
* @Author virgosnail
* @Date 2018/12/12
*/ public class Test {
public static void main(String[] args) {
// interface 的 default 方法通过实现类的实例对象调用
new Person().hi();
// interface 的 static 方法通过 接口名称.方法名称直接调用
Human.hello(); }
}

interface中定义default方法和static方法的更多相关文章

  1. JS中的call()方法和apply()方法用法总结

    原文引自:https://blog.csdn.net/ganyingxie123456/article/details/70855586 最近又遇到了JacvaScript中的call()方法和app ...

  2. js中的splice方法和slice方法简单总结

    slice:是截取用的 splice:是做删除 插入 替换用的 slice(start,end): 参数: start:开始位置的索引 end:结束位置的索引(但不包含该索引位置的元素) 例如: va ...

  3. JS中的call()方法和apply()方法用法总结(挺好 转载下)

    最近又遇到了JacvaScript中的call()方法和apply()方法,而在某些时候这两个方法还确实是十分重要的,那么就让我总结这两个方法的使用和区别吧. 1. 每个函数都包含两个非继承而来的方法 ...

  4. TP框架中的A方法和R方法

    ThinkPHP 跨模块调用操作方法(A方法与R方法) 跨模块调用操作方法 前面说了可以使用 $this 来调用当前模块内的方法,但实际情况中还经常会在当前模块调用其他模块的方法.ThinkPHP 内 ...

  5. Mapper类/Reducer类中的setup方法和cleanup方法以及run方法的介绍

    在hadoop的源码中,基类Mapper类和Reducer类中都是只包含四个方法:setup方法,cleanup方法,run方法,map方法.如下所示: 其方法的调用方式是在run方法中,如下所示: ...

  6. java 中的set方法和get方法的理解

    get的意思是获取,set的意思是设置. get方法和set方法是实现类的封装访问的很好的工具. 当类中的变量设为private 时,他的意思就是说,只能通过自身和子类的访问,但是对于别的其他的类来说 ...

  7. Android中的Sqlite中的onCreate方法和onUpgrade方法的执行时机

    1.今天在做数据库升级的时候,遇到一个问题,就是onCreate方法和onUpgrade方法的执行时机的问题,这个当时在操作的时候,没有弄清楚,很是迷糊,后来看了相关的博客由于转发受限所以copy了一 ...

  8. Hibernate中Session.get()方法和load()方法的详细比较

    一.get方法和load方法的简易理解  (1)get()方法直接返回实体类,如果查不到数据则返回null.load()会返回一个实体代理对象(当前这个对象可以自动转化为实体对象),但当代理对象被调用 ...

  9. 3-自定义构造方法和description方法

    http://www.cnblogs.com/mjios/archive/2013/04/19/3031412.html -自定义构造方法和description方法 1 默认的构造方法是什么?有什么 ...

随机推荐

  1. 最新WordConut

    一.代码地址:https://gitee.com/cainiaoY/WordCount 二.项目分析:代码根据实现的功能不同分为两个模块,一个wcFuctiong类,一个wcTest类,其中wcFuc ...

  2. zabbix3.4 监控ESXI6.7

    一.ESXI WEB界面 管理--高级配置启用 键 Config.HostAgent.plugins.solo.enableMob 访问:https://10.81.1.219/mob/?moid=h ...

  3. 567. Permutation in String判断某字符串中是否存在另一个字符串的Permutation

    [抄题]: Given two strings s1 and s2, write a function to return true if s2 contains the permutation of ...

  4. GitHub下载子目录

    背景 整个Github目录太大,国内网速不好,且其他部分也不需要. 方法 把 /tree/master 改成 trunk. svn checkout https://github.com/lodash ...

  5. laravel简书(2)

    用户注册 public function register() {         //验证         $this->validate(\request(),[             ' ...

  6. 拉普拉斯平滑处理 Laplace Smoothing

    背景:为什么要做平滑处理? 零概率问题,就是在计算实例的概率时,如果某个量x,在观察样本库(训练集)中没有出现过,会导致整个实例的概率结果是0.在文本分类的问题中,当一个词语没有在训练样本中出现,该词 ...

  7. adb pull 文件夹到电脑

    正常来讲是支持文件夹的. 但实际执行的时候发现: pull: building file list...0 files pulled. 0 files skipped. 其实出现这个提示,归根到底还是 ...

  8. Eclipse设置智能提示

    1.解决智能感知提示响应时间,使Eclipse追上VS的响应步伐:Window→Preferences→Java→Editor→Content Assist 这里的Auto activation de ...

  9. Python开发——基础

    注释 单行注释 # 被注释的内容 多行注释 """ 被注释的内容 """ 解释器路径 #!/usr/bin/env python # 用于L ...

  10. grafana add custom dashboard

    grafana-dashboard-json prometheus-operator helm 中的grafana dashboard 扩展的时候,需要转换下载(https://grafana.com ...