zoukankan      html  css  js  c++  java
  • 验证接口实现类对于接口中所有方法是否都要重写?

     1 package Verify;
     2 
     3 interface Output{
     4     //抽象方法
     5     void out();
     6     void get();
     7 
     8     //默认方法,使用default关键字
     9     default void print(){
    10         System.out.println("I Love You!接口里的默认方法。");
    11     }
    12 
    13     //类方法,使用static关键字
    14     static String staticTest(){
    15         return "I Love You!接口里的类方法。";
    16     }
    17 }
    18 public class InterfaceTest implements Output{
    19     @Override
    20     public void out() {
    21         System.out.println("必须重写接口里的抽象方法1");
    22     }
    23 
    24     @Override
    25     public void get() {
    26         System.out.println("必须重写接口里的抽象方法2");
    27     }
    28 
    29     public static void main(String[] args){
    30         InterfaceTest test = new InterfaceTest();
    31 
    32         test.out();
    33         test.get();//结论:接口的抽象方法在实现类中属于必须被重写的
    34         test.print();//结论:接口的默认方法在实现类中属于可重写也可不重写
    35 //        test.staticTest();//结论:Static method may be invoked on containing interface class only
    36         System.out.println(Output.staticTest());//结论:接口的静态方法也叫类方法,则是使用接口来调用
    37     }
    38 }

    结果截图:

  • 相关阅读:
    第六周 组合数据类型
    第五周 函数和代码复用
    第四周 程序的控制结构
    第三周 Python基本数据类型
    python库
    sublime text3自定义设置及快捷键大全
    Sublime Text3插件安装教程
    Linux 标准目录结构
    快学Scala-10--List 和Set
    快学Scala-09--模式匹配
  • 原文地址:https://www.cnblogs.com/masker99/p/12578309.html
Copyright © 2011-2022 走看看