一个圆锥类,底圆是一个圆的对象的引用。 为什么会有错误呢?怎么才能正确运行?
public class Yuan{
double r;
Yuan(double r)
{
this.r=r;
}
double m()
{
double m;
m=3.14*r*r;
return m;
}
}
public class YuanZ{  //要去掉public才能正确运行?为什么
Yuan t1;
double h;
yuanZ(Yuan t1,double h)//无返回类型的要写void
{
this.t1=t1;
this.h=h;
}
double zm()
{
double zm;
zm=t1.m()*h/3;
return zm;
}
}
class TestYuan
{
public static void main(String[] args)
{
Yuan t1 =new Yuan(6);
System.out.println("r="+t1.r);
System.out.println("m="+t1.m());
YuanZ t2 =new YuanZ(t1,9);   //这里应该怎么写?
System.out.println("t2 y="+t2.t1); 
System.out.println("t2 h="+t2.h);
System.out.println("t2 zm="+t2.zm());
}
}程序错误比较多,让大客们见笑了,我也找到了几个。请大家指点怎么才能正确的运行起来?
另外还有一个问题就是:当一个对象的参数需要指向另一个对象的时候,应该怎么写?该注意些什么问题:
yuanZ(Yuan t1,double h)   YuanZ t2 =new YuanZ(t1,9); 就是这几个地方,没怎么弄明白,代码的书写和内存的分配。 
谢谢大家啦

解决方案 »

  1.   

    blog 更新:
     think in java 章后练习答案.....
     http://blog.csdn.net/heimaoxiaozi/
      

  2.   

    package Jan;  class Yuan{
    double r;
    Yuan(double r)
    {
    this.r=r;
    }
    double m()
    {
    double m;
    m=3.14*r*r;
    return m;
    }
    }
     class YuanZ{  //要去掉public才能正确运行?为什么
    Yuan t1;
    double h;
    YuanZ(Yuan t1,double h)//无返回类型的要写void
    {
    this.t1=t1;
    this.h=h;
    }
    double zm()
    {
    double zm;
    zm=t1.m()*h/3;
    return zm;
    }
    }
     public  class TestYuan
    {
    public static void main(String[] args)
    {
    Yuan t1 =new Yuan(6);
    System.out.println("r="+t1.r);
    System.out.println("m="+t1.m());
    YuanZ t2 =new YuanZ(t1,9);   //这里应该怎么写?
    System.out.println("t2 y="+t2.t1); 
    System.out.println("t2 h="+t2.h);
    System.out.println("t2 zm="+t2.zm());
    }
    }
      

  3.   

    一个.java文件只能有一个public类
      

  4.   

    public class YuanZ{ //要去掉public才能正确运行?为什么 
    一个文件只能有1个公有类 而且必须和文件名完全相同yuanZ(Yuan t1,double h)//无返回类型的要写void
    这个是构造函数啊 不可以写void int 之类的东东 一般都是public 或不写后边太乱 错误很多 有点迷糊你要做什么??Yuan t1; 这个定义的是什么???? 就是这几个地方,没怎么弄明白,代码的书写和内存的分配。
    别管内存 当一个对象的参数需要指向另一个对象的时候,应该怎么写?
    我也是初学者 我只会引用指向对象......  对象可以指向对象吗??  为啥不把参数赋给另一个对象 ,  实例化就可以了