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 }

    结果截图:

  • 相关阅读:
    cuda(2)---方阵乘法
    cuda(1)-imageBlur
    python(6) 字符串操作
    CUDA 编程之Release模式和Debug模式
    20200909 day4 刷题记录
    20200908 day3 刷题记录
    20200906 day1 模拟(一)
    刷题Day 4-6 树形dp三题
    4.28 刷题Day 3 树形dp一题
    DTQ2019-D1T2 括号树 题解
  • 原文地址:https://www.cnblogs.com/masker99/p/12578309.html
Copyright © 2011-2022 走看看