我在下面的问题中遇到错误。帮我跟踪错误 [英] I get an error in the below problem. Help me trace the error
本文介绍了我在下面的问题中遇到错误。帮我跟踪错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
public class Demo1
{
class Rectangle
{
private double length;
private double width;
public void setLength(double len)
{
length = len;
}
public void setWidth(double wid)
{
width = wid;
}
public double getLength()
{
return length;
}
public double getWidth()
{
return width;
}
public double getArea()
{
return length*width;
}
}
public static void main(String[] args)
{
Rectangle rect = new Rectangle();
rect.setLength(10);
rect.setWidth(10);
System.out.println("The value of length is " + rect.getLength());
System.out.println("The value of length is " + rect.getWidth());
System.out.println("The value of length is " + rect.getArea());
}
}
我的尝试:
首先我编写了这个程序并且工作正常但是在新程序发生变化后我得到了错误。为什么这样?
What I have tried:
First I wrote this program and it worked fine but after the changes in the new program I got error. Why so?
public class Rectangle
{
private double length;
private double width;
public void setLength(double len)
{
length = len;
}
public void setWidth(double wid)
{
width = wid;
}
public double getLength()
{
return length;
}
public double getWidth()
{
return width;
}
public double getArea()
{
return length*width;
}
public static void main(String[] args)
{
Rectangle rect = new Rectangle();
rect.setLength(10);
rect.setWidth(10);
System.out.println("The value of length is " + rect.getLength());
System.out.println("The value of length is " + rect.getWidth());
System.out.println("The value of length is " + rect.getArea());
}
}
推荐答案
Quote:
Rectangle rect = new Rectangle();
Rectangle rect = new Rectangle();
要实例化内部类的实例 Rectagle
,您需要一个实例外部类 Demo1
(参见嵌套类( Java教程 - 学习Java语言 - 类和对象) [ ^ ])。
To instantiate an instance of the inner class Rectagle
, you need an instance of the outer class Demo1
(see Nested Classes (The Java Tutorials - Learning the Java Language - Classes and Objects)[^]).
这篇关于我在下面的问题中遇到错误。帮我跟踪错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文