Part 08 - Classes
Defining a Class
Classes are important because they allow you to split up your code into simpler, logical parts. They also allow for better organization and data manipulation.
This declares a blank
"Cat". It can't do anything at all, because there's nothing to do with it.
Fields and Properties
fields hold information and
properies are accessors to that information.
class Cat:declares the start of a
_name. You named the
_name as stringdeclares a
Catthat is a
fluffy = Cat()declares an instance of
fluffy.Name = 'Fluffy'accesses the
Catand sets its value to
'Fluffy'. This will cause
Fields are not set directly because of security.
There are two other types of
getter and a
setter. Technically, a regular
property is just the combination of the two.
If you were to try to assign a value to
fluffy.Name or retrieve a value from
fluffy.FavoriteFood, an error would have occurred, because the code just does not exist for you to do that.
Setter are very handy, but it's actually Boo's shortened version of what is really happening. Here's an example of the full code.
fields are visible inside their own class, you can see that
Name is just a wrapper around
_name. Using this expanded syntax is handy if you want to do extra verification or not have it wrap exactly around its
field, maybe by trimming whitespace or something like that first.
value is a special keyword for the
setter statement, that contains the value to be assigned.
Creates a normal, public class, fully accessible to all other types.
Creates a class that is only accessible by its containing class (the class this was declared in) and any inheriting classes.
A class only accessible by the assembly it was declared in.
Combination of protected and internal.
Creates a class that is only accessible by its containing class (the class this was declared in.)
Creates a class that cannot be instanced. This is designed to be a base class for others.
Creates a class that cannot be inherited from.
abstract keyword is the Class Modifier.
Inheritance is very simple in Boo.
Cat to inherit from
Feline. This gives the members
Cat, even though they were not declared in
You can also have more than one
class inherit from the same
class, which promotes code reuse.
More about inheritance is covered in Part 10 - Polymorphism, or Inherited Methods
Classes can inherit from one or zero other
classes and any number of
To inherit from more than one interface, you would use the notation
class Child(IBaseOne, IBaseTwo, IBaseThree):
Interfaces allow you to set up an API (Application Programming Interface) for
classes to base themselves off of.
No implementation of code is put inside
interfaces, that is up to the
Interfaces can inherit from any number of other
interfaces. They cannot inherit from any
This defines IFeline having one method, Roar, and one
Properties must be explicitly declared in
interfaces. Methods are explained in Part 09 - Methods.
Difference between Value and Reference Types
There are two types in the Boo/.NET world: Value and Reference types. All classes form Reference types. Numbers and such as was discussed in Part 02 - Variables#List of Value Types are value types.
Value types can never be set to
null, they will always have a default value. Numbers default value will generally be 0.
- Create a
classthat inherits from more than one
- See what happens if you try to inherit from more than one
Go on to Part 09 - Methods