改正下面java代码的错误部分

来源:百度知道 编辑:UC知道 时间:2024/09/28 15:15:05
改正下面java代码的错误部分,要求如下,
class Tangle{
private double width;
private double height;
开头Tangle类的width,height必须为私有的,这里不能改变;完整代码如下:
class Tangle{
private double width;
private double height;
static String color="红";
public Tangle(){
width=1;
height=1;
color="红";
}
//构造一个指定宽度和高度的长方形
public Tangle(double width,double height,String color){
width=width;
height=height;
color=color;
}
//返回长方形的宽度
public double getwidth(){
return width;
}
//给长方形设一个新的宽度
void setwidth(double newwidth){

width=newwidth;
}
//返回长方形的高度
double getheight(){
return height;
}
//给长方形设一个新的高度
public void setheight(double newheiht){
height=3;
}
//返回颜色
public String getcolor(){
return color;
}
public void setcolor(String newcolor){
color=newcolor;
}
//返回面积
public double findaream(){

//构造一个指定宽度和高度的长方形
public Tangle(double width,double height,String color){
this.width=width;
this.height=height;
this.color=color;
}

public class Rectangle{
public static void main(String[] args){
Tangle h=new Tangle();
h.setwidth(3);
h.setheight(5);
h.setcolor(黄色);
System.out.println("宽度为:"+h.getwidth()+"\n"+"高度为:"+h.getheight()+"\n"+"颜色为:"+"黄色"+"\n"+"面积为:"+h.findaream());
}
}

//必须是私有的,所以不能直接赋值取值,要调用set和get方法来进行赋值和取值

h.setwidth(3);
h.setwidth(5);
h.color = "黄色";
System.out.println("宽度为:" + h.getwidth() + "\n" + "高度为:" + h.getheight() + "\n"+ "颜色为:" + "黄色" + "\n" + "面积为:" + h.findaream());

声明为private 使用区域是类内的 其他类是.不出来的

static String color="红";
既然声明为stati