Java构造函数

我正在尝试学习如何在Java中指定类构造函数。 我开始明白,他们指定了这个类所创build的对象的实例variables的types。 它们也可以用来设置实例variables的初始值。 以下示例来自Sun网站上的Java教程:

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

在你的类源代码中,你应该把构造函数放在哪里?

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

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

万一我的教官或盐湖社区学院的任何一位pipe理员遇到过这个问题,让我明确我的意图。 这个问题张贴在学术诚实的最大精神。 我问这个问题寻求一般的build议,并帮助理解使用Java编程语言的正确方法。 我决不会利用别人的工作,把它当作我自己的工作。 我使用这里提供的答案作为我理解的一般帮助。 我自己做所有的工作,不要复制回答我的问题的人提供的工作。

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

至于variables名,所有6个实际上是variables名,但范围是不同的。 指定为构造函数(startCadence,startSpeed,startGear)的参数只能在构造函数中使用。 其他3(齿轮,节奏,速度)可能是全classvariables,可用于所有方法。 但是这个定义并没有显示在你的代码片段中。 全class会看起来像:

 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. } } 

希望有所帮助!

齿轮,节奏和速度是类的成员variables(在别处声明),startCadence,startSpeed和startGear是函数参数。

 class Bicycle { private int gear, cadence, speed; public Bicycle(int startCadence, int startSpeed, int startGear) { // set the value of member variables from passed parameters gear = startGear; cadence = startCadence; speed = startSpeed; } } 

在你的类源代码中,你应该把构造函数放在哪里?

我使用以下内容:

 package statement .... import statements.... public class MyClass { // instance attributes private int i; // class attribute private static int MAX; // static methods public static int getClassCount() { } // Constructor!! public MyClass() { // constructor. } // public methods // protected methods // private methods // public static void main } 

但他们可以去任何地方。 我觉得这是更好的哟把事情的可见性的顺序。 例如,我宁愿在私有方法之前使用公有方法(所以如果我正在寻找一个特定的公共方法,我知道它在文件的顶部)。出于同样的原因,我通常把构造函数放在顶部。

这些参数是variables的名字吗?

没有必要,您可以根据需要命名它们。 我通常使用相同的名字。

…或是齿轮,节奏和速度variables的名字?

它们是实例variables名称

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

第一个是构造函数的参数名称,前者是它自己的对象的属性名称。

拿这个样本:

  public class Person { private String name; // a person has a name. public Person( String nameParameter ) { this.name = nameParameter; } public String toString() { return "My name is : " + this.name; } public static void main( String [] args ) { // creates a new "instance" and assign "Patrick" as its name. Person one = new Person("Patrick"); System.out.println( one ); // prints "My name is: Patrick" // each person have its own name. Person two = new Person("Oscar"); System.out.println( two ); // prints "My name is: Oscar" } } 

正如你所看到的,当你将一个值传递给构造函数时,你使用的是一个参数,当你看到构造函数代码时,你会看到参数名(接收该参数),然后将其分配给实例属性。

我通常把我的构造函数放在我的文件的顶部,包,导入,Javadoc和静态/实例variables声明部分之后。

齿轮,节奏和速度是类variables,大概是在构造函数的某个地方定义的。 startCadence,startSpeed和startGear也是variables,但它们是传递给构造函数的参数。

你也可以看到这样的东西:

 public Bicycle(int cadence, int speed, int gear) { this.gear = gear; this.cadence = cadence; this.speed = speed; } 

它从相同名称的参数设置类variables。

  1. 这完全取决于你。 我通常从所有variables开始,然后是构造函数,然后是方法,但这只是个人偏好。
  2. 参数的名称是完全不相关的,只要你不把它们命名为你的variables。 在这个例子中, gearcadencespeed是variables。
  3. 您(或某人)将三个ints传递给构造函数。 名称( startCadencestartSpeedstartGear被称为forms参数 ,它们是您可以识别参数的方式。请参阅http://en.wikipedia.org/wiki/Parameter_(computer_science); 。
    gearcadencespeed在class上的其他地方定义。 类中的任何方法都可以引用它们。

别担心 – 如果你在这方面工作,这种事情很快就会成为第二天性。

哦,我可以build议你得到一个好的IDE? BlueJ应该对初学者有好处, NetBeans和Eclipse对于更有经验的程序员来说是好的。 源代码高亮可以是非常宝贵的。

你真的需要Head First Java的副本

实际上,构造函数的参数不必存储为对象的成员variables。 这是一个例子:

 class NumberAsString { private String numAsStr; public NumberAsString(double number) { this.numAsStr = Double.toString(number); } } 

在这个例子中,构造函数的参数实际上并不存储在任何地方,但是它的值是计算一个或多个成员variables值所必需的。

你所看到的行为,其中所有参数直接存储为成员variables,这是很常见的。 特别是对于为其成员variables(不提供任何计算或转换function)提供“getter”和“setter”方法的某些种类的类。 在Java世界中,这些类通常被称为“bean”。 (是的,这是一个非常愚蠢的名字。)

int startCadence和节奏之间的基本区别不在variables中,而在其范围内。 如果一个variables是在一个方法内部定义的,比如一个构造函数,它将只存在于这个方法内部,而不是外部。 如果一个variables是在一个类中定义的,它将会存在于该类中的任何地方,这样的variables就具有全局范围。 variablesstartCadence只存在于构造函数中,所以如果你想在其他地方使用它的值,可以把它传递给另一个全局variables。 这是这里发生的事情:节奏= startCadence;

1)构造函数的位置丝毫不重要。 按照惯例,我个人把它放在如下:

 public class Bicycle { public int gear, cadence speed; public Bicycle(int startCadence, int startSpeed, int startGear) { gear = startGear; cadence = startCadence; speed = startSpeed; } public void otherFunction1() {} public void otherFunction2() {} public void otherFunction3() {} } 

2)齿轮,节奏和速度是类的成员variables; 他们属于每辆自行车,并且是每辆自行车的不同variables。 然而,startCadence,startSpeed和startGear是局部variables,它们只属于该函数。 当我要求一个

 new Bicycle(10, 15, 5); 

startCadence设置为10,startSpeed设置为15,startGear设置为5.然后,构造函数将成员variables初始化为它们的“start”对应variables。

(int startCadence, int startSpeed, int startGear)参数是构造函数参数,它们将设置Bicycle字段的cadencespeedgear 。 它们只在构造函数中可用。 gearcadencespeed是每个自行车实例唯一的实例variables,可以从其他方法引用。 构造函数参数允许您为对象的初始化提供参数。 在这个例子中,你可以创build一个齿轮为1,节奏为0,速度为0的自行车对象,如下所示:

 Bicycle bike = new Bicycle(0, 0, 1); 

或者你可以创build一个移动的自行车,节奏是60转/分钟,速度是10英里/小时,如下所示:

 Bicycle bike = new Bicycle(60, 10, 3); 

构造函数的放置是不相关的,但通常构造函数被放置在类定义的开始。

 public class Account { private static final Exception InsufficientFundsException = null; private int accountNumber; private double availableBalance; private int pin; public Account(int account, int pin,double amount ) { this.accountNumber = account; this.pin = pin; this.availableBalance = amount; } public void credit(double amount) { this.availableBalance= this.availableBalance+amount; } public void debit(double amount) throws Exception { if(this.availableBalance>=amount) { this.availableBalance= this.availableBalance-amount; } else { throw new InsufficientFundsException(); } } public int getAccountNumber() { return accountNumber; } public double getAvailableBalance() { return availableBalance; } public String toString() { return "Account="+accountNumber+"pin="+pin+"balance"+availableBalance; } public boolean validatePIN(int pin) { if(this.pin==pin) return true; else return false; } }