接口数组定义

接口数组定义

首页维修大全综合更新时间:2023-09-17 08:59:20

接口数组定义

可以的,给你写个例子

interface Speak {

void speak();

}

class Cat implements Speak {

public void speak() { System.out.println("meow"); }

}

class Dog implements Speak {

public void speak() { System.out.println("woof"); }

}

class Bird implements Speak {

public void speak() { System.out.println("chirrup"); }

}

class test

{

public static void main(String[] args) {

Speak[] pets = new Speak[]{ new Cat(), new Dog(), new Bird() };

for(Speak pet : pets)

pet.speak();

}

}

大家还看了
也许喜欢
更多栏目

© 2021 3dmxku.com,All Rights Reserved.