代码有点小毛病,不能分开赋值,求大佬指点
父类
package project55;
public abstract class Shape {
public abstract void c(int a,int b,int r);
public abstract void s(int a,int b,int r);
}
矩形
package project55;
public class Rectangle extends Shape {
@Override
public void c(int a,int b,int r) {
// TODO Auto-generated method stub
System.out.println("矩形周长:"+(a+b)*2);
}
@Override
public void s(int a,int b,int r) {
// TODO Auto-generated method stub
System.out.println("矩形面积:"+a*b);
}
}
圆形
package project55;
public class Circle extends Shape {
double π=3.14;
@Override
public void c(int a,int b,int r) {
// TODO Auto-generated method stub
System.out.println("圆形的周长:"+π*r*2);
}
@Override
public void s(int a,int b,int r) {
// TODO Auto-generated method stub
System.out.println("圆形的面积:"+π*r*r);
}
}
Initail函数
package project55;
import java.util.Scanner;
public class Initail {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner input=new Scanner(System.in);
System.out.print("请输入矩形的长a:");
int a=input.nextInt();
System.out.print("请输入矩形的宽b:");
int b=input.nextInt();
System.out.print("请输入圆形半径r:");
int r=input.nextInt();
Shape shape1=new Rectangle();
shape1.c(a,b,r);
shape1.s(a,b,r);
Shape shape2=new Circle();
shape2.c(a,b,r);
shape2.s(a,b,r);
}
}
结果
请输入矩形的长a:4
请输入矩形的宽b:5
请输入圆形半径r:6
矩形周长:18
矩形面积:20
圆形的周长:37.68
圆形的面积:113.03999999999999