首页 文章

Implements vs extends:何时使用?有什么不同?

提问于
浏览
595

请用易于理解的语言或某些文章的链接进行解释 .

15 回答

  • 1

    当您需要子类/接口中的父类/接口的属性时使用 Extends ,并且当您需要类中接口的属性时使用 implements .

    例:

    • 使用类扩展

    class Parent {

    }

    class Child extends Parent {

    }

    • 使用界面扩展

    interface Parent {

    }

    interface Child extends Parent {

    }

    • 实施

    接口A {

    }

    B类实现A {

    }

    扩展和实现的组合

    interface A{
    
    }
    
    class B
    
    {
    
    }
    
    class C implements A,extends B{
    
    }
    
  • 30

    extends 用于扩展课程 .

    implements 用于实现接口

    接口和常规类之间的区别在于,在接口中,您无法实现任何声明的方法 . 只有“实现”接口的类才能实现这些方法 . 接口的C等价物将是一个抽象类(不完全相同但几乎相同) .

    对于类,java也不支持 multiple inheritance . 这可以通过使用多个接口来解决 .

    public interface ExampleInterface {
        public void doAction();
        public String doThis(int number);
     }
    
     public class sub implements ExampleInterface {
         public void doAction() {
           //specify what must happen
         }
    
         public String doThis(int number) {
           //specfiy what must happen
         }
     }
    

    现在扩展一个 class

    public class SuperClass {
        public int getNb() {
             //specify what must happen
            return 1;
         }
    
         public int getNb2() {
             //specify what must happen
            return 2;
         }
     }
    
     public class SubClass extends SuperClass {
          //you can override the implementation
          @Override
          public int getNb2() {
            return 3;
         }
     }
    

    在这种情况下

    Subclass s = new SubClass();
      s.getNb(); //returns 1
      s.getNb2(); //returns 3
    
      SuperClass sup = new SuperClass();
      sup.getNb(); //returns 1
      sup.getNb2(); //returns 2
    

    我建议你做更多关于 dynamic binding, polymorphism and in general inheritance in Object-oriented programming 的研究

  • 30

    我注意到你的 Profiles 中有一些C问题 . 如果你从C中理解多重继承的概念(指的是从多个其他类继承特性的类),Java不允许这样做,但它确实有关键字 interface ,它有点像C中的纯虚拟类 . . 正如许多人所提到的那样,你是一个类(并且你只能从一个扩展),而你 implement 是一个接口 - 但你的类可以实现任意数量的接口 .

    即,这些关键字及其使用规则描述了Java中多重继承的可能性(您只能拥有一个超类,但您可以实现多个接口) .

  • 36

    extends 用于从基类继承(即扩展其功能) .

    implements 适用于实现接口的时间 .

    这是一个很好的起点:Interfaces and Inheritance .

  • 12

    class 只能"implement" interface . 只有一个 class "extends" a class . 同样, interface 可以扩展另一个 interface .

    class 只能扩展另一个 class . class 可以实现几个 interface .

    如果您更想知道何时使用 abstract class es和 interface s,请参阅此主题:Interface vs Abstract Class (general OO)

  • 7

    通常 implements 用于实现接口, extends 用于扩展基类行为或抽象类 .

    extends :派生类可以扩展基类 . 您可以重新定义已 Build 关系的行为 . 派生类“ is a ”基类类型

    implements :您正在实施 Contract . 实现接口“ has a ”功能的类 .

    在java 8发行版中,接口可以在接口中使用默认方法,它在接口本身提供实现 .

    请参阅此问题以了解何时使用它们:

    Interface vs Abstract Class (general OO)

    理解事物的例子 .

    public class ExtendsAndImplementsDemo{
        public static void main(String args[]){
    
            Dog dog = new Dog("Tiger",16);
            Cat cat = new Cat("July",20);
    
            System.out.println("Dog:"+dog);
            System.out.println("Cat:"+cat);
    
            dog.remember();
            dog.protectOwner();
            Learn dl = dog;
            dl.learn();
    
            cat.remember();
            cat.protectOwner();
    
            Climb c = cat;
            c.climb();
    
            Man man = new Man("Ravindra",40);
            System.out.println(man);
    
            Climb cm = man;
            cm.climb();
            Think t = man;
            t.think();
            Learn l = man;
            l.learn();
            Apply a = man;
            a.apply();
    
        }
    }
    
    abstract class Animal{
        String name;
        int lifeExpentency;
        public Animal(String name,int lifeExpentency ){
            this.name = name;
            this.lifeExpentency=lifeExpentency;
        }
        public void remember(){
            System.out.println("Define your own remember");
        }
        public void protectOwner(){
            System.out.println("Define your own protectOwner");
        }
    
        public String toString(){
            return this.getClass().getSimpleName()+":"+name+":"+lifeExpentency;
        }
    }
    class Dog extends Animal implements Learn{
    
        public Dog(String name,int age){
            super(name,age);
        }
        public void remember(){
            System.out.println(this.getClass().getSimpleName()+" can remember for 5 minutes");
        }
        public void protectOwner(){
            System.out.println(this.getClass().getSimpleName()+ " will protect owner");
        }
        public void learn(){
            System.out.println(this.getClass().getSimpleName()+ " can learn:");
        }
    }
    class Cat extends Animal implements Climb {
        public Cat(String name,int age){
            super(name,age);
        }
        public void remember(){
            System.out.println(this.getClass().getSimpleName() + " can remember for 16 hours");
        }
        public void protectOwner(){
            System.out.println(this.getClass().getSimpleName()+ " won't protect owner");
        }
        public void climb(){
            System.out.println(this.getClass().getSimpleName()+ " can climb");
        }
    }
    interface Climb{
        public void climb();
    }
    interface Think {
        public void think();
    }
    
    interface Learn {
        public void learn();
    }
    interface Apply{
        public void apply();
    }
    
    class Man implements Think,Learn,Apply,Climb{
        String name;
        int age;
    
        public Man(String name,int age){
            this.name = name;
            this.age = age;
        }
        public void think(){
            System.out.println("I can think:"+this.getClass().getSimpleName());
        }
        public void learn(){
            System.out.println("I can learn:"+this.getClass().getSimpleName());
        }
        public void apply(){
            System.out.println("I can apply:"+this.getClass().getSimpleName());
        }
        public void climb(){
            System.out.println("I can climb:"+this.getClass().getSimpleName());
        }
        public String toString(){
            return "Man :"+name+":Age:"+age;
        }
    }
    

    输出:

    Dog:Dog:Tiger:16
    Cat:Cat:July:20
    Dog can remember for 5 minutes
    Dog will protect owner
    Dog can learn:
    Cat can remember for 16 hours
    Cat won't protect owner
    Cat can climb
    Man :Ravindra:Age:40
    I can climb:Man
    I can think:Man
    I can learn:Man
    I can apply:Man
    

    重点要明白:

    • 狗和猫是动物,他们通过 Animal 分享 name,lifeExpentency 来扩展 remember ()和 protectOwner ()

    • 猫可以爬()但是狗没有 . 狗可以思考()但猫不会 . 通过实现该功能,这些特定功能将添加到 CatDog .

    • 男人不是动物,但他可以 Think,Learn,Apply,Climb

    通过这些例子,您可以理解这一点

    Unrelated classes can have capabilities through interface but related classes override behaviour through extension of base classes.

  • 75

    界面是对象可以执行的操作的描述...例如,当您打开灯开关时,灯亮起,您不关心它是怎么做的 . 在面向对象编程中,接口是对象必须具有的所有函数的描述才能成为“X” . 同样,作为一个例子,任何“行动喜欢”灯光的东西都应该有一个turn_on()方法和一个turn_off()方法 . 接口的目的是允许计算机强制执行这些属性并知道TYPE T的对象(无论接口是什么)必须具有称为X,Y,Z等的函数 .

    接口是一种编程结构/语法,允许计算机在对象(类)上强制执行某些属性 . 例如,假设我们有一个汽车类,一个踏板车类和一个卡车类 . 这三个类中的每一个都应该有一个start_engine()动作 . 每辆车的"engine is started"如何留给每个特定的 class ,但他们必须有一个start_engine动作的事实是interface的领域 .

  • 14

    Extends :这用于将父类的属性获取到基类中,并且可能包含已在子类中重写的已定义方法 .

    Implements :这用于通过在子类中定义接口(仅具有函数签名而不是其定义的父类)来实现它 .

    有一个特殊条件:"What if I want a new Interface to be the child of an existing interface?" . 在上面的条件中,子接口 extends 是父接口 .

  • 613
    • A扩展B:

    A和B都是类或两个接口

    • A实现B

    A是类,B是接口

    • 其中A是接口而B是类的情况在Java中是不合法的 .
  • 6

    Implements用于Interfaces,extends用于扩展类 .

    为了让它更容易理解,一个界面就像声音 - 界面 - 模型,你需要应用,跟随,以及你的想法 .

    Extend用于类,在这里,您通过向其添加更多功能来扩展已存在的内容 .

    还有一些说明:

    接口可以扩展另一个接口 .

    当您需要在实现接口或扩展特定场景的类之间进行选择时,请转到实现接口 . 因为类可以实现多个接口但只扩展一个类 .

  • 4

    当子类扩展一个类时,它允许子类继承(重用)并覆盖超类型中定义的代码 . 当一个类实现一个接口时,它允许从该类创建的对象在任何需要接口值的上下文中使用 .

    这里真正的问题是,当我们实现任何东西时,它只是意味着我们正在使用这些方法 . 它们的值和返回类型没有变化的余地 .

    但是当我们扩展任何东西时,它就成了你 class 的延伸 . 您可以更改它,使用它,重用它并且它不一定需要返回与超类中相同的值 .

  • 5

    在Java语言中创建自己的新类时,将使用这两个关键字 .

    区别: implements 表示您正在使用类中的Java接口元素 . extends 表示您正在创建要扩展的基类的子类 . 您只能在子类中扩展一个类,但您可以根据需要实现任意数量的接口 .

    有关更多详细信息,请参阅interface上的oracle文档页面 .

    这有助于阐明界面是什么,以及使用它们的惯例 .

  • 23

    如下图所示,一个类扩展了另一个类,一个接口扩展了另一个接口,但是一个类实现了一个接口 .
    enter image description here

    更多details

  • 6

    用最简单的术语 extends 用于继承 classimplements 用于在你的 class 中应用 interface

    extends

    public class Bicycle {
        //properties and methods
    }
    public class MountainBike extends Bicycle {
        //new properties and methods
    }
    

    implements

    public interface Relatable {
        //stuff you want to put
    }
    public class RectanglePlus implements Relatable {
        //your class code
    }
    

    如果你仍然有困惑,请阅读:https://docs.oracle.com/javase/tutorial/java/IandI/subclasses.html https://docs.oracle.com/javase/tutorial/java/IandI/usinginterface.html

  • 11

    我们使用SubClass只在子类想要使用已在SuperClass中声明的某些功能(方法或实例变量)时,或者如果我想稍微修改SuperClass的功能(方法重写)时才使用扩展SuperClass . 但是,例如,我有一个Animal类(SuperClass)和一个Dog类(SubClass),我在Animal类中定义的方法很少,例如 . doEat(); ,doSleep(); ... 还有很多 .

    现在,我的Dog类可以简单地扩展Animal类,如果我想让我的狗使用Animal类中声明的任何方法,我可以通过简单地创建一个Dog对象来调用这些方法 . 所以这样我可以保证我有一只可以吃饭睡觉的狗,做任何我想让狗做的事 .

    现在,想象一下,有一天,一些猫爱好者进入我们的工作区,她试图扩展动物类(猫也吃饭和睡觉) . 她创建了一个Cat对象并开始调用这些方法 .

    但是,有人试图制作一个Animal类的对象 . 你可以知道猫是如何睡觉的,你可以告诉狗狗是怎么吃的,你可以知道大象是如何饮酒的 . 但是在制作Animal类的对象时没有任何意义 . 因为它是一个模板,我们不想要任何一般的饮食方式 .

    所以相反,我更愿意创建一个抽象类,没有人可以实例化,但可以用作其他类的模板 .

    So to conclude, Interface is nothing but an abstract class(a pure abstract class) which contains no method implementations but only the definitions(the templates). So whoever implements the interface just knows that they have the templates of doEat(); and doSleep(); but they have to define their own doEat(); and doSleep(); methods according to their need.

    只有在想要重用SuperClass的某些部分时才能扩展(但请记住,您可以根据需要覆盖SuperClass的方法)并在需要模板时实现并希望自己定义它们(根据您的需要) .

    我将与您分享一段代码:您尝试使用不同的输入集并查看结果 .

    class AnimalClass {
    
    public void doEat() {
    
        System.out.println("Animal Eating...");
    }
    
    public void sleep() {
    
        System.out.println("Animal Sleeping...");
    }
    
    }
    
    public class Dog extends AnimalClass implements AnimalInterface, Herbi{
    
    public static void main(String[] args) {
    
        AnimalInterface a = new Dog();
        Dog obj = new Dog();
        obj.doEat();
        a.eating();
    
        obj.eating();
        obj.herbiEating();
    }
    
    public void doEat() {
        System.out.println("Dog eating...");
    }
    
    @Override
    public void eating() {
    
        System.out.println("Eating through an interface...");
        // TODO Auto-generated method stub
    
    }
    
    @Override
    public void herbiEating() {
    
        System.out.println("Herbi eating through an interface...");
        // TODO Auto-generated method stub
    
    }
    
    
    }
    

    Defined Interfaces

    public interface AnimalInterface {
    
    public void eating();
    
    }
    
    
    interface Herbi {
    
    public void herbiEating();
    
    }
    

相关问题