构造函数(java)

发布于 2024-09-25 18:03:59 字数 283 浏览 0 评论 0原文

好吧,我有一个作业,我必须创建一个具有一组私有属性的类,我已经完成了。这变得很棘手,因为我对 java 编程(或一般编程)相当陌生,并且在封装方面不太熟练。我以前没有使用过 getter/setter 或构造函数。我了解 getter/setter 方法,但尚未了解构造函数的任何在线信息,甚至不了解其目的。

我必须为每个属性创建 getter/setter 和构造函数,因此我似乎应该学习使用这些方法。如果有人能给我一个带有构造函数的类的示例,并解释构造函数在做什么,以及为什么我要使用它,那就太好了。

提前谢谢你了。

Okay so I have an assignment where I have to create a class with a set of private properties, which I have done. It gets tricky because I'm fairly new to java programming (or programming in general) and am not very practiced in encapsulation. I have not used getters/setters or constructors before. I understand the getter/setter methods, but have yet to understand any of the information online for constructors, or even understand their purpose.

I have to create getters/setters and constructors for each of the properties so it would seem I should learn to use these methods. If anyone could give me an example of a class with a constructor and explain what the constructor is doing, and why I would use it it would be nice.

Thank you ahead of time.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

咋地 2024-10-02 18:03:59

首先,欢迎来到编程世界:)

让我们看一个简单的例子。

假设您想编写一个类来表示一个 Square。创建 Square 对象时,必须赋予 Square 某些属性,例如每条边的长度。这就是构造函数发挥作用的地方。

类布局如下所示,请注意 length_of_each_side 变量未分配值。

public class Square {
     int length_of_each_side;

     public Square() {
     ...
     }

     public Square(int length) {
     ...
     }
}

如果您不指定正方形的大小,您可以使用默认构造函数为您提供预定义的正方形。

public Square() {
     length_of_each_side = 1;
}

或者你可以允许某人指定正方形的大小

public Square(int length) {
     length_of_each_side = length;
}

如果我想创建一个预定义的正方形(长度1),它看起来像这样

Square mySquare = new Square();

如果我想创建一个正方形并指定长度55,它看起来像这

Square mySquare = new Square(55);

First off, welcome to the programming world :)

Lets look at a simple example.

Say you want to program a class to represent a Square. When you create a Square object, you have to give the Square certain properties, such as the length of each side. This is where constructors come into play.

The class layout looks like this, note the length_of_each_side variable is not assigned a value.

public class Square {
     int length_of_each_side;

     public Square() {
     ...
     }

     public Square(int length) {
     ...
     }
}

You can have a default constructor that will give you a predefined Square if you do not specify the size of the square.

public Square() {
     length_of_each_side = 1;
}

or you can allow someone to specify the size of the square

public Square(int length) {
     length_of_each_side = length;
}

If I want to create a pre-defined Square (length 1), it would look like this

Square mySquare = new Square();

If I want to create a Square and specify the length of 55, it would look like this

Square mySquare = new Square(55);
心欲静而疯不止 2024-10-02 18:03:59

我不确定你在学校里如何看待这个问题,或者你的老师告诉你什么,但只是一个建议——不要将使用 getter 和 setter 作为习惯问题。它们在 Java 中不是“自动”的,这是有原因的,因为它们确实不应该存在。

当你有一个对象时,其想法是要求该对象为你做一些事情——换句话说,你应该提供为你作用于变量的方法,而不是获取变量并作用于它然后将其放回。

很多时候你无法避免吸气剂——但只有当你确定需要它们时才应该添加它们,而不是作为习惯问题。

构造函数是设置所有成员变量的最佳位置。事实上,如果你将变量设置为final(这是一个非常好的习惯,使类“不可变”),构造函数将是你可以分配它们的唯一地方。

创建对象时保证会调用构造函数,因此这是设置对象的好地方。在构造函数中,如果确保对象处于有效状态并且拥有最终成员变量,则不可能使对象进入无效状态——这是一种非常方便的做法,可以节省大量调试时间。

I'm not sure just how you are looking at this in school or what your teacher is telling you, but just a suggestion--do not use getters and setters as a matter of habit. There is a reason they are not "automatic" in Java, it's because they really shouldn't be there.

When you have an object, the idea is to ask the object to do something for you--in other words, you should provide methods that act on the variables for you rather than getting the variable and acting on it and putting it back.

There are a lot of times you can't avoid getters--but they should only be added when you are sure you need them, not as a matter of habit.

Your constructor is the BEST place to set all your member variables. In fact, if you make variables final (a very good habit to get into, makes the class "immutable") the constructor will be the only place you can assign them.

Your constructor is guaranteed to be called when your object is created, so it's a good place to set your object up. In your constructor if you ensure the object is in a valid state AND you have final member variables, it's impossible to get your object into an invalid state--this is a really handy practice and can save you a lot of debugging time.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文