Class
Published Apr 23, 2021Updated Mar 7, 2023
Contribute to Docs
A class is a piece of code within a program that functions as a template or blueprint for creating multiple instances of a particular object.
The template provides initial values and behavior for each of these instances. Each instance is an object itself.
Example
Suppose we want to create a Cat
class with three instance variables:
@cat_name
@cat_color
@cat_age
class Catdef initialize(name, color, age)@cat_name = name@cat_color = color@cat_age = ageendend
Now we can create a bunch of objects based off of the Cat
class:
mochi = Cat.new("Mochi", "Gray", 2)bean = Cat.new("Bean", "Black/Gray", 6)femi = Cat.new("Femme en Noir", "Black/White", 9)
Contribute to Docs
- Learn more about how to get involved.
- Edit this page on GitHub to fix an error or make an improvement.
- Submit feedback to let us know how we can improve Docs.
Learn more on Codecademy
- Career path
Computer Science
Looking for an introduction to the theory behind programming? Master Python while learning data structures, algorithms, and more!Includes 6 CoursesWith Professional CertificationBeginner Friendly75 hours - Career path
Front-End Engineer
Front-end engineers work closely with designers to make websites beautiful, functional, and fast.Includes 34 CoursesWith Professional CertificationBeginner Friendly115 hours - Course
Learn Python 3
Learn the basics of Python 3.12, one of the most powerful, versatile, and in-demand programming languages today.With CertificateBeginner Friendly23 hours