⋯⋯⋯⋯⋯⋯⋯⋯⋯⋯⋯⋯⋯⋯⋯⋯⋯⋯⋯⋯⋯⋯⋯⋯⋯⋯⋯⋯⋯⋯⋯⋯⋯⋯⋯⋯⋯⋯
仅供参考
public class ShapeTest {
abstract class Shape{
public abstract double area();
public abstract double perimeter();
}
class Rect extends Shape{
private double width;
private double length;
public Rect(double w,double l){
width = w;
length = l;
}
@Override
public double area() {
return width*length;
}
@Override
public double perimeter() {
return 2*(width+length);
}
}
class Circle extends Shape{
private double r;
public Circle(double r){
this.r = r;
}
@Override
public double area() {
return Math.PI * r * r;
}
@Override
public double perimeter() {
return 2 * Math.PI * r;
}
}
public static void main(String[] args){
ShapeTest st = new ShapeTest();
Rect r = st.new Rect(2,3);
System.out.println("矩形的周长为:"+r.perimeter());
System.out.println("矩形的面积为:"+r.area());
Circle c = st.new Circle(1); //单位圆
System.out.println("圆形的周长为:"+c.perimeter());
System.out.println("圆形的面积为:"+c.area());
}
}
定义一个抽象类Shape ,两个抽象方法,area , perimeter ,定义矩形类Rectangle ,继承抽象类 ,实现两个抽象方法,定义两个属性height ,width;
public int area(){
return height*width;
}
就这样做
考察的是面向对象的抽象思维,抽象父类,以及它对应的各个子类。建议先好好研究下课本上的例子,然后仿造例子思考该题目。
public abstract class Shape {
public abstract double area();
public abstract double perimeter();
}
public class Rectangle extends Shape {
private double length;
private double width;
public double getLength() {
return length;
}
public void setLength(double length) {
this.length = length;
}
public double getWidth() {
return width;
}
public void setWidth(double width) {
this.width = width;
}
@Override
public double area() {
return getLength() * getWidth();
}
@Override
public double perimeter() {
return 2 * (getWidth() + getWidth());
}
}
public class Triangle extends Shape {
private double a, b, c;
public double getA() {
return a;
}
public void setA(double a) {
this.a = a;
}
public double getB() {
return b;
}
public void setB(double b) {
this.b = b;
}
public double getC() {
return c;
}
public void setC(double c) {
this.c = c;
}
@Override
public double area() {
double p = (getA() + getB() + getC()) / 2;
return Math.sqrt(p * (p - getA()) * (p - getB()) * (p - getC()));
}
@Override
public double perimeter() {
return getA() + getB() + getC();
}
}
public class Circle extends Shape {
private double diameter;
public double getDiameter() {
return diameter;
}
public void setDiameter(double diameter) {
this.diameter = diameter;
}
@Override
public double area() {
return Math.PI * Math.pow(getDiameter() / 2, 2);
}
@Override
public double perimeter() {
return Math.PI * getDiameter();
}
}
public class 测试 {
public static void main(String [] args){
Rectangle rec = new Rectangle();
rec.setLength(10);
rec.setWidth(5);
double rec_area = rec.area();
double rec_perimeter = rec.perimeter();
System.out.println("矩形的面积:"+rec_area+",周长"+rec_perimeter);
Triangle tri = new Triangle();
tri.setA(3);
tri.setB(4);
tri.setC(5);
double tri_area = tri.area();
double tri_perimeter = tri.perimeter();
System.out.println("三角形的面积:"+tri_area+",周长"+tri_perimeter);
Circle cir = new Circle();
cir.setDiameter(10);
double cir_area = cir.area();
double cir_perimeter = cir.perimeter();
System.out.println("圆形的面积:"+cir_area+",周长"+cir_perimeter);
}
}
考察抽象类的使用。也可以使用接口进行操作。
// 抽象类
public abstract class Shape {
void area() {}
void perimeter() {}
}
public class TestCycle extends Shape {
// 半径
private int r;
public TestCycle(int r) {
this.r = r;
}
@Override
public void area() {
System.out.println("圆形的面积: " + Math.PI * r * r);
}
@Override
public void perimeter() {
System.out.println("圆形的周长: " + 2 * Math.PI * r);
}
public static void main(String args[]){
int r = 5;
TestCycle tc = new TestCycle(r);
tc.area();
tc.perimeter();
}
// 圆形的面积: 78.5
// 圆形的周长: 31.4
}
// 矩形
public class TestRectangle extends Shape {
private int chang;
private int kuan;
public TestRectangle(int c, int k) {
this.chang = c;
this.kuan = k;
}
@Override
public void area() {
int area = chang * kuan;
System.out.println("矩形的面积: " + area);
}
@Override
public void perimeter() {
int perimeter = (2*chang) * (2*kuan);
System.out.println("矩形的周长: " +perimeter);
}
public static void main(String args[]){
int c = 5;
int k = 2;
TestRectangle tc = new TestRectangle(c, k);
tc.area();
tc.perimeter();
}
// 输出 面积是 10
// 输出 周长是 40
}