Java 构造函数 [英] Java Constructors

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

问题描述

我正在尝试学习如何在 Java 中指定类构造函数.我开始明白他们指定了由该类创建的对象的实例变量的类型.它们还可用于设置实例变量的初始值.以下示例来自 Sun 网站上的 Java 教程:

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)?

这些参数是变量的名称吗?: (int startCadence, int startSpeed, int startGear) 还是 gear、cadence 和 speed 是变量的名称?

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?

(int startCadence, int startSpeed, int startGear)和gear、cadence、speed有什么区别?

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

万一我的老师或盐湖社区学院的任何管理员遇到这个问题,让我明确表达我的意图.这个问题是本着最大的学术诚信精神发布的.我问这个问题是为了寻求一般建议并帮助理解使用 Java 编程语言的正确方法.我绝不会使用他人的作品并将其呈现为我自己的作品.我使用这里提供的答案作为我理解的一般帮助.我做我自己的所有工作,不会复制回答我问题的人提供的工作.

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.

至于变量名,6个其实都是变量名,只是作用域不同.指定为构造函数参数的那些(startCadence、startSpeed、startGear)仅在构造函数中可用.其他 3 个(齿轮、节奏、速度)可能是类范围的变量,可用于所有方法.但是,定义未显示在您的代码片段中.完整的课程看起来像:

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

希望有帮助!

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

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