好友
阅读权限 10
听众
最后登录 1970-1-1
本帖最后由 hcz888 于 2021-3-9 01:14 编辑
复习了九天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();
}
}
免费评分
查看全部评分
发帖前要善用【论坛搜索 】 功能,那里可能会有你要找的答案或者已经有人发布过相同内容了,请勿重复发帖。