永发信息网

java,接口

答案:1  悬赏:10  手机版
解决时间 2021-01-15 16:46
  • 提问者网友:一抹荒凉废墟
  • 2021-01-14 23:22
java,接口
最佳答案
  • 五星知识达人网友:撞了怀
  • 2021-01-14 23:54
interface Pet{
public String getname();
public String move();
public String speak();
public String toString();
}

class Cat implements Pet{
String name;
public Cat(String n){
name = n;
}
public String getname(){
return name;
}
public String move(){
return "run";
}
public String speak(){
return "miao miao";
}
public String toString(){
return "My pet:" + name;
}
}

class Dog implements Pet{
int weight;
String name;
public Dog(String s,int t){
name=s;
weight=t;
}
public String move() {
return "run";
}
public String speak() {
return "wang wang";
}
public String getname() {
return name;
}
public int getweight() {
return weight;
}
public String toString() {
return "My pet:" + name;
}
}

public class JLab0504A {
public static void main(String[] args){
Cat myPet = new Cat("George");
Dog myDog = new Dog("Spot",20);
System.out.println(myPet.toString());
System.out.println("name:"+myPet.getname());
System.out.println("speak: "+myPet.speak());
System.out.println("move:"+myPet.move());
System.out.println();
System.out.println(myDog.toString());
System.out.println("name:"+myDog.getname());
System.out.println("speak:"+myDog.speak());
System.out.println("move:"+myDog.move());
System.out.println("weight:"+myDog.getweight());
}
}
//代码已运行过
我要举报
如以上回答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
点此我要举报以上问答信息
大家都在看
推荐资讯