class Circle {
創(chuàng)新互聯(lián)是一家專注于成都網(wǎng)站設計、網(wǎng)站建設與策劃設計,西藏網(wǎng)站建設哪家好?創(chuàng)新互聯(lián)做網(wǎng)站,專注于網(wǎng)站建設10余年,網(wǎng)設計領域的專業(yè)建站公司;建站業(yè)務涵蓋:西藏等地區(qū)。西藏做網(wǎng)站價格咨詢:13518219792
public static final double PI = 3.14159; //圓周率
private double radius; //半徑
//構造方法,將半徑置為0
public Circle() {
radius = 0.0;
}
//構造方法,創(chuàng)建Circle對象時將半徑初始化為r
public Circle(double r) {
this.radius = r;
}
//獲得圓的半徑值
public double getRadius(){
return this.radius;
}
//獲得圓的周長
public double getArea() {
return PI * radius * radius;
}
//獲得圓的面積
public double getPerimeter() {
return 2 * PI * radius;
}
// 將圓的半徑、周長、面積輸出到屏幕
public void disp() {
System.out.println("圓的半徑為:"+getRadius()+" 圓的周長為:"+getPerimeter()+" 圓的面積為:"+getArea());
}
}
//測試類
class testCircle {
public static void main(String args[]) {
Circle[] circleArray = new Circle[10];
for (int i = 0; i circleArray.length; i++) {
circleArray[i] = new Circle(Math.random()*100);
}
for (Circle circle : circleArray) {
circle.disp();
}
}
}
public class Exam
{
public static void main(String[] args)
{
Circle c=new Circle(3,4,5);
System.out.printf("圓心:(%f,%f),半徑:%f,面積:%f",c.x,c.y,c.r,c.countArea());
}
}
class Circle
{
public Circle()
{
this(0,0,0);
}
public Circle(double x,double y,double r)
{
this.x=x;
this.y=y;
this.r=r;
}
public double countArea()
{
return Math.PI*r*r;
}
/*private*/public double x,y,r;
}
public class Test {
public static void main(String[] args) {
//三角形
Triangle t = new Triangle(3.0,4.0,5.0);
t.GetArea();
//圓形
Circle c = new Circle(5.0);
c.getArea();
}
}
class Triangle {
double x, y, z, p, s;
public Triangle(double x, double y, double z) {
this.x = x;
this.y = y;
this.z = z;
}
public void GetArea() {
if (x + y = z || x + z = y || y + z = x)
System.out.println("不能構成三角形");
else {
p = (x + y + z) / 2;
s = (double) Math.sqrt(p * (p - x) * (p - y) * (p - z));
System.out.println("三角形面積為:"+s);
}
}
}
class Circle {
double r ;
public Circle(double r){
this.r = r;
}
public void getArea() {
double S = Math.PI * r * r;
System.out.print("圓形面積為:" + S);
}
}
我建的是類部類,你移出去一樣的。希望能幫到你!