java题,求解答啊!以矩形的面积计算为例,编写一个方法将引用类型作为参数...

发布网友

我来回答

3个回答

热心网友

public class Demo1
{
public static void main(String []args)throws IOException
{
Square a=new Square(1.1f,1.1f);
a.GetResults(a);

}
}

class Square
{
float width,length;
Square(float w,float l)
{
width=w;
length=l;
}
public void GetResults(Square s)
{
System.out.println("面积为:"+s.width*s.length);

}

}

感觉有点多此一举。。。。。为什么要再将Square 传过去呢;
public void GetResults()
{
System.out.println("面积为:"+width*length);
}
直接这样不可以吗

热心网友

参数类型要一致啊,下面的那个类改下就行,还有那个异常不要抛
class Square
{
Square s;
float width,length;
Square(float w,float l)
{
width=w;
length=l;
}
public void GetResults(float width2,float length2)
{
System.out.println("面积为:"+s.width*s.length);

}

}

热心网友

public class Demo1
{
public static void main(String []args)
{
try{
Square a=new Square(1.1f,1.1f);
a.GetResults();
}
catch(Exception e){
System.out.println(e.getMessage());
}

}
}

class Square
{

float width,length;
Square(float w,float l)
{
width=w;
length=l;
}
public void GetResults()
{
System.out.println("面积为:"+width*length);

}

}

声明声明:本网页内容为用户发布,旨在传播知识,不代表本网认同其观点,若有侵权等问题请及时与本网联系,我们将在第一时间删除处理。E-MAIL:11247931@qq.com