1.编写一个Java程序,在程序中定义一个抽象类Shape,里面有一个求面积方法calArea,
编写一个Java程序,在程序中定义一个抽象类Shape,里面有一个求面积方法calArea,再定义Shape类的子类Rectangle类,在子类中实现父类的抽象方法。创建...
编写一个Java程序,在程序中定义一个抽象类Shape,里面有一个求面积方法calArea,再定义Shape类的子类Rectangle类,在子类中实现父类的抽象方法。创建主类TestShape测试程序。
import java.util.Scanner;
abstract class Shape{
abstract double GetArea();
}
// Write your own code
class Rectangle extends Shape{
int a,b;
Rectangle(){
}
Rectangle(int a,int b){
this.a = a;
this.b = b;
}
@Override
double GetArea() {
// TODO Auto-generated method stub
return (double)a*b;
}
}
public class test1{
public static void main(String[] args){
Scanner sca = new Scanner(System.in);
int aa = sca.nextInt();
int b = sca.nextInt();
Rectangle a = new Rectangle(aa,b);
System.out.println(a.GetArea());
}
}
不知道怎么改 抽象里,一改就错 展开
import java.util.Scanner;
abstract class Shape{
abstract double GetArea();
}
// Write your own code
class Rectangle extends Shape{
int a,b;
Rectangle(){
}
Rectangle(int a,int b){
this.a = a;
this.b = b;
}
@Override
double GetArea() {
// TODO Auto-generated method stub
return (double)a*b;
}
}
public class test1{
public static void main(String[] args){
Scanner sca = new Scanner(System.in);
int aa = sca.nextInt();
int b = sca.nextInt();
Rectangle a = new Rectangle(aa,b);
System.out.println(a.GetArea());
}
}
不知道怎么改 抽象里,一改就错 展开
1个回答
展开全部
没啥大问题,既然返回是double那么输入就用double
还有记住每一个类一个java文件,不要放在一个java文件中!!!
Shape.java类
public abstract class Shape{
abstract double GetArea();
}
Rectangle.java类
public class Rectangle extends Shape{
double a,b;
Rectangle(double a,double b){
this.a = a;
this.b = b;
}
@Override
double GetArea() {
return a*b;
}
}
Test1.java类
import java.util.Scanner;
public class Test1{
public static void main(String[] args){
Scanner sca = new Scanner(System.in);
double aa = sca.nextDouble();
double b = sca.nextDouble();
Rectangle a = new Rectangle(aa,b);
System.out.println(a.GetArea());
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询