Java object oriented interface (2)

Java object oriented interface (2)

Portal: java object-oriented interface (1)

Interface definition and implementation interface

interface Countable {
    int NUM = 50;
}

interface Edible {
    String name = "Edible";
    void howToEat();
}
interface CreatureDoing extends Edible,Countable {
    public abstract void howToSleep();
}
System.out.println(CreatureDoing.name);//Edible
System.out.println(CreatureDoing.NUM);//50
//类实现多个接口
class 类 implements 接口1,接口2{}
//类继承某类,且实现多个接口
class 类1 extends 类2 implements 接口1,接口2{}

Interface and polymorphism

//接口也实现了多态
Edible[] ediblesArray = new Edible[]{new Chicken(),new Orange()};
for (Edible edible : ediblesArray) {
    edible.howToEat();
}

Does the interface inherit from object

System.out.println(cd.hashCode());
package com.my.pac19;

/**
 * @auther Summerday
 */
public class TestEdible {
    public static void main(String[] args) {

        System.out.println(Countable.NUM);
        System.out.println(Edible.name);
        //接口 不能用于创建实例,但是可以声明引用类型变量指向其实现类的对象
        CreatureDoing cd = new Chicken();
        System.out.println(cd.hashCode());
        System.out.println();
        //接口也实现了多态
        Edible[] ediblesArray = new Edible[]{new Chicken(),new Orange()};
        for (Edible edible : ediblesArray) {
            edible.howToEat();
        }
    }
}

interface Countable {
    int NUM = 50;
}

interface Edible {
    String name = "Edible";

    public abstract void howToEat();
    //redundant 多余的

}

//接口继承接口
interface CreatureDoing extends Edible,Countable {
    public abstract void howToSleep();

}

abstract class Animal {

    public abstract void call();
}
class Chicken extends Animal implements Edible,CreatureDoing {
    //实现Edible中的howToEat方法
    @Override
    public void howToEat() {
        System.out.println("鸡要烤着吃");
    }
    //实现Animal中的call方法
    @Override
    public void call() {
        System.out.println("咯咯哒");
    }
    //实现Creature中的howToSleep方法
    @Override
    public void howToSleep() {
        System.out.println("呼呼");
    }
}
class Orange implements Edible {

    @Override
    public void howToEat() {
        System.out.println("橘子要剥皮吃");
    }
}

If there is any improper description in this article, please leave a message in the comment area!

Reference link: https://stackoverflow.com/questions/6056124/do-interfaces-inherit-from-object-class-in-java?r=SearchResults https://docs.oracle.com/javase/specs/jls/se7/html/jls-9.html#jls -9.2

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>