Java编程,急
答案:3 悬赏:70 手机版
解决时间 2021-01-15 03:26
- 提问者网友:凉末
- 2021-01-14 13:17
Java编程,急
最佳答案
- 五星知识达人网友:野味小生
- 2021-01-14 14:19
interface Animal{
void cry();
String getAnimalName();
}
class Simulator{
public void playSound(Animal animal){
System.out.print("动物的名称:"+animal.getAnimalName()+" ,叫声:");
animal.cry();
System.out.println();
}
}
class Dog implements Animal{
String name;
public Dog(String name){
this.name = name;
}
public void cry() {
System.out.println("汪汪....");
}
public String getAnimalName() {
return this.name;
}
}
class Cat implements Animal{
String name;
public Cat(String name){
this.name = name;
}
public void cry() {
System.out.println("喵喵....");
}
public String getAnimalName() {
return this.name;
}
}
public class Application {
public static void main(String args[]){
Simulator simulator = new Simulator();
simulator.playSound(new Dog("Dog"));
simulator.playSound(new Cat("Cat"));
}
}运行结果:
动物的名称:Dog ,叫声:汪汪....
动物的名称:Cat ,叫声:喵喵....
void cry();
String getAnimalName();
}
class Simulator{
public void playSound(Animal animal){
System.out.print("动物的名称:"+animal.getAnimalName()+" ,叫声:");
animal.cry();
System.out.println();
}
}
class Dog implements Animal{
String name;
public Dog(String name){
this.name = name;
}
public void cry() {
System.out.println("汪汪....");
}
public String getAnimalName() {
return this.name;
}
}
class Cat implements Animal{
String name;
public Cat(String name){
this.name = name;
}
public void cry() {
System.out.println("喵喵....");
}
public String getAnimalName() {
return this.name;
}
}
public class Application {
public static void main(String args[]){
Simulator simulator = new Simulator();
simulator.playSound(new Dog("Dog"));
simulator.playSound(new Cat("Cat"));
}
}运行结果:
动物的名称:Dog ,叫声:汪汪....
动物的名称:Cat ,叫声:喵喵....
全部回答
- 1楼网友:过活
- 2021-01-14 16:03
public class Simulator {
public void playSound(Animal animal) {
animal.cry();
animal.getAnimalName();
}
public static void main(String[] args) {
Simulator simulator=new Simulator();
simulator.playSound(new Dog("Dog"));
simulator.playSound(new Cat("Cat"));
}
}
public interface Animal {
abstract void cry();
abstract String getAnimalName();
}
public class Cat implements Animal{
private String name;
public Cat(String name) {
this.name=name;
}
@Override
public void cry() {
System.out.println(name+" Cry!!!");
}
@Override
public String getAnimalName() {
System.out.println(name);
return name;
}
}
public class Dog implements Animal{
private String name;
public Dog(String name) {
this.name=name;
}
@Override
public void cry() {
System.out.println(name+" Cry!!!");
}
@Override
public String getAnimalName() {
System.out.println(name);
return name;
}
}
public void playSound(Animal animal) {
animal.cry();
animal.getAnimalName();
}
public static void main(String[] args) {
Simulator simulator=new Simulator();
simulator.playSound(new Dog("Dog"));
simulator.playSound(new Cat("Cat"));
}
}
public interface Animal {
abstract void cry();
abstract String getAnimalName();
}
public class Cat implements Animal{
private String name;
public Cat(String name) {
this.name=name;
}
@Override
public void cry() {
System.out.println(name+" Cry!!!");
}
@Override
public String getAnimalName() {
System.out.println(name);
return name;
}
}
public class Dog implements Animal{
private String name;
public Dog(String name) {
this.name=name;
}
@Override
public void cry() {
System.out.println(name+" Cry!!!");
}
@Override
public String getAnimalName() {
System.out.println(name);
return name;
}
}
- 2楼网友:空山清雨
- 2021-01-14 14:33
Animal
public interface Animal {
public void cry();
public String getAnimalName();
}
Cat
public class Cat implements Animal {
private String name;
public Cat(String name) {
this.name = name;
}
@Override
public void cry() {
System.out.println("Cat " + name + " cry");
}
@Override
public String getAnimalName() {
return this.name;
}
}
Dog
public class Dog implements Animal {
private String name;
public Dog(String name) {
this.name = name;
}
@Override
public void cry() {
System.out.println("Dog " + name + " cry");
}
@Override
public String getAnimalName() {
return this.name;
}
}
Simulator
public class Simulator {
public void playSound(Animal animal) {
animal.cry();
}
}
Application
public class Application {
public static void main(String[] args) {
Simulator simulator = new Simulator();
simulator.playSound(new Dog("Dog"));
simulator.playSound(new Cat("Cat"));
}
}
我要举报
如以上问答信息为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
推荐资讯