首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > JAVA > J2SE开发 >

简单程序,大哥们帮小弟我调试下,生成小对象

2012-01-22 
简单程序,大哥们帮我调试下,生成小对象Java codepublic class d{int aaa 3public static void main(Str

简单程序,大哥们帮我调试下,生成小对象

Java code
public class d{    int aaa = 3;    public static void main(String args[])    {       Cylinder obj = new Cylinder();       obj.area();               }    private class Cylinder{    double radius;    int height;    double pi=3.14;    void area()    {        System.out.println("圆柱体面积="+(pi*radius*radius) * aaa);    }    void volume()    {        System.out.println("圆柱体体积="+(((pi*radius*radius))*height));    }}} 


[解决办法]
给你修改了下
Java code
package ceshi;public class d{    int aaa = 3;    public static void main(String args[])    {       Cylinder obj = new Cylinder();       obj.area();               }} class Cylinder {    double radius;    int height;    double pi = 3.14;    void area() {        double aaa = 0;         //你没有定义aaa  需要自己定义并给所需要的值        System.out.println("圆柱体面积=" + (pi * radius * radius) * aaa);    }    void volume() {        System.out.println("圆柱体体积=" + (((pi * radius * radius)) * height));    }}
[解决办法]
public class d
{
int aaa = 3;
public static void main(String args[])
{
Cylinder obj = new Cylinder();
obj.area(aaa);

}

public class Cylinder
{
double radius;
int height;
double pi=3.14;
void area(int aaa)
{
System.out.println("圆柱体面积="+(pi*radius*radius) * aaa);
}
void volume()
{
System.out.println("圆柱体体积="+(((pi*radius*radius))*height));
}
}


[解决办法]
Java code
public class Test{        public static void main(String args[])    {       Cylinder obj = new Cylinder();       obj.area(10.0,5);               }}class Cylinder{        double pi=3.14;    void area(double radius,int height)         {        //不过你的圆柱体表面积计算公式好像有问题吧,应该是(2π*R*R+2π*R*H)        System.out.println("圆柱体面积="+(pi*radius*radius) );    }    void volume(double radius,int height)    {        System.out.println("圆柱体体积="+(((pi*radius*radius))*height));    }} class Cylinder前不加private
[解决办法]
没仔细看 看样子应该是2楼那样 传一个aaa 过去 2楼正解
[解决办法]
aaa 应该是作为参数传过去的。同意2楼。
[解决办法]
看迷糊了
[解决办法]
引用楼主 sure2003 的帖子:
Java codepublic class d
{
int aaa = 3;
public static void main(String args[])
{
Cylinder obj = new Cylinder();
obj.area(aaa);

}

private class Cylinder
{
double radius;
int height;
double pi=3.14;
void area(int aaa)
{
System.out.println("圆柱体面积="+(pi*radius*radius) * aaa); //aaa在Cylinder中没定义,可以用参数传进去
}
void volume()
{


[解决办法]
2楼的 obj.area(aaa); 是不是有问题? 我测试了一下提示不能对静态字段进行静态引用。

小弟愚钝请指教

[解决办法]
用传值的应该比较好吧
------解决方案--------------------


只需要改一处就没问题了...楼上那些贴代码的麻烦先看好LZ的程序再把自己的东西运行下再贴...
首先,LZ的代码中private class Cylinder只是个内部类,所以那些搞成了2个类的贴出来的代码我就不说啥了~
其次,就拿2楼的代码说下,那个内部类你Cylinder obj = new Cylinder(); 这样都能new出来?
在LZ的代码里加了一点东西,应该可以满足LZ的要求了,问题就在这里创建内部类实例上,其实大家都知道,咋就都不好好看看捏?

Java code
public class d {    int aaa = 3;    public static void main(String args[]) {        Cylinder obj = [color=#FF0000]new d().new Cylinder();[/color]        obj.area();    }    private class Cylinder {        Cylinder() {        }                //这里把radius 和height 给你赋了2个初值,要不你下面(pi * radius * radius) * aaa)这里计算就                  //肯定为0了,int和double 默认初值为零        double radius = 1;        int height = 1;        double pi = 3.14;        void area() {            System.out.println("圆柱体面积=" + (pi * radius * radius) * aaa);        }        void volume() {            System.out.println("圆柱体体积=" + (((pi * radius * radius)) * height));        }    }}
[解决办法]
怀疑加颜色的坏了,怎么又没加上还把颜色代码弄出来了,就这句,再试下
Cylinder obj = new d().new Cylinder();
[解决办法]
12楼 正解
[解决办法]
传值问题!

热点排行
Bad Request.