How do I use classes?

2019-03-21 16:05发布

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.

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

Edit: 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.

标签: class
10条回答
欢心
2楼-- · 2019-03-21 16:21

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

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.

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.

Edit: 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".

查看更多
Anthone
3楼-- · 2019-03-21 16:25

Classes are a way programmers mark their territory on code.
They are supposedly necessary for writing big projects.
Linus and his team must have missed that memo developing the linux kernel.

However, they can be good for organization and categorizing code I guess. It makes it easier to navigate code in an ide such as visual studio with the object browsers.

Here are some usage demonstrations of classes in 31 languages on rosettacode

查看更多
男人必须洒脱
4楼-- · 2019-03-21 16:25

Classes is a term used in the object oriented programming (OOP) paradigm. They provide abstraction, modularity and much more to your code. OOP is not language specific, other examples of languages supporting it are C++ and Java.

I suggest youtube to get an understanding of the basics. For instance this video and other related lectures.

查看更多
啃猪蹄的小仙女
5楼-- · 2019-03-21 16:35

First of all back to the definitions:

Class definition:

  • Abstract definition of something, an user-type, a blueprint;
  • Has States / Fields / Properties (what an object knows) and Methods / Behaviors / Member Functions (what an object does);
  • Defines objects behavior and default values;

Object definition:

  • Instance of a Class, Repository of data;
  • Has a unique identity: the property of an object that distinguishes it from other objects;
  • Has its own states: describes the data stored in the object;
  • Exhibits some well defined behavior: follows the class’s description;

Instantiation:

  • Is the way of instantiate a class to create an object;
  • Leaves the object in a valid state;
  • Performed by a constructor;

To use a class you must instantiate the class though a contructor. In PHP a straight-forward example could be:

<?php
class SampleClass {
   function __construct() {
       print "In SampleClass constructor\n";
   }
}

// In SampleClass constructor
$obj = new SampleClass ();
?> 
查看更多
beautiful°
6楼-- · 2019-03-21 16:35

Since you are using PHP I'll use it in my code examples but most everything should apply. OOP treats everything as an object, which is a collection of methods (functions) and variables. In most languages objects are represented in code as classes. Take the following code:

class person
{
   $gender = null;
   $weight = null;
   $height = null;
   $age = null;
   $firstName = null;
   $lastName = null;
   function __CONSTRUCT($firstName, $lastName)
   {
      //__CONSTRUCT is a special method that is called when the class is initialized
      $this->firstName = $firstName;
      $this->lastName = $lastName;
   }
}

This is a valid (if not perfect) class when you use this code you'll first have to initailize an instance of the class which is like making of copy of it in a variable:

$steve = new person('Steve', 'Jobs');

Then when you want to change some property (not technicaly the correct word as there are no properties in PHP but just bear with me in this case I mean variable). We can access them like so:

$steve->age = 54;
查看更多
再贱就再见
7楼-- · 2019-03-21 16:36

Here's a good page about Classes and Objects:

http://ficl.sourceforge.net/oo_in_c.html

查看更多
登录 后发表回答