可以节省很多不必要的set函数阿! 有时候结构简单的类,还是构造方法比较实用。

解决方案 »

  1.   

    构造涵数 分为 有参数constructor 和无参数constructor 
    当你要声明一个一个类的对象的时候。如果你没有定义这个类的无参数构造函数的时候,系统会自动生成一个(当然这里还有很多可以和不可以的地方)
    这个自动生成的构造函数。显然什么功能也不具备。但是能就因为这个尔说构造函数没有用吗?显然这个说法有很多的漏洞!事实上对于所有,方法体非空的构造函数而言。当你用new ClassObject() 来生成一个对象的时候,事实上,ClassObject()的构造方法 ClassObject()是一定要运行一下的。这样,你在这个构造函数中的代码,当然也要运行了!
    所以构造方法还是有用的。对于方法体为空,或是系统自动生成的构造函数,这样说也是不公平的。
    毕竟 用 A a = new A(); 虽然类A 的构造方法A() 什么也没干,但是毕竟也用来生成了一个对象a 并给他非配了内存吗!
      

  2.   

    同意 iamqqmyheart(注销||登录) “构造方法是不必要的”
    视情况而定===================================
             情人节快乐
           有情人终成眷属
       我的一分耕耘,你能给一分收获
      

  3.   

    We name it construtor because these methods construct certain things. As a matter of fact, they construct a memory space for the Object instance first of all. They also construct certain member fields' values thought the parameters passed to the constructor. However, this is not necessory all the time, and there are times we don't need the fields' values initialized, or we can use getter methods to assign value. Then, we introduced default constructor which contains no parameter at all. It's just a convenient version of constructor since it actually defeat the meaning of constructor.Constructors 是可以被继承的。使用缺省构造函数这个词汇事实上是不准确的,如果一定要说,是使用了从java.lang.Object继承下来的默认构造函数(default constructor)。Technically, an abstract Class should not contain any constructor, but it could have for not to write constructors again and again in the sub classes. but now, one has to be careful about the constructor inheritance.
    Consider the following 2 examples:
    //////////////////////////////////////////////
    class A
    {
    }
    class B extends A
    {
       public static void main(String[] args)
       {
          B b = new B();
       }
    }
    //////////////////////////////////////////////
    class A
    {
       A(int i)
       {
       }
    }
    class B extends A
    {
       public static void main(String[] args)
       {
          B b = new B();
       }
    }
    The question is whether they work? both? or neither? or the first? or the second?
      

  4.   

    The answer is the first sample works, but the second won't pass the compiling.
    If class B extends class A, class C extends class B.
    Then, in B, if no constructor is defined, C will extends all constructors from A.
    in B, if one or more constructors are defined, C will only extends the constructors defined in B, not those in A.
    **********************************************
    Another topic I would like to mention. Some people argues about the static methods defeat the meaning of Object Oriented Programming concept.因为用了静态的方法就不再是面对对象了。
    The solution is to have those methods not in static and have a constructor. The problem is the flood of the object instance if they are not destroyed or collected. Here, a private constructor comes in handy.
      

  5.   

    public class MyMath
    {
        private MyMath(){}
        private static MyMath singleInstance = new MyMath();
        public static getInstance()
        {
            return singleInstance;
        }
        /////////////////////////////////////////////////////
        public int neg(int num)
        {
            return -num;
        }
        // Other non-static methods.
    }
    ***************
    In the caller, instead of the classical MyMath.neg(34);
    one can do this:
    MyMath.getInstance().neg(34);
    好像有点多此一举,但是,逻辑更加严紧了。
      

  6.   

    编译器怎么自动生成一个不带任何参数、无任何执行语句的空构造器?
    class A
    {
       A(int i)
       {
       }
    }
    class B extends A
    {
       public static void main(String[] args)
       {
          B b = new B();
       }
    }
    编译不通过。