如何创建表示矩形的2个类 [英] How do I create 2 classes that represent a rectangle

查看:75
本文介绍了如何创建表示矩形的2个类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要创建一个抽象的形状类,它具有设置和获取对象宽度的方法。然后我需要编写一个矩形类,使用设置和获取长度的方法扩展形状类,以及计算和打印区域(宽度x长度)。我不确定如何开始这个请帮助



我尝试过:



 public class Shape {

private double width = 100;
私人双倍身高= 500;

public Shape(int w,int h){
this.width = 5;
this.setHeight(2);
}
public Shape(双w,双h){
width = w;
height = h;
}
public void setWidth(double width){
this.width = width;
}

public double getWidth(){
返回宽度;
}
public double getHeight(){
返回高度;
}
public void setHeight(double height){
this.height = height;}
}

解决方案

你不知道抽象类是什么,请看看:摘要方法和类 [ ^

i need to create a abstract shape class that has methods of setting and getting the width of the object. then i need to write a rectangle class that extends the shape class with methods of setting and getting the length as well as calculating and printing the area (width x length). im not sure on how to start this please help

What I have tried:

public class Shape {

   private double width = 100;
   private double height = 500;

    public Shape(int w, int h) {
        this.width = 5;
        this.setHeight(2);
    } 
    public Shape(double w, double h) {
    width = w;
    height = h;
    }
    public void setWidth(double width){
    	   this.width = width;
    	}

    	public double getWidth(){
    	   return width;
    	}
		public double getHeight() {
			return height;
		}
	public void setHeight(double height) {
			this.height = height;}
}

解决方案

You have no inkling of what abstract class is, check this out: Abstract Methods and Classes[^]


这篇关于如何创建表示矩形的2个类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆