1.抽象类
定义一个抽象类Shape,包含求面积area和周长perimeter的两个函数。定义圆Circle类,继承Shape类。定义三角形Triangle,继承Shape类。定义长方形类Square,继承Shape类。并完成以上类的测试。
1.Shape.java
package work02;
public abstract class Shape {
abstract double area();
abstract double perimeter();
}
2.Circle01.java
package work02;
public class Circle01 extends Shape {
double r;
public Circle01(double r) {
this.r = r;
}
public double area() {
return Math.PI * r * r;
}
public double perimeter() {
return 2 * Math.PI * r;
}
public String toString() {
return "圆形面积:" + area() + " 圆形周长:" + perimeter();
}
}
3.Triangle.java
package work02;
public class Triangle extends Shape {
public double len;
public Triangle(double len) {
super();
this.len = len;
}
public double area() {
return Math.sqrt(3) * len * len / 4;
}
double perimeter() {
return 3 * len;
}
public String toString() {
return "正三角形面积:" + area() + " 正三角形周长:" + perimeter();
}
}
package work02;
public class Square extends Shape {
double x, y;
public Square(double x, double y) {
this.x = x;
this.y = y;
}
double area() {
return x * y;
}
double perimeter() {
return 2 * (x + y);
}
public String toString() {
return "长方形面积:" + area() + " 长方形周长:" + perimeter();
}
}
5.ShapeTest.java
package work02;
public class ShapeTest {
public static void main(String[]args){
Circle01 ce=new Circle01(2);
System.out.println(ce);
Triangle tr=new Triangle(4);
System.out.println(tr);
Square sq=new Square(2,3);
System.out.println(sq);
}
}
2.接口
定义一个Shape接口,包含求面积和周长的两个函数。定义一个坐标类Coordinates,包含两个成员变量X坐标和Y坐标。定义圆Circle类,继承Coordinates并实现Shape接口。定义三角形Triangle,继承Coordinates并实现Shape接口。定义长方形类Square,继承Coordinates并实现Shape接口。并完成以上类的测试。
1.Shape.java
package work02;
interface Shape {
public abstract double area();
public abstract double perimeter();
}
2.Coordinates.java
package work02;
public class Coordinates {
public double x,y;
public Coordinates(double x, double y) {
super();
this.x = x;
this.y = y;
}
}
3.Circle01.java
package work02;
public class Circle01 extends Coordinates implements Shape {
public double r;
public Circle01(double x,double y,double r) {
super(x,y);
this.r = r;
}
public double area() {
return Math.PI * r * r;
}
public double perimeter() {
return 2 * Math.PI * r;
}
public String toString() {
return "圆形面积:" + area() + " 圆形周长:" + perimeter();
}
}
4.Triangle.java
package work02;
public class Triangle extends Coordinates implements Shape {
public double len;
public Triangle(double x,double y,double len) {
super(x,y);
this.len = len;
}
public double area() {
return Math.sqrt(3) * len * len / 4;
}
public double perimeter() {
return 3 * len;
}
public String toString() {
return "正三角形面积:" + area() + " 正三角形周长:" + perimeter();
}
}
5.Square.java
package work02;
public class Square extends Coordinates implements Shape {
public Square(double x, double y) {
super(x,y);
//this.x = x;
//this.y = y;
}
public double area() {
return x * y;
}
public double perimeter() {
return 2 * (x + y);
}
public String toString() {
return "长方形面积:" + area() + " 长方形周长:" + perimeter();
}
}
6.ShapeTest.java
package work02;
public class ShapeTest {
public static void main(String[]args){
Shape ce=new Circle01(0,0,2);
System.out.println(ce);
Shape tr=new Triangle(0,0,4);
System.out.println(tr);
Shape sq=new Square(2,3);
System.out.println(sq);
}
}
/*
圆形面积:12.566370614359172 圆形周长:12.566370614359172
正三角形面积:6.928203230275509 正三角形周长:12.0
长方形面积:6.0 长方形周长:10.0
*/