你能帮我理解封装吗(初学者)?(Can you help me understand encapsulation please (Beginner)?)

编程入门 行业动态 更新时间:2024-10-27 19:19:15
你能帮我理解封装吗(初学者)?(Can you help me understand encapsulation please (Beginner)?)

我一直在使用一本迄今为止出色的Java书,但它并没有善于解释我们使用setter和getter的原因。 我已经搜索了其他帖子,但没有一个真正帮助我特别。 我只会从这本书中发布一个程序,并用它解释我的问题。

package lesson1; public class GoodDog { private int size; public void setSize(int sz){ if(sz > 10){ size = sz; } } public int getSize(){ return size; } }

第二课:

package lesson1; public class GoodDogTestDrive { public static void main(String[] args) { GoodDog one = new GoodDog(); one.setSize(15); System.out.println(one.getSize()); } }

这是我的第一篇文章,所以如果它不作为代码出来,然后原谅我。 不要问为什么这些课程被命名,我没有想象力,只是使用了这本书的名字。

无论如何,我的理解是封装(对我来说,这基本上意味着getters和setter)阻止直接访问即时变量。 唯一有效的原因是因为一些即时变量的值不应该被允许。

比如说我们有int高度; (即时变量)我们不应该说object.height = 0; ('对象'是一个随机引用变量)。 在我的小程序中,它只是一个简单的限制狗的高度。 这并不意味着有意义,但我只是想理解这个概念。 当程序员需要使用其他代码时,封装仅仅意味着编程公司。 比如说,制作一款游戏或者与众不同?

我观看了一段视频,并指出它只是简化了代码管理。 不过,我仍然不觉得我完全理解这个概念。 有人可以简单地向我解释这一点。 请注意,我是初学者,不会理解涉及中间代码的示例。

感谢您的帮助。

I've been using a Java book which has been excellent so far but it hasn't been to good at explaining the reason why we use setters and getter. I've searched other posts but none have really helped me specifically. I'll just post a program from the book and explain my problems with it.

package lesson1; public class GoodDog { private int size; public void setSize(int sz){ if(sz > 10){ size = sz; } } public int getSize(){ return size; } }

Second class:

package lesson1; public class GoodDogTestDrive { public static void main(String[] args) { GoodDog one = new GoodDog(); one.setSize(15); System.out.println(one.getSize()); } }

This is my first post so if it doesn't come out as code then forgive me. Don't ask why the classes are named that, I had no imagination and just used the book's name.

Anyway, my understanding is that encapsulation (To me this basically means getters and setters) prevents the direct access to instant variables. The only valid reason for this is because some values of instant variables should not be allowed.

For example say we had int height; (Instant variable) We should not be able to say object.height = 0; ('object' being a random reference variable). In my little program it is simply a dog's height with a simply restriction. It's not meant to make sense but I'm just trying to make sense of this concept. Is encapsulation simply meant for programming firms when programmers need to use each others code to. say, make a game for example or is it different?

I watched a video also stating that it simply makes code easier to manage. However I still don't feel that I fully understand this concept. Can someone please explain this to me in simple terms. Please note that I'm a beginner and will not understand examples involving intermediate code.

Thank you for your help.

最满意答案

如果我明白你在问什么,你可以将封装看作是利用编程语言的数据隐藏功能的做法。 面向对象的核心原则之一是对象包含数据和操作数据的方法。 封装是数据的一般内部化,因此只有对象自己的方法才能修改对象的状态。

If I understand what you're asking, you can think of encapsulation as the practice of utilizing data-hiding features of a programming language. One of the core principles of object orientation is that objects contain both data and methods to operate on that data. Encapsulation is the general internalization of the data so that only an object's own methods can modify the object's state.

更多推荐

本文发布于:2023-08-04 22:10:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1422350.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:帮我   你能   初学者   Beginner   encapsulation

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!