A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 张世刚 初级黑马   /  2012-7-17 00:39  /  1765 人查看  /  5 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

本帖最后由 张世刚 于 2012-7-17 21:52 编辑

public class TestCircle1 {

        /**
         * @param args
         */
        public static void main(String[] args) {
          Circle1 circle1 = new Circle1 ();
          System.out.println("The area of the circle of radius"+circle1.radius+"is"+
          circle1.getArea());
         
          Circle1 circle2 = new Circle1 (25);
          System.out.println("The area of the circle of radius"+circle2.radius+"is"+
                          circle2.getArea());
         
          Circle1 circle3 = new Circle1 (125);
          System.out.println("The area of the circle of radius"+circle3.radius+"is"+
                          circle3.getArea());
         
          circle2.radius =100;
          System.out.println("The area of the circle of radius "+ circle2.radius+"is"+
              circle2.getArea());
          }
}
class Circle1{
          double radius;
         
          Circle1(){
                  radius = 1.0 ;
                  
          }
          Circle1(double newRadius){
                  radius = newRadius;
          }
          double getArea () {
                  return radius*radius*Math.PI;
          }
}

// 现在想把两个类组合成一个,可是就是出错
代码如下


public class TestCircle1 {

        /**
         * @param args
         */
        public static void main(String[] args) {
          Circle1 circle1 = new Circle1 ();
          System.out.println("The area of the circle of radius"+circle1.radius+"is"+
          circle1.getArea());
         
          Circle1 circle2 = new Circle1 (25);
          System.out.println("The area of the circle of radius"+circle2.radius+"is"+
                          circle2.getArea());
         
          Circle1 circle3 = new Circle1 (125);
          System.out.println("The area of the circle of radius"+circle3.radius+"is"+
                          circle3.getArea());
         
          circle2.radius =100;
          System.out.println("The area of the circle of radius "+ circle2.radius+"is"+
              circle2.getArea());
          }

          double radius;
         
          Circle1(){
                  radius = 1.0 ;
                  
          }
          Circle1(double newRadius){
                  radius = newRadius;
          }
          double getArea () {
                  return radius*radius*Math.PI;
          }
}
  




  
   

评分

参与人数 1技术分 +1 收起 理由
蒋映辉 + 1

查看全部评分

5 个回复

倒序浏览
public class TestCircle1 {

        /**
          * @param args
          */
         public static void main(String[] args) {
           Circle1 circle1 = new Circle1 ();
           System.out.println("The area of the circle of radius"+circle1.radius+"is"+
           circle1.getArea());
           
          Circle1 circle2 = new Circle1 (25);
           System.out.println("The area of the circle of radius"+circle2.radius+"is"+
                           circle2.getArea());
           
          Circle1 circle3 = new Circle1 (125);
           System.out.println("The area of the circle of radius"+circle3.radius+"is"+
                           circle3.getArea());
           
          circle2.radius =100;
           System.out.println("The area of the circle of radius "+ circle2.radius+"is"+
               circle2.getArea());
           }

          double radius;
           
          Circle1(){//楼主还没搞清楚构造函数的定义方法吧?这里只能代表一个一般函数。要想将两个类合并,你可以试一试用内部类的方法。
                   radius = 1.0 ;
                  
           }
           Circle1(double newRadius){
                   radius = newRadius;
           }
           double getArea () {
                   return radius*radius*Math.PI;
           }
}


运用内部类合并 代码如下:

package cn.itcast.day01;

public class TestCircle1 {
         
    /**
      * @param args
      */
     public static void main(String[] args) {
            Circle1 circle1 = new TestCircle1().new Circle1 ();//在这里使用了内部类。
       System.out.println("The area of the circle of radius"+circle1.radius+"is"+
       circle1.getArea());
      
      Circle1 circle2 = new TestCircle1().new Circle1 (25);
       System.out.println("The area of the circle of radius"+circle2.radius+"is"+
                       circle2.getArea());
      
      Circle1 circle3 = new TestCircle1().new Circle1 (125);
       System.out.println("The area of the circle of radius"+circle3.radius+"is"+
                       circle3.getArea());
      
      circle2.radius =100;
       System.out.println("The area of the circle of radius "+ circle2.radius+"is"+
           circle2.getArea());
       }

     class Circle1{
               double radius;
               
              Circle1(){
                       radius = 1.0 ;
                      
               }
               Circle1(double newRadius){
                       radius = newRadius;
               }
               double getArea () {
                       return radius*radius*Math.PI;
               }
     }
}

评分

参与人数 1技术分 +1 收起 理由
蒋映辉 + 1

查看全部评分

回复 使用道具 举报
很简单的嘛,你合并后的类名没改,不存在Circle1这个类了,怎么还可能有Circle1的无参和有参数构造函数?
以下是我把干扰信息去掉后的代码:
  1. public class InnerClassDemo2 {
  2.         public static void main(String[] args) {
  3.                 InnerClassDemo2 circle1 = new InnerClassDemo2 ();
  4.         System.out.println(circle1.radius);
  5.         
  6.         InnerClassDemo2 circle2 = new InnerClassDemo2 (25);
  7.         System.out.println(circle2.radius);
  8.         
  9.         InnerClassDemo2 circle3 = new InnerClassDemo2 (125);
  10.         System.out.println(circle3.radius);
  11.         
  12.         circle2.radius =100;
  13.         System.out.println(circle2.radius);
  14.         }
  15.         double radius;
  16.         InnerClassDemo2(){
  17.             radius = 1.0 ;
  18.     }
  19.         InnerClassDemo2(double newRadius){
  20.             radius = newRadius;
  21.     }
  22.     double getArea () {
  23.         return radius*radius*Math.PI;
  24.    }
  25. }
复制代码

评分

参与人数 1技术分 +1 收起 理由
蒋映辉 + 1

查看全部评分

回复 使用道具 举报
本帖最后由 黑马刘涛 于 2012-7-17 17:43 编辑
  1. public class TestCircle1 {


  2.         /**
  3.          * @param args
  4.          */
  5.         public static void main(String[] args) {
  6.                 InnerCircle1 innerCircle1 = new InnerCircle1 ();
  7.           System.out.println("The area of the circle of radius"+innerCircle1.radius+"is"+
  8.           innerCircle1.getArea());
  9.          
  10.           InnerCircle1 innerCircle2 = new InnerCircle1 (25);
  11.           System.out.println("The area of the circle of radius"+innerCircle2.radius+"is"+
  12.                                           innerCircle2.getArea());
  13.          
  14.           InnerCircle1 innerCircle3 = new InnerCircle1 (125);
  15.           System.out.println("The area of the circle of radius"+innerCircle3.radius+"is"+
  16.                                           innerCircle3.getArea());
  17.          
  18.           innerCircle2.radius =100;
  19.           System.out.println("The area of the circle of radius "+ innerCircle3.radius+"is"+
  20.                   innerCircle3.getArea());
  21.           }
  22.                         
  23.           static  class InnerCircle1 //内部类,注意加static修饰符,因为main函数为静态
  24.           {
  25.                   double radius;
  26.          
  27.                   InnerCircle1(){
  28.                                   radius = 1.0 ;
  29.                                  
  30.                   }
  31.                   InnerCircle1(double newRadius){
  32.                                   radius = newRadius;
  33.                   }
  34.                   double getArea () {
  35.                                   return radius*radius*Math.PI;
  36.                   }
  37.           }
  38.          
  39. }
复制代码
//原先定义了一个Circle1类,在编译的时候会生成Circle1.class文件,毫无疑问现在要弄成内部类。

捕获.PNG (11.02 KB, 下载次数: 16)

捕获.PNG

评分

参与人数 1技术分 +1 收起 理由
蒋映辉 + 1

查看全部评分

回复 使用道具 举报
本帖最后由 程潇 于 2012-7-17 08:11 编辑

合并类的时候,构造函数以及和改动后的引用都要跟着变化
  1. public class TestCircle1 {

  2.         /**
  3.          * @param args
  4.          */
  5.         public static void main(String[] args) {
  6.           Circle1 circle1 = new Circle1 (); //类已经合并,原来的Circle1类已经不存在,这里要将Circle1改为TestCircle1
  7.                                                          //改成 TestCircle1 circle1 = new TestCircle1()
  8.           System.out.println("The area of the circle of radius"+circle1.radius+"is"+
  9.           circle1.getArea());
  10.          
  11.           Circle1 circle2 = new Circle1 (25); //问题同上,改为 TestCircle1 circle2 = new TestCircle1(25)
  12.           System.out.println("The area of the circle of radius"+circle2.radius+"is"+
  13.                           circle2.getArea());
  14.          
  15.           Circle1 circle3 = new Circle1 (125);//问题同上,改为 TestCircle1 circle3 = new TestCircle1(125)
  16.           System.out.println("The area of the circle of radius"+circle3.radius+"is"+
  17.                           circle3.getArea());
  18.          
  19.           circle2.radius =100;
  20.           System.out.println("The area of the circle of radius "+ circle2.radius+"is"+
  21.               circle2.getArea());
  22.           }

  23.           double radius;
  24.          
  25.           Circle1(){  //类名变了,构造方法名要跟着变化,改为 TestCircle1()
  26.                   radius = 1.0 ;
  27.                   
  28.           }
  29.           Circle1(double newRadius){ //同上,改为 TestCircle1(double newRadius)
  30.                   radius = newRadius;
  31.           }
  32.           double getArea () {
  33.                   return radius*radius*Math.PI;
  34.           }
  35. }
复制代码
回复 使用道具 举报
顶咯,楼主还要把内部内温习下,内部类很重要的。
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马