public class TestRect {
public static void main(String[] args) {
Rect rect = new Rect();
System.out.println("矩形周长:" + rect.perimeter());
MyRect myRect = new MyRect(20,30);
System.out.println("周长:"+myRect.perimeter());
System.out.println("面积:"+myRect.area());
}
}
class Rect{
double width;
double height;
public Rect() {
this.width = 10;
this.height = 10;
}
public Rect(double width,double height) {
this.width = width;
this.height = height;
}
public double perimeter() {
return (width+height)*2;
}
}
class MyRect extends Rect{
public MyRect() {
}
public MyRect(double width,double height) {
super(width,height);
}
double area() {
return width*height;
}
}
public class Rect {
private int width;
private int height;
public Rect() {
super();
}
public int perimeter(){
int zc=(height+width)*2;
return zc;
}
public Rect(int width, int height) {
super();
this.width = width;
this.height = height;
}
public int getWidth() {
return width;
}
public void setWidth(int width) {
this.width = width;
}
public int getHeight() {
return height;
}
public void setHeight(int height) {
this.height = height;
}
}
public class MyRect extends Rect{
public int area(){
int mj=getWidth()*getHeight();
return mj;
}
}
public class TestReact {
public static void main(String[] args) {
Rect rect=new Rect(20, 10);
System.out.println("宽度:"+rect.getWidth()+",高度为:"+rect.getHeight()+"的矩形周长为:"+rect.perimeter());
MyRect myRect=new MyRect();
myRect.setHeight(10);
myRect.setWidth(20);
System.out.println("宽度:"+myRect.getWidth()+",高度为:"+myRect.getHeight()+"的矩形周长为:"+myRect.perimeter());
System.out.println("宽度:"+myRect.getWidth()+",高度为:"+myRect.getHeight()+"的矩形的矩形面积为:"+myRect.area());
}
}
代码如上,万望采纳。
我可以帮你写哦,关注一下,我帮你写。
代码如下:
1、矩形类:Rect
/**
* 矩形类
* @author zxl
* @date 2021/05/28
**/
public class Rect {
public int width;
public int height;
public Rect() {
this.width = 10;
this.height = 10;
}
public Rect(int width, int height) {
this.width = width;
this.height = height;
}
public int getWidth() {
return width;
}
public void setWidth(int width) {
this.width = width;
}
public int getHeight() {
return height;
}
public void setHeight(int height) {
this.height = height;
}
public int perimeter(){
return (width+height)*2;
}
}
2、我的矩形类:MyRect
/**
* 我的矩形类
* @author zxl
* @date 2021/05/28
**/
public class MyRect extends Rect{
public MyRect() {
}
public MyRect(int width, int height) {
super(width, height);
}
public int area(){
return super.height*super.width;
}
}
3、测试形状类:TestRect
/**
* 测试矩形
* @author zxl
* @date 2021/05/28
**/
public class TestRect {
public static void main(String[] args){
// 测试父类默认长宽周长
Rect rect = new Rect();
int perimeter = rect.perimeter();
System.out.println("测试父类默认长宽周长:"+perimeter);
// 测试父类指定长宽周长
Rect rect1 = new Rect(20,20);
int perimeter1 = rect1.perimeter();
System.out.println("测试父类指定长宽周长:"+perimeter1);
// 测试子类默认长宽周长
MyRect myRect = new MyRect();
int perimeter3 = myRect.perimeter();
System.out.println("测试子类默认长宽周长:"+perimeter3);
// 测试子类指定长宽周长
MyRect myRect1 = new MyRect(20,20);
int perimeter4 = myRect1.perimeter();
System.out.println("测试子类指定长宽周长:"+perimeter4);
// 测试子类默认长宽面积
MyRect myRect2 = new MyRect();
int area = myRect2.area();
System.out.println("测试子类默认长宽面积:"+area);
// 测试子类指定长宽面积
MyRect myRect3 = new MyRect(20,20);
int area1 = myRect3.area();
System.out.println("测试子类指定长宽面积:"+area1);
}
}
望采纳!
您好,我是有问必答小助手,您的问题已经有小伙伴解答了,您看下是否解决,可以追评进行沟通哦~
如果有您比较满意的答案 / 帮您提供解决思路的答案,可以点击【采纳】按钮,给回答的小伙伴一些鼓励哦~~
ps:问答VIP仅需29元,即可享受5次/月 有问必答服务,了解详情>>>https://vip.csdn.net/askvip?utm_source=1146287632