Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment: Migrated to Confluence 5.3

...

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.

Code Block
borderStylesolid
titledeclaring a function
borderStylesolid
class Cat:
    pass

fluffy = Cat()

...

Simply, fields hold information and properies are accessors to that information.

Code Block
borderStylesolid
titleproperty example
borderStylesolid
class Cat:
    [Property(Name)]
    _name as string

fluffy = Cat()
fluffy.Name = 'Fluffy'

...

There are two other types of properties, a getter and a setter. Technically, a regular property is just the combination of the two.

Code Block
borderStylesolid
titlegetter/setter example
borderStylesolid
class Cat:
    [Getter(Name)]
    _name = 'Meowster'

    [Setter(FavoriteFood)]
    _favoriteFood as string

fluffy = Cat()
print fluffy.Name
fluffy.FavoriteFood = 'Broccoli'
No Format
bgColor#D8DDE9
titleOutput
borderStylesolid
titleOutput
Meowster

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.

Using the attributes Property, Getter, and 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.

Code Block
borderStylesolid
titleexplicit property example
borderStylesolid
class Cat:
    Name as string:
        get:
            return _name
        set:
            _name = value

    _name as string

fluffy = Cat()
fluffy.Name = 'Fluffy'

...

Tip
titleProperty Pre-condition

It is also possible to define a precondition that must be met before setting a value directly through the Property shorthand.

Code Block
borderStylesolid
titleproperty example
borderStylesolid
class Cat:
    [Property(Name, Name is not null)]
    _name as string

fluffy = Cat()
fluffy.Name = null # will raise an ArgumentException

...

Tip
titleRecommendation

Never use the public Class Modifier. It is assumed to be public if you specify no modifier.

Code Block
borderStylesolid
titleclass modifier example
borderStylesolid
abstract class Cat:
    [Property(Name)]
    _name as string

...

Inheritance is very simple in Boo.

Code Block
borderStylesolid
titleinheritance example
borderStylesolid
class Cat(Feline):
    [Property(Name)]
    _name as string

class Feline:
    [Property(Weight)]
    _weight as single //In Kilograms

...

Interfaces can inherit from any number of other interfaces. They cannot inherit from any classes.

Code Block
borderStylesolid
titleinterface example
borderStylesolid
interface IFeline:
    def Roar()

    Name:
        get
        set

...