Java构造函数 [英] Java Constructors

查看:164
本文介绍了Java构造函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图学习如何在Java中指定类构造函数。我开始理解他们指定从该类创建的对象的实例变量的类型。它们还可以用于设置实例变量的初始值。下面的例子来自Sun的网站上的Java教程:

  public Bicycle(int startCadence,int startSpeed,int startGear){
gear = startGear;
cadence = startCadence;
speed = startSpeed;
}

你的类源代码中应该放什么构造函数? / p>

这些参数是变量的名称:(int startCadence,int startSpeed,int startGear)还是齿轮,节奏和速度变量的名称?



(int startCadence,int startSpeed,int startGear)和档位,节奏和速度有什么区别?



我的导师或盐湖社区学院的任何管理员遇到这个问题的可能性不大,让我明白我的意图。这个问题是以最大的学术诚实精神发表的。我问这个问题寻求一般的建议,并帮助理解使用Java编程语言的正确方法。我绝不使用别人的工作,把它代表为我自己的工作。我使用这里提供的答案作为我的理解的一般帮助。

解决方案

构造函数可以出现在任何地方,类的代码。然而,按照惯例,大多数人把它们放在任何其他不是构造函数的函数之前。



对于变量名,所有6实际上是变量名,是不同的。作为参数指定给构造函数(startCadence,startSpeed,startGear)的参数只能在构造函数中使用。其他3(齿轮,节奏,速度)可能是类的变量,可用于所有的方法。但是,定义不会显示在您的代码段中。完整的类将看起来像mroe:

  class Bicycle 
{
//类级变量
private int gear;
private int cadence;
private int speed;

//构造函数
public Bicycle(int startCadence,int startSpeed,int startGear){
gear = startGear;
cadence = startCadence;
speed = startSpeed;
}

//另一种方法(不是构造函数)
public void ShiftUp(){
gear = gear + 1; //注意齿轮变量在这里也可用。
}
}

希望有所帮助!


I am trying to learn how to specify class constructors in Java. I am starting to understand that they specify the types of instance variables of objects made from that class. They also can be used to set the instance variable initial values. The follow example is from the Java tutorial on Sun's website:

public Bicycle(int startCadence, int startSpeed, int startGear) {
    gear = startGear;
    cadence = startCadence;
    speed = startSpeed;
}

Where in your classes source code should you put the constructor(s)?

Are these arguments the names of the variables?: (int startCadence, int startSpeed, int startGear) or are gear, cadence and speed the names of the variables?

What is the difference between (int startCadence, int startSpeed, int startGear) and gear, cadence and speed?

In the unlikely event that my instructor or any administrator from Salt Lake Community College ever comes across this question, let me make my intentions clear. This question is posted in the greatest spirit of academic honesty. I ask this question to seek general advice and help in understanding the proper way to use the Java programming language. I in no way use the work of others and represent it as my own work. I use the answers provided here as a general aid in my understanding. I do all my own work and do not copy work provided by people answering my question.

解决方案

The constructors can appear anywhere in the code for the class. However, by convention, most people put them before any other functions that aren't constructors.

As for the variable names, all 6 are actually variable names, but the scope is differnet. The ones specified as parameters to the constructor (startCadence, startSpeed, startGear) are only available within the constructor. The other 3 (gear, cadence, speed) are probably class-wide variables, available to all methods. However the definition isn't shown in your code snippet. The full class would look mroe like:

class Bicycle
{
    // class-level variables
    private int gear;
    private int cadence;
    private int speed;

    // constructor
    public Bicycle(int startCadence, int startSpeed, int startGear) {
        gear = startGear;
        cadence = startCadence;
        speed = startSpeed;
    }

    // another method (not a constructor)
    public void ShiftUp() {
        gear = gear + 1; // notice the 'gear' variable is available here too.
    }
}

Hope that helps!

这篇关于Java构造函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆