接口Java1.8中的具体方法 [英] concrete methods in interfaces Java1.8

查看:127
本文介绍了接口Java1.8中的具体方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在讨论中,我的一位朋友告诉我concrete methods would be allowed in java 1.8 in interfaces,那时我想到一个问题,即如果允许,那么我们将如何区分这些方法.例如
我有两个接口Animal.javaPet.java,并且都具有相同的具体方法,即eat()

During a discussion one of my friend tell me that concrete methods would be allowed in java 1.8 in interfaces then at that time a question came into my mind i-e If they are allowed then How will we distinguish the methods.For example
I have two Interface Animal.java and Pet.java and both have same concrete method i-e eat()

   public interfaces Animal{

        void eat(){
                System.out.println("Animal Start eating ....");
        }
   }

   public interfaces Pet{

        void eat(){
                System.out.println("Pet Start eating ....");
        }
   }

现在,我的Zoo.java都实现了这两个功能,并且没有覆盖

Now my Zoo.java implement both of these and didn't override

    public class Zoo() implements Pet , Animal{ 
             //Now name method is a part of this class
   }

现在这是我的困惑.如何使用Test对象

Now here is my confusion.How can I call a specific method on inteface animal using Test object

public class Demo{
        public static void main(String[] args){

                 Zoo zoo = new Zoo();
                 zoo.eat();    //What would be the output
        }
 }

有什么建议吗?还是在java1.8中对此有任何解决方案,因为我找不到答案?

Any suggestions? or is there any solution for this in java1.8 as I am unable to find its answer.

推荐答案

除非您在Zoo类中覆盖eat,否则您会收到编译时错误.

You get a compile time error, unless you override eat in your Zoo class.

java: class defaultMethods.Zoo inherits unrelated defaults for eat() from types Pet and Animal

最新,最漂亮的jdk是此处btw .语法应该是

The latest and geatest jdk is here btw. And the syntax should be

default void eat(){
  System.out.println("Animal Start eating ....");
}

这篇关于接口Java1.8中的具体方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆