java的构造方法
声明一个矩形类Rectangle,其中有多个构造方法。用不同的构造方法创建对象,并输出矩形的周长和面积。我想尽快要这份程序,希望人才们给做份答案,本人先谢谢了...
声明一个矩形类Rectangle,其中有多个构造方法。用不同的构造方法创建对象,并输出矩形的周长和面积。
我想尽快要这份程序,希望人才们给做份答案,本人先谢谢了 展开
我想尽快要这份程序,希望人才们给做份答案,本人先谢谢了 展开
3个回答
展开全部
可以运行,源代码如下:
public class Rectangle
{
private double length;
private double width;
public Rectangle()
{
}
public Rectangle(double length, double width)
{
this.length=length;
this.width=width;
}
public void setLength(double length)
{
this.length=length;
}
public double getLength()
{
return length;
}
public void setWidth(double width)
{
this.width=width;
}
public double getWidth()
{
return width;
}
public String getValue()
{
String str="长方形的面积为:"+getLength()*getWidth()+";周长为:"+2*(getLength()+getWidth())+";";
return str;
}
public static void main(String[] args)
{
Rectangle r1=new Rectangle(2.0,3.0);
System.out.println(r1.getValue());
Rectangle r2=new Rectangle();
r2.setLength(4.0);
r2.setWidth(2.0);
System.out.println(r2.getValue());
}
}
public class Rectangle
{
private double length;
private double width;
public Rectangle()
{
}
public Rectangle(double length, double width)
{
this.length=length;
this.width=width;
}
public void setLength(double length)
{
this.length=length;
}
public double getLength()
{
return length;
}
public void setWidth(double width)
{
this.width=width;
}
public double getWidth()
{
return width;
}
public String getValue()
{
String str="长方形的面积为:"+getLength()*getWidth()+";周长为:"+2*(getLength()+getWidth())+";";
return str;
}
public static void main(String[] args)
{
Rectangle r1=new Rectangle(2.0,3.0);
System.out.println(r1.getValue());
Rectangle r2=new Rectangle();
r2.setLength(4.0);
r2.setWidth(2.0);
System.out.println(r2.getValue());
}
}
展开全部
两个构造方法就够了把...
一个是无参构造:
public Rectangle(){}
另一个是有参构造:
public Rectangle(int height, int width){
System.out.println("矩形的周长是:" + (height + width) * 2) ;
System.out.println("矩形的面积是:" + height * width) ;
}
你创建对象的时候传入不同参数就行了...
一个是无参构造:
public Rectangle(){}
另一个是有参构造:
public Rectangle(int height, int width){
System.out.println("矩形的周长是:" + (height + width) * 2) ;
System.out.println("矩形的面积是:" + height * width) ;
}
你创建对象的时候传入不同参数就行了...
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
public class Rectangle {
private int girth = 0;
private int space = 0;
private int length = 0;
private int width = 0;
public Rectangle()
{
this.length = 3;
this.width = 4;
}
public Rectangle(int length,int width)
{
this.length = length ;
this.width = width ;
}
public int calGirth()
{
return 2*(length+width);
}
public int calSpace()
{
return length*width;
}
public static void main(String args[])
{
Rectangle tRectangle = new Rectangle();
System.out.println("调用构造方法Rectangle()的到的周长和面积分别为:"+tRectangle.calGirth()+","+tRectangle.calSpace());
Rectangle mRectangle = new Rectangle(3,4);
System.out.println("调用构造方法Rectangle(int length,int width)的到的周长和面积分别为:"+mRectangle.calGirth()+","+mRectangle.calSpace());
}
}
private int girth = 0;
private int space = 0;
private int length = 0;
private int width = 0;
public Rectangle()
{
this.length = 3;
this.width = 4;
}
public Rectangle(int length,int width)
{
this.length = length ;
this.width = width ;
}
public int calGirth()
{
return 2*(length+width);
}
public int calSpace()
{
return length*width;
}
public static void main(String args[])
{
Rectangle tRectangle = new Rectangle();
System.out.println("调用构造方法Rectangle()的到的周长和面积分别为:"+tRectangle.calGirth()+","+tRectangle.calSpace());
Rectangle mRectangle = new Rectangle(3,4);
System.out.println("调用构造方法Rectangle(int length,int width)的到的周长和面积分别为:"+mRectangle.calGirth()+","+mRectangle.calSpace());
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询