简体   繁体   English

如何在Java和Android中使用构造函数?

[英]How to use constructors in java, android?

I have a short question about the following code from 我对以下代码有一个简短的问题

http://www.androidhive.info/2013/09/android-sqlite-database-with-multiple-tables/ http://www.androidhive.info/2013/09/android-sqlite-database-with-multiple-tables/

Here are used two Constructors, one with the id, and the other without - I don't understand why. 这里使用了两个构造函数,一个带有id,另一个没有-我不明白为什么。 What's the benefit? 有什么好处?

I already read this thread: 我已经读过这个线程:

Why does this class have two constructors? 为什么此类有两个构造函数?

The answer I could understand is, that I can create a Tag with id and not, but I'm trying to understand, how to know which constructor it shall use? 我能理解的答案是,我可以创建一个不带id的Tag,但我试图理解,如何知道它将使用哪个构造函数? Is it just by the number of parameters? 仅仅是根据参数的数量?

    public class Tag {

    int id;
    String tag_name;

    // constructors
    public Tag() {

    }

    public Tag(String tag_name) {
        this.tag_name = tag_name;
    }

    public Tag(int id, String tag_name) {
        this.id = id;
        this.tag_name = tag_name;
    }

    // ...     
}

Yes, only by its amount of parameters. 是的,仅通过其参数数量即可。

It's called "overloading" of functions. 这称为功能的“重载”。 You can overload a function by providing the same signature with different parameters (according to their type and order). 您可以通过为相同的签名提供不同的参数(根据其类型和顺序)来使函数过载。

The JVM will then decide which method to use in a certain situation. 然后,JVM将决定在特定情况下使用哪种方法。

Please note: If you provide a constructor the JVM won't provide a default constructor any more. 请注意:如果提供构造函数,JVM将不再提供默认构造函数。

class Foo{

private int x;
private String name;

    Foo(int x){      //constructor 1
        this(x, "Default Name");
    }
    Foo(String name){  //constructor 2
        this(0, name);
    }
    Foo(int x, String name){  //constructor 3
        this.x = x;
        this.name = name;
    }
}

Foo f1 = new Foo(9); //calls constructor 1, who will call constructor 3
                     //f1.x = 9, f1.name = "Default Name"
Foo f2 = new Foo("Test"); // calls constructor 2, who will call constructor 3
                          // f2.x = 0; f2.name = "Test"
Foo f3 = new Foo(3, "John"); //calls constructor 3
                             // f3.x = 3; f3.name = "John"

Foo f4 = new Foo()  // This won't work! No default Constructor provided!

**which constructor it shall use? **应使用哪个构造函数? only of its amount of paremeters? 仅其参数的数量? ** Yes, for example If you call **是,例如,如果您致电

Tag newTag = new Tag();

it will call 它会打电话

 public Tag() {

}

but if you call 但是如果你打电话

 Tag newTag = new Tag("Name");

it will call 它会打电话

 public Tag(String tag_name) {

}

and so on 等等

by how many arguments you pass to the constructor that way it will know which one to call 通过向构造函数传递多少个参数,它将知道要调用哪个参数

The compiler knows "which constructor it shall use" by a rule, in the Java Language Specification . Java语言规范中 ,编译器通过规则知道“它将使用哪个构造函数”。

A resume: Is by signature of the method (the type and order of arguments --- the exceptions dont effect the signature, and the return type too). 简历:通过方法的签名(参数的类型和顺序---异常不会影响签名,返回类型也是如此)。 This is not restricted only by the constructor, but any method, properly Overloaded; 这不仅受到构造函数的限制,而且还受到适当重载的任何方法的限制; You can study these by topic of "Overloading". 您可以按“重载”主题研究这些内容。 The reason to Overload a method --- or a constructor too ---, is to give more flexibility. 重载方法或构造函数的原因是为了提供更大的灵活性。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM