=head1 NAME Perl6::Overview::Object - Object-oriented Programming =head1 DESCRIPTION =head2 Basic class definition class ClassName { has $.public_instance_var; # Untyped instance variable, a read-only accessor is automatically # generated. Methods have read-write access to $.instance_var. has $.public_instance_var is rw; # Untyped instance variable, a read-write accessor is automatically # generated. has Type $.public_instance_var; # Typed instance variable. has $!private_instance_var; # Private instance variable, no accessor is automatically generated. } =head2 Inheritance class ParentClass {...} class SubClass is ParentClass {...} # or class SubClass { is ParentClass; ...; } =head2 Methods method methodname(...) {...} # Standard subroutine signature method methodname($invocant: ...) {...} # The invocant (the class or the instance) is explicitly bound to # $invocant. In any case, there's self and $?CLASS, too. method ^methodname(...) {...} # Class method method methodname(::?CLASS $self: ...) {...} # Instance method =head2 Roles role RoleName { method methodname(...) {...} } class SomeClass does RoleName {...} # or class SomeClass { does RoleName; } =head2 Magical function self # Returns the current instance =head2 Magical variables $?CLASS # The current class as scalar variable ::?CLASS # The current class as package variable $?ROLE # The current role as scalar variable ::?ROLE # The current role as package variable =head2 Subtypes my subtype Int::Odd of Int where { $^value % 2 == 1 }; # Declaration of a new, lexical subtype of Int, allowing only odd integers. my Int::Odd $var = 3; # valid $var++; # invalid my Int::Odd $var = 4; # invalid