编写一个Java应用程序,要求编写一个抽象类A、两个抽象类的子类B、C和一个主类。要求该抽象类A有2个抽象方法:public abstract int f1(int xint y); public abstract double f2(double x,double y); 子类B中f1实现两个数相加,f2实现两个数相减,子类C中f1实现两个数相乘,f2实现两个数平方和。 要求:在应用程序的主类Ex2中,分别使用这两个子类创建对象,并分别让这两个子类对象的上转型对象调用f1()和f2()方法,并输出调用后的结果。
时间: 2023-05-16 14:06:04 浏览: 166
好的,以下是Java应用程序的代码:
抽象类A:
```
public abstract class A {
public abstract int f1(int x, int y);
public abstract double f2(double x, double y);
}
```
子类B:
```
public class B extends A {
@Override
public int f1(int x, int y) {
return x + y;
}
@Override
public double f2(double x, double y) {
return x - y;
}
}
```
子类C:
```
public class C extends A {
@Override
public int f1(int x, int y) {
return x * y;
}
@Override
public double f2(double x, double y) {
return x * x + y * y;
}
}
```
主类Ex2:
```
public class Ex2 {
public static void main(String[] args) {
A obj1 = new B();
A obj2 = new C();
int result1 = obj1.f1(3, 4);
double result2 = obj1.f2(3.0, 4.0);
System.out.println("Result1: " + result1);
System.out.println("Result2: " + result2);
int result3 = obj2.f1(3, 4);
double result4 = obj2.f2(3.0, 4.0);
System.out.println("Result3: " + result3);
System.out.println("Result4: " + result4);
}
}
```
输出结果:
```
Result1: 7
Result2: -1.0
Result3: 12
Result4: 25.0
```
希望这个答案能够帮到你!
阅读全文