How to use classes?

I am new to programming, and there is one thing that I confused. What is a class and how to use it? I understand a little, but I can not find the full answer.

By the way, if it depends on the language, then I program in PHP.

Edit: There's something else I forgot to say. In particular, I wanted to ask how defining functions are used in classes. I saw examples of PHP code where functions are defined inside classes, but I cannot understand why.

+3
source share
10 answers

As concise as possible: a class describes a dataset that can perform actions on its own.

, , . , , , , , .. , , getColorPalette getWidth. , ( ) - , , (, ).

- . , . , "" , .

- , . , , , , .

, . Image , , , . , . , , ( , , ..), , .

: , , - "" "". "" - , "". Image , , . , "" , . , , "" , "" .

+21

- , - (). , . OOP , - ++ Java.

youtube . .

+3

PHP, , . , () . . :

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;
   }
}

( ) , , , :

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

, - ( , PHP , , ). :

$steve->age = 54;
+3

: , , , , .

. , . "". , . , , , , , , .. "" ( ). , "()", .

, - " ". . PHP "$house = new House;", $house .

, , , . , , , , "$house->numberOfRooms++;". 4, 5 , 4. , .

; , , ..

, . , - () , , , . , , . "" "", .

, . . , , , . Python , , . .

+2

- , , .

, "int" "string" "float", .

, float, . . ( , , )

- , , , .

, "", , . , . ( "secret02", ). , , , N, , , . . ( ****** ). , , .

, , , , , . , , , . , "".

, . .

, , , . , .

, . , , . , . , - , , . .

+1

- , , , int, string .., - . , . . - Main(), sysync .

, Car car = new Car();, . , , () . , , .

,

CarClass theCar = new CarClass(); //

. , CarClass, . , "" () "" , () , dot , Main(), . - ( - , , # 1). , , . , ! "Hello World!" .

,

CarClass theCar = new CarClass(); //In a way this says this word "car" will be a car
theCar.Color = red; //Given the instance of a car we can add that color detail.

CarClass, . , , , , .

- . , , , , , . , . , , , , . ( , ... .) , , . , , . CarClass ? , , . , . ctor [tab] [tab]. , , . = ""; main() car.Color , , "". , . , , , . .

, . , - , , . , , .ToString(). , ​​ . .ToString() ToString , ToString . .ToString() . , ... Console.WriteLine(theCar.Color); , , . ...

+1
0

- , .
.
, , Linux.

, . , , .

31 rosettacode

0

, :

:

  • -, , ;
  • // ( ) //- ( );
  • ;

:

  • , ;
  • : , ;
  • : , ;
  • Exports a certain correct behavior: a description of the classes follows;

Instantiation:

  • A way to instantiate a class to create an object;
  • Returns the object in a valid state;
  • Performed by the constructor;

To use a class, you must create an instance of the class, although this will be a contructor. In PHP, a simple example could be:

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

// In SampleClass constructor
$obj = new SampleClass ();
?> 
0
source

Source: https://habr.com/ru/post/1710917/


All Articles