3、抽象类练习 图形抽象类,含求周长和面积方法,写一个具体的图形继承该抽象类,如圆
2个回答
展开全部
图形抽象类的代码:
abstract class MyShape {
abstract int calGirth(); //求周长
abstract double calSquare(); //求面积
}
三角形类的实现:
public class Triangle extends MyShape{
int borderA, borderB, borderC;
Triangle(int a, int b, int c)
Triangle()
@Override
int calGirth() {
return borderA + borderB + borderC;
}
@Override
double calSquare() {
double p = calGirth() / 2;
return Math.sqrt(p * (p - borderA) * (p - borderB) * (p - borderC));
}
public static void main(String[] args) {
Triangle test = new Triangle(3, 4, 5);
System.out.println("The girth of the triangle is " + test.calGirth());
System.out.println("The square of the triangle is " + test.calSquare());
}
}
实现两个抽象函数,测试结果正确,输出为:
The girth of the triangle is 12
The square of the triangle is 6.0
abstract class MyShape {
abstract int calGirth(); //求周长
abstract double calSquare(); //求面积
}
三角形类的实现:
public class Triangle extends MyShape{
int borderA, borderB, borderC;
Triangle(int a, int b, int c)
Triangle()
@Override
int calGirth() {
return borderA + borderB + borderC;
}
@Override
double calSquare() {
double p = calGirth() / 2;
return Math.sqrt(p * (p - borderA) * (p - borderB) * (p - borderC));
}
public static void main(String[] args) {
Triangle test = new Triangle(3, 4, 5);
System.out.println("The girth of the triangle is " + test.calGirth());
System.out.println("The square of the triangle is " + test.calSquare());
}
}
实现两个抽象函数,测试结果正确,输出为:
The girth of the triangle is 12
The square of the triangle is 6.0
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
--
2022-12-05 广告
2022-12-05 广告
图形化编程简单理解为用积木块形式编程,scratch和python也是其中的一种,属于入门级编程,以其简单生动的画面获得无数学生的喜爱,深圳市创客火科技有限公司是一家做教育无人机的公司,旗下有编程无人机,积木无人机及室内外编队,每款飞机含有...
点击进入详情页
本回答由--提供
展开全部
abstract class graph{
public float getCircum();
public float getArea();
}
class circle extends graph{
private float p=3.14159f;
private float r;
public circle(float r){
this.r=r;
}
public float getCircum(){
return 2*p*r;
}
public float getArea(){
return p*r*r;
}
}
public float getCircum();
public float getArea();
}
class circle extends graph{
private float p=3.14159f;
private float r;
public circle(float r){
this.r=r;
}
public float getCircum(){
return 2*p*r;
}
public float getArea(){
return p*r*r;
}
}
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询