吾爱破解 - LCG - LSG |安卓破解|病毒分析|www.52pojie.cn

 找回密码
 注册[Register]

QQ登录

只需一步,快速开始

查看: 2269|回复: 6
收起左侧

[Java 转载] 初学java 编写求正方形/长方形的周长与面积,正方体/长方体 的表面积与体积

[复制链接]
hcz888 发表于 2021-3-9 00:59
本帖最后由 hcz888 于 2021-3-9 01:14 编辑

image.png

复习了九天java,分享一下代码  
[Java] 纯文本查看 复制代码
package aaa;[/color][/size]

public interface PlaneGraphics2 {
public abstract double area();
public abstract double perimeter();
public abstract void print();
}



[Java] 纯文本查看 复制代码
package aaa;

public interface SolidGraphics1 {
public abstract double volume(); 
}



[Java] 纯文本查看 复制代码
package aaa;

public class Rectangle2 implements PlaneGraphics2
{
        protected double length;
        protected double width;
        
        Rectangle2(double length,double width){
                this.length=length;
                this.width=width;
        }
        Rectangle2(double length)
        {
                this.length=length;
        }
        Rectangle2(){
                this(0,0);
        }
        Rectangle2(Rectangle2 r1)
        {
                this(r1.length,r1.width);
        }
        public double perimeter()
        {
                return 2*(this.length+this.width);
        }
        public double area(){
                return this.length*this.width;
        }
        public void print(){
                if(this.length==this.width){
                        System.out.println("正方形,"+"边长是:"+this.length);
                }
                else{
                        System.out.println("长方形,"+"长是:"+this.length+"宽是:"+this.width);
                }
                System.out.println("周长是:"+this.perimeter()+"面积是:"+this.area());
        }
        
}



[Asm] 纯文本查看 复制代码
package aaa;

public class Cuboid1 extends Rectangle2 implements SolidGraphics1 {

        protected double height;
        
        Cuboid1(double length,double width,double height){
                this.length=length;
                this.width=width;
                this.height=height;
        }
        Cuboid1(Rectangle2 r1,double height){
                super(r1.length,r1.width);
                this.height=height;
        }
        Cuboid1(double length){
                this(length,length,length);
        }
        Cuboid1(){
                this(0,0,0);
        }
        public double perimeter(){
                return 0;
        }
        public double area(){
                return length*width*2+length*height*2+width*height*2;
        }
         public double volume()   {
                return super.area()*this.height;
        }
        public void print(){
                if(this.length==this.width&&this.width==this.height){
                        System.out.println("一个正方体"+",边长为"+this.length);
                }
                else{
                        System.out.println("一个长方体"+",长为"+this.length+
                                        "宽为"+this.width+"高为"+this.height);
                }
                System.out.println(",体积为"+this.volume()+"表面积"+this.area());
        }
}




[Java] 纯文本查看 复制代码
package aaa;

public class C_ex {
public static void main(String args[])
{
        PlaneGraphics2 s1=new Cuboid1(10,10,10);
        s1.print();
        PlaneGraphics2 s2=new Rectangle2(10,100);
        s2.print();
}
}

免费评分

参与人数 1吾爱币 +1 热心值 +1 收起 理由
芽衣 + 1 + 1 用心讨论,共获提升!

查看全部评分

发帖前要善用论坛搜索功能,那里可能会有你要找的答案或者已经有人发布过相同内容了,请勿重复发帖。

美丽的渗透者 发表于 2021-3-9 07:09
这个挺简单的,谢谢楼主
头像被屏蔽
First丶云心 发表于 2021-3-9 08:31
QingYi. 发表于 2021-3-9 08:51
神幻静 发表于 2021-3-9 08:57
给新入坑的小伙伴鼓励
fht000 发表于 2021-3-9 09:00
本帖最后由 fht000 于 2021-3-9 09:04 编辑

你这SolidGraphics1不应该继承PlaneGraphics2接口吗?或者Cuboid1同时实现这2个接口。反而Cuboid1去继承Rectangle2类,不符合面向接口编程的理念
救赎之旅 发表于 2021-3-9 10:11
java小伙伴
您需要登录后才可以回帖 登录 | 注册[Register]

本版积分规则 警告:本版块禁止灌水或回复与主题无关内容,违者重罚!

快速回复 收藏帖子 返回列表 搜索

RSS订阅|小黑屋|处罚记录|联系我们|吾爱破解 - LCG - LSG ( 京ICP备16042023号 | 京公网安备 11010502030087号 )

GMT+8, 2024-5-21 10:26

Powered by Discuz!

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表