库如何与其他代码交互?

发布于 2025-01-16 12:05:26 字数 257 浏览 3 评论 0原文

我一直在阅读有关封装的内容,并且不断看到有关如何更改类的隐私或添加以前没有的 getter 和 setter 的评论,这些评论可能会“破坏使用您的库的人的代码”。我实在不明白这一点。我在编程方面非常缺乏经验,我的理解是,你将一个库下载到你的计算机上,它包含在你正在编写的程序的文件中,所以如果原作者在库的副本中更改了某些内容,它就不会不影响你的副本。这是错误的吗?例如,图书馆是否更像是一个网站,您的计算机通过互联网连接到该网站,并且原作者可以更新,以便他们对其所做的更改可以影响您的代码的工作方式?

I've been reading about encapsulation and keep seeing comments about how changing the privacy of a class or adding getters and setters where there were none before can 'break the code' of people who use your library. I don't really understand this. I'm very inexperienced in programming, and my understanding is that you download a library onto your computer and it's included in the files of the program you're writing, so if the original author changed something in THEIR COPY of the library, it wouldn't affect your copy. Is this wrong? For example, is a library more like a website that your computer connects to through the internet and the original author can update, so that changes they make to it can affect how your code works?

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

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

发布评论

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

评论(2

清引 2025-01-23 12:05:26

软件不断变化,因此我们必须有一种方法来跟踪不同的版本 - 因此软件版本。当您下载一个库以在自己的程序中使用时,您(通常,就像使用依赖项管理工具一样)最终会下载该库的一个非常特定的版本。

如果库作者要更改使用它的界面,那么使用该库的开发人员在下载包含这些更改的版本时也必须更改他们使用它的方式。否则,它会破坏任何遵循过时接口的代码。

只要库作者遵循正确的版本控制程序,例如在新的主要版本中包括重大更改,并且这些更改提高了库接口的清晰度而不牺牲其他属性,那么这个论点就毫无意义。开发人员可以继续使用旧版本,也可以更新代码以兼容新版本。
除了可能在资源不足的情况下,嵌入式系统可以使用所有可用的优化,例如直接访问对象/结构属性而不是通过函数。

Software is constantly changing, so we must have a way to keep track of the different versions - hence software versions. When you download a library to use in your own program, you (usually, like with a dependency management tool) end up downloading a very specific version of that library.

If a library author was to change the interface to use it, developers using that library would also have to change how they use it when they download the version with those changes. Otherwise, it would break any code that follows an outdated interface.

As long as a library author follows proper versioning procedures, for instance including breaking changes in a new major version, and the changes improve the clarity of the library's interfaces without sacrificing other properties, then the argument is moot. Developers can either continue using the old version or update their code to be compatible with the new version.
Except for maybe in low resource, embedded systems that can use all optimizations available, like accessing object/structure properties directly rather than through a function.

深海里的那抹蓝 2025-01-23 12:05:26

图书馆

根据定义,计算机使用的非易失性资源的集合
程序,通常用于软件开发。这些可能包括
配置数据、文档、帮助数据、消息模板、
预先编写的代码和子例程、类、值或类型
规格。

说明

让我花时间在编码方面进行定义:假设您有一个
创建一个足球游戏,需要什么,场地,球,球员,
标志。

所有这些我们都封装在类中以作为对象游戏
包括以上所有内容。

现在您开始构建游戏,并意识到您正在花费多余的时间来制作重复的球员姓名、球衣设计、细节填充等。

为了避免这种情况,您可以创建特定于业务的函数,例如 1. 生成 T 恤并传递(颜色、设计、布料类型),然后它会返回您的 T 恤对象。

类似地,您可以通过传递国家/地区及其 ID 来获取玩家信息,所有这些详细信息都作为 Player 对象返回,其中包含他的姓名、地点、国家/地区、联系人等。

这就是类中函数的行为方式。

你的要求

类的隐私或添加 getter 和 setter,...使用您的库的人“破坏代码”

这些是您访问对象参数或为其设置值的方式,在某些语言中,getter 和 setter 是自动生成的,不需要显式设置,除非您在类对象创建期间需要自定义设置。
获取和设置器的最大优点是它确保可以为默认类创建分配一些您不想更改默认值的值,并且不允许人们对类的特定参数强制执行新值。
这就是在定义类及其函数时控制的方式。 getter 和 setter 也是函数,类变量可以在定义内部函数逻辑时获取/设置值。

你问

我的理解是,您将一个库下载到您的计算机上,然后
它包含在您正在编写的程序的文件中,因此如果
原作者在库的副本中更改了一些内容,它
不会影响你的副本。这是错误的吗?

是的,我认为它就像 CD 副本,我向您发送了一份副本,以便您可以使用我制作的副本中的这些信息,但是一旦我在 CompactDisc(CD) 中添加了新功能和内容,它就不会出现在您的副本中在那段时间被烧毁,因此您的代码使用旧版本,并且可以使用直到需要更新为止。
只有当您使用我的新 CD 副本(称为使用我的新库版本升级您的软件)时,您才会受到影响。
通常,大型软件不会立即更改其系统中的库,除非对 1. 需求、2. 安全性 3. 旧版本中的错误在新版本中修复进行了彻底的分析。新升级需要解决的因素。

快乐编码

  • 软件世界不受您的思维限制,因此您只需编写代码即可,不要思考什么是错或对。
  • 参与数学库构建任务,使用任何 python、java、c#、objective C、swift、javascript ...
  • 使用带有圆形、方形、多边形、球体对象的模块创建库
  • 每个对象都将具有使用其参数创建的相应类(圆形样本:半径、中心(x,y)等以及 setRadius、getCircumference 等函数)
    类似地,所有对象都创建自己的类
    您使用的抽象词意味着您将某些函数设为私有,只有类可以在内部访问但在创建新的数学对象时不会暴露给外部。

希望这对您有所帮助,祝您编码愉快。

Libraries:

By definition a collection of non-volatile resources used by computer
programs, often for software development. These may include
configuration data, documentation, help data, message templates,
pre-written code and subroutines, classes, values or type
specifications.

Explanation

Let me spend time on defining in coding aspects: Lets say you have a
create a soccer game, what does that need, field, ball, players,
flags.

All this we encapsulate in Class to make as Object Game which
comprises all above.

Now you start building game and realise you are spending redudant time making repeated player names, shirt design , details filling, etc.

To avoid this you make functions which are business specific like 1. generate tshirts and pass (color, design , cloth type) and it returnes you tshirt object in return.

Similary you get player information by passing country and his ID and all this details are return as Player object which have his name, place, country, contacts ets.

This is how the functions in class behaves.

Your ask

privacy of a class or adding getters and setters, ...an 'break the code' of people who use your library

These are ways how you access the object parameters or set values for them , in some languages the getter and setters are auto generated and not required to explicitly set unless you need custom settings during class object creation.
The best advantage of the getting and setter is it ensures the default class creation can be assigned some values which you dont want to change in defaults and also not allow people to enforce new values to that specific parameter of class.
This is how the control is made in place during defining your class and its functions. getter and setter are functions as well with class variables having factility to get/set values as you define the function logic inside.

You ask

my understanding is that you download a library onto your computer and
it's included in the files of the program you're writing, so if the
original author changed something in THEIR COPY of the library, it
wouldn't affect your copy. Is this wrong?

Yes think it like a CD Copy , I sent you a copy so you can use those info from the copy i made, but once i have new features and things added in CompactDisc(CD) it wouldnt be there in your copy i burnt during that time hence you code uses the old version and may use till there is need to update.
You can only get impacted if you take my new CD copy which is called as upgrading your software with my new library version.
Normally big guys software dont immediately change the library in their systems unless there is thorough analysis done with 1. need, 2. security 3. bugs in old fixed in new. factors to address for a new upgrade.

Happy Coding

  • Software world is free of your mind to code so dont think what is wrong or right just code.
  • Take a Maths Library building task in hand use anything python, java, c#, objective C, swift, javascript ...
  • Create library with modules with Circle, Square, Polygon, Sphere objects
  • Each object they will have thier respective Classes created with theier paramters (circle sample : radius, center(x,y), etc and functions like setRadius, getCircumference, etc)
    Similar way all objects makes thier own classes
    Abstrat word you used means some function you make private that only class can internally access but not exposed to outside when you create new Maths Object.

Hope this was helpful, happy coding.

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