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.

  • 相关阅读:
    功能测试用例大全
    相对最完整的软件测试工具手册
    测试用例的评审
    黑盒测试学习笔记-(深圳文鹏)
    Llinux:ubuntu常用命令(深圳文鹏)
    HDU-4857(拓扑排序)
    HDU-3665(单源最短路)
    HDU-3661(贪心)
    HDU-2059龟兔赛跑(基础方程DP-遍历之前的所有状态)
    HDU-1047(DP-二进制状态压缩)
  • 原文地址:https://www.cnblogs.com/backpacker/p/2271544.html
Copyright © 2011-2022 走看看