如何使用类? [英] How do I use classes?

查看:98
本文介绍了如何使用类?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是编程的新手,有一件事我很困惑。什么是类,我如何使用它?我理解了一点,但我似乎找不到一个完整的答案。

I'm fairly new to programming, and there's one thing I'm confused by. What is a class, and how do I use one? I understand a little bit, but I can't seem to find a full answer.

顺便说一下,如果这是语言特定的,那么我编程PHP。

By the way, if this is language-specific, then I'm programming in PHP.

编辑:还有别的东西我忘了说。具体来说,我想问一下如何在类中使用定义函数。我看过PHP代码中的函数在类中定义的例子,但我不能真正理解为什么。

There's something else I forgot to say. Specifically, I meant to ask how defining functions are used in classes. I've seen examples of PHP code where functions are defined inside classes, but I can't really understand why.

推荐答案

尽可能简洁:类描述了一组可以对自身执行操作的数据。

To be as succinct as possible: a class describes a collection of data that can perform actions on itself.

例如,您可能有一个表示图像的类。这个类的一个对象将包含描述图像所需的所有数据,然后还将包含旋转,调整大小,裁剪等方法。它还可以使用方法来询问对象有关它自己的属性,像getColorPalette或getWidth。这相对于能够直接访问原始(非对象)数据集合中的颜色调色板或宽度 - 通过使数据访问通过类方法,对象可以实施保持一致性的约束(例如,您不应该能够以改变宽度变量,而实际上不将图像数据改变为该宽度)。

For example, you might have a class that represents an image. An object of this class would contain all of the data necessary to describe the image, and then would also contain methods like rotate, resize, crop, etc. It would also have methods that you could use to ask the object about its own properties, like getColorPalette, or getWidth. This as opposed to being able to directly access the color pallette or width in a raw (non-object) data collection - by having data access go through class methods, the object can enforce constraints that maintain consistency (e.g. you shouldn't be able to change the width variable without actually changing the image data to be that width).

这是面向对象编程与过程编程的不同之处。在过程编程中,您有数据并且有函数。函数对数据起作用,但是没有数据的所有权,并且数据和使用它的函数之间没有根本的联系。

This is how object-oriented programming differs from procedural programming. In procedural programming, you have data and you have functions. The functions act on data, but there's no "ownership" of the data, and no fundamental connection between the data and the functions which make use of it.

面向对象的编程,你有对象,它们是与动作组合的数据。每种类型的数据都有一组定义的可以对自身执行的动作,以及一组定义的属性,它允许函数和其他对象以定义的,约束相关的方式读写。

In object-oriented programming, you have objects which are data in combination with actions. Each type of data has a defined set of actions that it can perform on itself, and a defined set of properties that it allows functions and other objects to read and write in a defined, constraint-respecting manner.

重点是将程序的各部分相互分离。使用Image类,可以确信所有处理图像数据的代码都在Image类的方法中。你可以确保没有其他代码将以意想不到的方式陷入你的图像的内部。另一方面,图像类之外的代码可以知道有一种定义的方式来操纵图像(调整大小,裁剪,旋转方法等),而不必担心图像数据是如何存储的,或者图像如何函数被实现。

The point is to decouple parts of the program from each other. With an Image class, you can be assured that all of the code that manipulates the image data is within the Image class's methods. You can be sure that no other code is going to be mucking about with the internals of your images in unexpected ways. On the other hand, code outside your image class can know that there is a defined way to manipulate images (resize, crop, rotate methods, etc), and not have to worry about exactly how the image data is stored, or how the image functions are implemented.

编辑:另一个有时很难掌握的东西是术语类和对象之间的关系。 类是如何创建特定类型的对象的描述。 Image类将描述存储图像数据所需的变量,并给出所有Image方法的实现代码。称为图像类的实例的图像对象是该描述用于存储一些实际数据的特定用途。例如,如果你有五个图像来表示,你会有五个不同的图像对象,所有相同的图像类。

And one more thing that is sometimes hard to grasp is the relationship between the terms "class" and "object". A "class" is a description of how to create a particular type of "object". An Image class would describe what variables are necessary to store image data, and give the implementation code for all of the Image methods. An Image object, called an "instance" of an image class, is a particular use of that description to store some actual data. For example, if you have five images to represent, you would have five different image "objects", all of the same Image "class".

这篇关于如何使用类?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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