用java编写一个类,描述桌子括以下几种属性:长宽高颜色。
答案:4 悬赏:0
解决时间 2021-01-18 12:32
- 提问者网友:不要迷恋哥
- 2021-01-18 04:11
用java编写一个类,描述桌子括以下几种属性:长宽高颜色。
最佳答案
- 二级知识专家网友:痴妹与他
- 2021-01-18 05:18
import java.awt.Color;
import static java.lang.System.out;
public class Desk
{
private int width; //宽度
private int height; //高度
private Color color; //颜色
public Desk(int width, int height)
{
this.width = width;
this.height = height;
}
public Desk(Color color)
{
this.color = color;
}
public Desk(int width, int height, Color color)
{
this(width, height);
this.color = color;
}
public void showAttribute()
{
out.println("width = "+width
+"height = "+height
+"Color = " +color);
}
public static void main(String[] args)
{
//建立三个对象
Desk d1 = new Desk(10,20);
Desk d2 = new Desk(Color.GREEN);
Desk d3 = new Desk(10,20,Color.GREEN);
//分别显示每个对象的所有属性
d1.showAttribute();
d2.showAttribute();
d3.showAttribute();
}
}
import static java.lang.System.out;
public class Desk
{
private int width; //宽度
private int height; //高度
private Color color; //颜色
public Desk(int width, int height)
{
this.width = width;
this.height = height;
}
public Desk(Color color)
{
this.color = color;
}
public Desk(int width, int height, Color color)
{
this(width, height);
this.color = color;
}
public void showAttribute()
{
out.println("width = "+width
+"height = "+height
+"Color = " +color);
}
public static void main(String[] args)
{
//建立三个对象
Desk d1 = new Desk(10,20);
Desk d2 = new Desk(Color.GREEN);
Desk d3 = new Desk(10,20,Color.GREEN);
//分别显示每个对象的所有属性
d1.showAttribute();
d2.showAttribute();
d3.showAttribute();
}
}
全部回答
- 1楼网友:雪起风沙痕
- 2021-01-18 08:52
请给分,为什么不给分就提问?
- 2楼网友:北城痞子
- 2021-01-18 08:01
你是在学面向对象吗,同学追问类与对象
- 3楼网友:鱼忧
- 2021-01-18 06:41
自己动手做呗,简单到不能再简单的入门例题都不想去做吗。追问不是本专业追答跟是不是什么专业有什么关系,你要学java就要认真去学,自己思考努力去试着做,如果是想混混交任务的话你学这干嘛,有那时间玩不是更好呢。
我要举报
如以上回答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
点此我要举报以上问答信息!
大家都在看
推荐信息