Crystal by example: Classes
Classes are used to organize code. They combine data and actions in one place. Each class serves as a blueprint for creating objects with their own values and behaviors.
Crystal uses classes; the language is class-based because it is object-oriented.
The initialize method runs when we create a new object with .new. It sets the initial values of the object.
Classes can have methods, which perform actions, and variables, which store information. These variables are accessed with @ inside the class.
A getter creates a way to read a value, a setter creates a way to change a value, and property does both at the same time.
self represents the object itself. It is used to call a method from within the same class.
With inheritance, a class can use what another class already has by writing <. The child class can modify or enhance what it inherited from the parent class.
An abstract class is a type of class that serves only as a model. It cannot be instantiated directly and defines methods that other classes must implement.
class Basic def initialize(@name : String) end
def say puts "Hi, I'm #{@name}" endend
b = Basic.new("Anna")b.say
class Info getter name setter age
def initialize(@name : String, @age : Int32) endend
i = Info.new("Carl", 25)puts i.namei.age = 26
class Data property count : Int32
def initialize @count = 0 endend
d = Data.newd.count += 1puts d.count
class Counter getter value
def initialize @value = 0 end
def add @value += 1 self.show end
def show puts "Value: #{@value}" endend
c = Counter.newc.add
class Animal def speak puts "Some sound" endend
class Dog < Animal def speak puts "Woof!" endend
dog = Dog.newdog.speak
abstract class Being abstract def speakend
class Person < Being def speak puts "Hello!" endend
a = Person.newa.speak$ crystal run classes.cr
Hi, I'm AnnaCarl1Value: 1Woof!Hello!