「面积java继承」java继承求图形面积

博主:adminadmin 2022-11-23 07:02:05 64

本篇文章给大家谈谈面积java继承,以及java继承求图形面积对应的知识点,希望对各位有所帮助,不要忘了收藏本站喔。

本文目录一览:

java.继承和派生:编写一个程序计算出圆的面积、圆柱体的表面积和体积。

class Point{

private int x, y;

//get / set

public Point(){

}

public Point(int x, int y){

this.x=x; this.y=y;

}

}

继承就是再extends Point、并添加相应的方法、属性

java代码:用继承的方法根据三角形的面积求三棱锥及三棱柱的体积?利用最简单的代码。

public class TestTriangle {

public static void main(String[] args) {

Tera t = new Tera();

t.setBotton(5);

t.setHeight(6);

t.setteraHeight(7);

t.treaVolume();

}

}

class Triangle {

private float botton;

private float height;

public void setBotton(float botton) {

this.botton = botton;

}

public void setHeight(float height) {

this.height = height;

}

public float area() {

return botton*height/2;

}

}

class Tera extends Triangle {

private float teraHeight;

public void setteraHeight(float teraHeight) {

this.teraHeight = teraHeight;

}

public void treaVolume() {

float area = super.area();

float prismVolume = area*this.teraHeight;

float treaVolume = area*this.teraHeight/3;

System.out.println("三棱锥体积 "+treaVolume+"\n\n");

System.out.println("三棱柱体积 "+prismVolume+"\n\n");

}

}

我不知道公式有没有错哈!你可以运行试试看

用Java继承算面积

等了一下午?给你写了一个,用的是接口。。。

import java.text.DecimalFormat;

interface Shape{

public double getArea();

}

class Circle implements Shape{

private double radius;

public Circle(double r){

radius = r;

}

public double getArea(){

return Math.PI * radius *radius;

}

}

class Triangle implements Shape{

private double lineLong;

public Triangle(double l){

lineLong = l;

}

public double getArea(){

return lineLong * lineLong *Math.sin(Math.PI/3) /2;

}

}

class Square implements Shape{

private double lineLong;

public Square(double l){

lineLong = l;

}

public double getArea(){

return lineLong * lineLong;

}

}

public class Area{

public static void main(String[] args){

DecimalFormat fmt = new DecimalFormat("0.00");

Shape s1 = new Circle(2.0);

System.out.println("Circle area is:" + fmt.format(s1.getArea()));

Shape s2 = new Triangle(2.0);

System.out.println("Triangle area is: " + fmt.format(s2.getArea()));

Shape s3 = new Square(2.0);

System.out.println("Square area is: " + fmt.format(s3.getArea()));

}

}

要是非让用extends,就把interface 改成 class

把implements改成 extends,别的都不用改

java用继承打印圆和长方形的面积和周长,一定要用继承的方法

Shape.java

public class Shape {

// 半径

private double r;

// 长

private double l;

// 宽

private double h;

// 实例化圆的时候需要给出半径,长方形的长和宽

public Shape(double r, double l, double h) {

this.r = r;

this.l = l;

this.h = h;

}

// 圆的

public double calArea() {

return Math.PI * Math.pow(r, 2);

}

public double calLength() {

return 2 * r * Math.PI;

}

// 长方形的

public double calArea2() {

return l * h;

}

public double calLength2() {

return 2 * (l + h);

}

}

ShapeTest.java

public class ShapeTest extends Shape {

public ShapeTest(double r, double l, double h) {

super(r, l ,h);

// TODO Auto-generated constructor stub

}

public static void main(String[] args) {

ShapeTest ct = new ShapeTest(4.0, 3, 10);

System.out.println("圆的面积为:" + ct.calArea());

System.out.println("圆的周长为:" + ct.calLength());

System.out.println("长方形面积:"+ct.calArea2());

System.out.println("长方形周长:"+ct.calLength2());

}

}

望采纳

面积java继承的介绍就聊到这里吧,感谢你花时间阅读本站内容,更多关于java继承求图形面积、面积java继承的信息别忘了在本站进行查找喔。

The End

发布于:2022-11-23,除非注明,否则均为首码项目网原创文章,转载请注明出处。