Class Attributes
In Object‑Oriented Programming, attributes (also called properties or fields) are the pieces of data that each object of a class holds. In EasyBite, attributes define the state of an object, and methods define its behavior. This document provides a very detailed, step‑by‑step guide to declaring, initializing, accessing, and managing class attributes in EasyBite—including how to use the this keyword to refer to the current object. Every concept is explained simply, with plenty of examples and outputs, so that even complete beginners can follow along.
Table of Contents
- Class Attributes
- Table of Contents
- What Are Attributes?
- Declaring Attributes in a Class
- Default Values for Attributes
- Instance Attributes vs. Class‑Level Attributes
- Initializing Attributes in
init - Accessing and Modifying Attributes
- Using Attributes Inside Methods
- Using the
thisKeyword - Inherited Attributes in Subclasses
- Attribute Naming Conventions
- Detailed Examples with Outputs
- Best Practices
- Conclusion
What Are Attributes?
- Attributes are variables that belong to each object (instance) of a class.
- They represent the state or data of an object.
- Every instance has its own copy of each attribute declared in the class.
For example, a Person class might have attributes name and age. Each Person object stores its own name and age independently.
Declaring Attributes in a Class
Attributes are declared at the top of a class using set and a default value. The syntax is:
class ClassName
set attribute1 to defaultValue1
set attribute2 to defaultValue2
...
end class
attribute1,attribute2, etc. become part of every object created fromClassName.- You must supply a default value (e.g., an empty string
"", a number0, or an empty list[]).
No output is produced by declaring attributes alone.
Default Values for Attributes
Defaults serve two purposes:
- Initialization: Ensure attributes have a known starting value.
- Type hinting: Indicate what kind of data the attribute holds (string, number, list, boolean).
Examples of defaults:
- String:
set title to "" - Number:
set count to 0 - List:
set items to [] - Boolean:
set isActive to true
These defaults apply to every new object unless you override them in the constructor.
Instance Attributes vs. Class‑Level Attributes
Instance Attributes
- Declared inside
class ... end class. - Each object gets its own separate copy.
- Modifying an attribute on one object does not affect others.
Class‑Level Attributes (Static)
- EasyBite does not natively support static or class‑level attributes in the same way as some other languages.
- All attributes you declare in the class block behave as instance attributes.
- If you need shared data, you can declare a global variable outside the class.
Initializing Attributes in init
To set attributes based on constructor parameters, define an init block inside your class:
class Person
set name to ""
set age to 0
init(initialName, initialAge)
set name to initialName
set age to initialAge
end init
end class
initruns automatically when you create a new object withnew.- You pass
initialNameandinitialAgetonew Person(...). - Inside
inityou assign those values to the attributes.
No output occurs until you use the object.
Accessing and Modifying Attributes
Once you have an object, you can read or write its attributes using dot notation:
set p to new Person("Alice", 30)
// Read an attribute
show("Name: " + p.name)
// Output:
// Name: Alice
// Modify an attribute
set p.age to 31
show("New age: " + p.age)
// Output:
// New age: 31
p.namereturns the current value ofname.set p.age to 31changesageon that specific object.
Using Attributes Inside Methods
Methods defined in the class can refer directly to attributes without qualification:
class Person
set name to ""
set age to 0
init(n, a)
set name to n
set age to a
end init
method describe()
show("Person: " + name + ", Age: " + age)
end method
end class
set p to new Person("Bob", 25)
p.describe()
// Output:
// Person: Bob, Age: 25
Inside describe(), name and age refer to that object’s attributes.
Using the this Keyword
EasyBite also supports the this keyword to explicitly refer to the current object. This can make code clearer, especially when attribute names shadow local variables or parameters.
Syntax
- Inside methods or
init, usethis.attributeto refer to the object’s property. - Similarly,
this.method()calls another method on the same object.
Detailed Examples
class Counter
set count to 0
init(start)
this.count to start
end init
method increment()
this.count to this.count + 1
end method
method display()
show("Current count is " + this.count)
end method
end class
set c to new Counter(5)
c.display()
// Output:
// Current count is 5
c.increment()
c.display()
// Output:
// Current count is 6
Here, using this.count makes it explicit that we’re modifying the object’s count attribute.
Inherited Attributes in Subclasses
When you create a subclass with inherit, it automatically gets all attributes from its parent:
class Employee inherit Person
set employeeId to ""
end class
set e to new Employee("Carol", 28)
show("Name: " + e.name)
// Output:
// Name: Carol
show("ID: " + e.employeeId)
// Output:
// ID:
EmployeeinheritsnameandagefromPerson.- It adds its own
employeeIdattribute. - If
Personhas aninit, be sure to callsuper.init(...)inEmployee’sinitto initialize inherited attributes.
Attribute Naming Conventions
Follow EasyBite’s naming rules for attributes:
- Lowercase names, optionally with underscores for readability.
- No hyphens or spaces.
- Descriptive but concise:
- Good:
user_name,total_count - Avoid:
UserName,totalCount,user-name
- Good:
Detailed Examples with Outputs
Example 1: Simple Person Class
class Person
set name to ""
set age to 0
init(n, a)
this.name to n
this.age to a
end init
method introduce()
show("Hi, I am " + this.name + " and I am " + this.age + " years old.")
end method
end class
set alice to new Person("Alice", 30)
alice.introduce()
Output:
Hi, I am Alice and I am 30 years old.
Example 2: Car Class with this and Multiple Attributes
class Car
set make to ""
set model to ""
set year to 0
set speed to 0
init(mk, mdl, yr)
this.make to mk
this.model to mdl
this.year to yr
end init
method status()
show("Car: " + this.year + " " + this.make + " " + this.model + "; Speed: " + this.speed + " km/h")
end method
end class
set car1 to new Car("Toyota", "Corolla", 2021)
car1.status()
Output:
Car: 2021 Toyota Corolla; Speed: 0 km/h
Then modify speed:
set car1.speed to 80
car1.status()
Output:
Car: 2021 Toyota Corolla; Speed: 80 km/h
Best Practices
- Use
thiswhen method parameters or local variables share names with attributes. - Always provide sensible defaults for attributes.
- Initialize attributes in
initto override defaults. - Group related attributes at the top for readability.
- Document each attribute with inline comments if needed.
Conclusion
Class attributes in EasyBite are the foundation of an object’s state. By declaring attributes with set ... to, initializing them in init, accessing them via object.attribute or this.attribute, and understanding inheritance, you can build clear, maintainable data models. Combined with methods and the this keyword, attributes enable you to write robust, real‑world applications using EasyBite’s beginner‑friendly OOP syntax. Happy coding!