在主类中实例化时,构造函数的正确用法是什么?

abd*_*dev 5 java constructor

我的老师编写和组织代码的方式与我的补充教科书中提出的方式非常不同.在最近的一个实验中,我们需要使用该wheels库在一个窗口中显示多个对象.这是Snowman由我的老师编写的类(为方便起见,未包含其他对象的代码):

public class Snowman {
    private Ellipse _top;
    private Ellipse _middle;
    private Ellipse _bottom;

public Snowman() {
        _top = new Ellipse();
        _top.setColor(Color.WHITE);
        _top.setFrameColor(Color.BLACK);
        _top.setFrameThickness(1);
        _top.setSize(80, 80);
        _middle = new Ellipse();
        _middle.setColor(Color.WHITE);
        _middle.setFrameColor(Color.BLACK);
        _middle.setFrameThickness(1);
        _middle.setSize(120, 120);
        _bottom = new Ellipse();
        _bottom.setColor(Color.WHITE);
        _bottom.setFrameColor(Color.BLACK);
        _bottom.setFrameThickness(1);
        _bottom.setSize(160, 160);
    }
    public void setLocation(int x, int y) {
        _top.setLocation(x + 40, y - 170);
        _middle.setLocation(x + 20, y - 100);
        _bottom.setLocation(x, y);
    }
}    
Run Code Online (Sandbox Code Playgroud)

此对象稍后在SnowmanCartoon类中实例化:

public class SnowmanCartoon extends Frame{
    private Snowman _snowman;
    private Eyes _eyes;
    private Hat _hat;
    private Bubble _bubble;

    public SnowmanCartoon() {
        _snowman = new Snowman();
        _snowman.setLocation(100, 300);
        _eyes = new Eyes();
        _eyes.setLocation(165, 150);        
        _hat = new Hat();
        _hat.setLocation(152, 98);
        _bubble = new Bubble();
        _bubble.setLocation(280, 60);
    }
    public static void main(String[] args) {
        new SnowmanCartoon();
    }
}
Run Code Online (Sandbox Code Playgroud)

以下是我的担忧:

  1. 在这两个类中,为什么有一个与类同名的方法,它的目的是什么?

  2. 为什么方法setLocation()是一个void方法而方法Snowman()不是,即使Snowman()没有返回任何东西?

  3. SnowmanCartoon类,当它说private Snowman _snowman;_snowman = new Snowman();,是SnowmanSnowman阶级,或者说Snowman()方法?

  4. 如果Snowman对象的实例化是指Snowman()设置其所有属性的方法,为什么我不需要使用点运算符:Snowman.Snowman()

  5. 在我的教科书中,实例变量和方法在一个类中声明,并在另一个类中实例化.但是,它们似乎同时出现在我老师的代码中.

riz*_*lp1 8

名为class的方法称为类的"构造函数".它用于实例化对象.

例:

public class MyClass { // class
    public MyClass() { // constructor

    }
}
Run Code Online (Sandbox Code Playgroud)

请注意,构造函数与任何其他方法一样,但是它没有返回类型.它基本上返回"this"对象.

你打电话的时候

MyClass a = new MyClass(); 
Run Code Online (Sandbox Code Playgroud)

这将实际调用构造函数并创建对象.

请注意,通过使用不同数量和类型的参数,您可以在类中拥有多个构造函数.

在java中,如果不包含任何构造函数,则默认情况下任何类都将具有默认的无参数构造函数.

public MyClass() {
}
Run Code Online (Sandbox Code Playgroud)

在线构建者可获得大量信息.它们是开始学习面向对象编程的基本概念.这里有更多信息.

--EDIT--回答有关如何致电特定问题的问题.

拿这两个构造函数

public MyClass(){
// do things
}

public MyClass(String p){
// do things
}
Run Code Online (Sandbox Code Playgroud)

然后在实例化对象时

MyClass c = new MyClass(); // use constructor 1
MyClass d = new MyClass("value"); // use constructor 2
Run Code Online (Sandbox Code Playgroud)


kos*_*osa 6

public Snowman() {...}被称为构造函数.在为此类创建对象时将调用此方法.如果你没有定义构造函数,jvm会隐式为你创建一个.请阅读此javadoc以了解有关构造函数的更多信息.

Snowman()不是方法,它是构造函数.构造函数没有返回类型.如果添加返回类型,则将其视为方法.