📄 test.java
字号:
class Rectangle
{
protected int width;
protected int height;
//下面是类Rectangle的三个构造方法
public Rectangle()
{ //此方法无参数,缺省地给出长和宽
width=20;height=30;
}
public Rectangle(int w,int h)
{ //此方法由参数给出长和宽
width=w;height=h;
}
public Rectangle(Rectangle r)
{ //此方法给出另一个Rectangle作参数
width=r.width();
height=r.height();
}
public int width()
{
return width;
}
public int height()
{
return height;
}
}
class Test
{ //分别声明、实例化和初始化Rectangle类的三个对象r1、r2和r3
public static void main(String args[])
{
//调用第一个构造方法,初始化r1
Rectangle r1=new Rectangle();
int w1=r1.width();
int h1=r1.height();
System.out.println("width="+w1+" height="+h1);
//调用第二个构造方法,初始化r2
Rectangle r2=new Rectangle(12,20);
int w2=r2.width();
int h2=r2.height();
System.out.println("width="+w2+" height="+h2);
//调用第三个构造方法,初始化r3
Rectangle r3=new Rectangle(r1);
int w3=r3.width();
int h3=r3.height();
System.out.println("width="+w3+" height="+h3);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -