62,614
社区成员
发帖
与我相关
我的任务
分享
package P78.EX16;
class MyRectangle
{
private int Length,Width;
void setLength(int Length)
{
this.Length=Length;
}
void setWidth(int Width)
{
this.Width=Width;
}
int getWidth()
{
return Width;
}
int getLength()
{
return Length;
}
int getArea()
{
return Length*Width;
}
String tostring()
{
//String s=String.format("%d,%d",Length,Width);
//return s;这两种都可以实现 格式。
String s=Width+","+Length;
return s;
}
}
package P78.EX16;
class MySquare extends MyRectangle
{
private int Length;
int getArea()
{
return Length*Length;
}
String tostring()
{
String s=String.format("%d", Length);
return s;
}
}
package P78.EX16;
public class Mainclass
{
public static void main(String args[])
{
MyRectangle a=new MyRectangle();
a.setLength(3);
a.setWidth(2);
System.out.println(a.tostring());
System.out.println(a.getArea());
MyRectangle b=new MySquare();
b.setLength(8);
System.out.println(b.tostring());
System.out.println(b.getArea());
}
}
MyRectangle b=new MySquare();
b.setLength(8);
class MySquare extends MyRectangle
{
private int Length;
void setLength(int Length)
{
this.Length=Length;
}
int getArea()
{
return Length*Length;
}
String tostring()
{
String s=String.format("%d", Length);
return s;
}
}