补全代码 ``` class Animal{ void cry(){} } //重写cry实现多态 class Dog ________ Animal{ void cry(){ System.out.println("Wang!Wang!......"); } } //重写cry实现多态 class Cat ________ Animal{ void cry(){ System.out.println("miao~~miao~~..."); } } _______ class Example5_9{ public static void main(String args[]){ Animal _______; //================== animal=____ Dog(); //animal是Dog对象的上转型对象 animal.cry(); //上转型对象可以代替子类对象去调用子类重写的实例方法, //================== animal=_____ Cat(); //animal是Cat对象的上转型对象 animal.cry(); } ``` ```java interface Computable{ final int MAX=100; int f(int x); //不用实现 public ________ int g(int x,int y); //虚拟方法,不用实现 } class A _________ Computable{ public int f(int x){ return x*x; } public int g(int x,int y){ return x+y; } } class B ___________ Computable{ //递两个参数 public int f(int x){ return x*x*x; } //递两个参数 public int g(int x,int y){ return x*y; } } public class Example5_11{ public ________ void main(String args[]){ Computable a,b; a=_____ A(); b=_____ B(); System.out.println(a.MAX); System.out.println(""+a.f(10)+" "+a.g(12,6)); //硬传递两个参数 System.out.println(b.MAX); System.out.println(""+b.f(10)+" "+b.g(29,2)); //硬传递两个参数 } }
时间: 2023-12-13 11:03:16 浏览: 142
class_design.zip_class design java
补全代码:
```java
class Animal{
void cry(){}
}
//重写cry实现多态
class Dog extends Animal{
void cry(){
System.out.println("Wang!Wang!......");
}
}
//重写cry实现多态
class Cat extends Animal{
void cry(){
System.out.println("miao~~miao~~...");
}
}
public class Example5_9{
public static void main(String args[]){
Animal animal;
//==================
animal = new Dog(); //animal是Dog对象的上转型对象
animal.cry(); //上转型对象可以代替子类对象去调用子类重写的实例方法,
//==================
animal = new Cat(); //animal是Cat对象的上转型对象
animal.cry();
}
}
interface Computable{
final int MAX=100;
int f(int x); //不用实现
public abstract int g(int x,int y); //虚拟方法,不用实现
}
class A implements Computable{
public int f(int x){
return x*x;
}
public int g(int x,int y){
return x+y;
}
}
class B implements Computable{
//递两个参数
public int f(int x){
return x*x*x;
}
//递两个参数
public int g(int x,int y){
return x*y;
}
}
public class Example5_11{
public static void main(String args[]){
Computable a,b;
a=new A();
b=new B();
System.out.println(a.MAX);
System.out.println(""+a.f(10)+" "+a.g(12,6)); //硬传递两个参数
System.out.println(b.MAX);
System.out.println(""+b.f(10)+" "+b.g(29,2)); //硬传递两个参数
}
}
```
阅读全文