zoukankan      html  css  js  c++  java
  • Constructor – why do I need u?

    Let’s write a java file first. File name is Gouzaohanshu.java
    notice: the main() must be in the class name which is same as the file name, i.e. Gouzaohanshu
    ============================
    package ch.allenstudy.newway01;

    class Gouzao{
    private int i,j;
    public void set(int a, int b)
    {
    i = a;
    j = b;
    }
    public void show()
    {
    System.out.printf(“i=%d, j=%d\n”, i, j);
    }
    }

    public class Gouzaohanshu
    {
    public static void main(String[] args)
    {
    Gouzao gz = new Gouzao();
    gz.set(3, 55);
    gz.show();
    }
    }
    ========================
    You see, we can assign value to i and j with gz.set() method.
    While, how if I

    Constructor, if translated into Chinese, its “构造函数”. But this translation is very bad. It looks like the constructor is a kind of method. But, in fact, Constructor is NOT method. The accurate translation should be “构造器”.
    The different between Constructor and Method is:
    Once you new an object, you will call the Constructor directly.
    But for mehtod, after you new an object, only after you call it, it will be invoked.

    Let’s see a sample.

    Below java file (Gouzaohanshu.java) is very simple.
    ====================
    package ch.allenstudy.newway01;

    class Gouzao{
    private int i,j;
    public Gouzao()
    {
    System.out.println(“Hello Constructor!”);
    }

    }

    public class Gouzaohanshu
    {
    public static void main(String[] args)
    {
    Gouzao gz = new Gouzao();
    }
    }
    ======================
    Please notice the bold part. It is a constructor.
    In class Gouzaohanshu, we don’t call any method, just a new an object. Whhat’s the result? As below:
    —————–
    Hello Constructor!
    —————–
    You can see, the code in public Gouzao() is exectued.

    Now you understand that, for a Constructor, once the object is generated, they are called.

    While, what use of this?
    Now, if with the method way, if we want to assign values to i and j, we have to new the object, and then call the gz.set() method.
    If we can assign values to i and j the same time of NEWing the object, that will be perfect.
    But how to reach this with only using the way like Gouzao gz = new Gouzao(1,2)?
    Change the code to:
    —————————-
    package ch.allenstudy.newway01;

    class Gouzao {
    private int i, j;

    public Gouzao(int a, int b) {
    i = a;
    j = b;
    System.out.printf(“Now i = %d, j = %d\n”, a, b);
    }

    }

    public class Gouzaohanshu {
    public static void main(String[] args) {
    Gouzao gz = new Gouzao(2,3);
    }
    }
    —————————
    Result:
    Now i = 2, j = 3

    You see, we have reach the object — assign values to variables directly with Constructor.

    Overall, why do we need constructor?
    Answer: we need it to assign values to variables directly.

    ————————
    BTw, when we write Gouzao gz = new Gouzao(2,3), this means that: you are creating a new object gz based on class Gouzao, and at the same time, you are transferring 2 values (“2″ and “3″) to the constructor.

  • 相关阅读:
    COCOS学习笔记--单点触控
    [ExtJS5学习笔记]第九节 Extjs5的mvc与mvvm框架结构简单介绍
    STM32F103 TIM1输出PWM设置
    【转】Android--UI之ProgressBar--不错
    【传】玩转Android---UI篇---ImageButton(带图标的按钮)
    【转】Android--多线程之Handler--不错
    【转】MFC获取程序目录路径方法
    【转】蓝牙4.0BLE cc2540 usb-dongle的 SmartRF Packet Sniffer 抓取数据方法--不错
    【转】windows常用消息大全(系统消息、通告消息、用户消息)
    【转】VC的MFC中重绘函数的使用总结(整理)
  • 原文地址:https://www.cnblogs.com/backpacker/p/2271544.html
Copyright © 2011-2022 走看看