首页 > 解决方案 > 这个界面有什么问题?

问题描述

接口可以包含正文吗?帮我解决这部分

public interface Figures
{
  public void printMessage (String s)
  {
    System.out.println ("This figure is " + s);
  }
  public double area ();    // to calculate the area of the figure
}

标签: javainterfaceabstract

解决方案


default从 Java 8 开始,我们可以使用关键字为接口方法提供默认植入

public interface Figures
{
  public default void printMessage (String s)
  {
    System.out.println ("This figure is " + s);
  }
  public double area ();    // to calculate the area of the figure
}

推荐阅读