package com.demo;
public class Test{
/**
* @param args
*/
public static void main(String[] args) {
System.out.println("请输入矩形的长和宽:");
Shape rec = new Rectangle();
rec.ciru();
rec.area();
System.out.println("请输入圆的半径:");
Shape cir = new Circular();
cir.area();
cir.ciru();
}
}以下是全部代码package com.demo;
public abstract class Shape {
abstract void ciru();//周长
abstract void area();//面积
}package com.demo;
import java.util.Scanner;
public abstract class Rectangle extends Shape {
Scanner input = new Scanner(System.in);
int length = input.nextInt();
int width = input.nextInt();
@Override
void ciru() {
// TODO Auto-generated method stub
System.out.println("矩形的周长是:" + 2*(length + width));
}
@Override
void area() {
// TODO Auto-generated method stub
System.out.println("矩形的面积是:" + length*width);
}
}package com.demo;
import java.util.Scanner;
public abstract class Circular extends Shape {
static double PI = 3.14;
Scanner input = new Scanner(System.in);
int r = input.nextInt();
@Override
void ciru() {
// TODO Auto-generated method stub
System.out.println("圆的周长为:" + 2*PI*r);
}
@Override
void area() {
// TODO Auto-generated method stub
System.out.println("圆的面积为:" + PI*r*r);
}
}package com.demo;
public class Test{
/**
* @param args
*/
public static void main(String[] args) {
System.out.println("请输入矩形的长和宽:");
Shape rec = new Rectangle();
rec.ciru();
rec.area();
System.out.println("请输入圆的半径:");
Shape cir = new Circular();
cir.area();
cir.ciru();
}
}
添加回答
举报
0/150
提交
取消