Search notes:

Object oriented vim script: cats and dogs

This is a totally simple vim scriptlet to demonstrate object oriented inheritance with vim script. The base class is Animal. It contains the member variables name and typicalSound and the member function sayName().
sayName() assembles a string by combining typicalSound with the name and echoes it.
Two classes are derived from Animal: Dog and Cat. Objects of these types can be instantiated with let var = Dog.new(name). After that, the animal can be made say its name by calling var.sayName().
" ../run_ cats-and-dogs
"
"   Inspired by http://vi.stackexchange.com/a/3923/985
"

redir > cats-and-dogs.out


let g:Animal={}

function g:Animal.new(typicalSound, name)

  let l:newAnimal = copy(self)

  let l:newAnimal.typicalSound = a:typicalSound
  let l:newAnimal.name         = a:name

  return l:newAnimal

endfu

function g:Animal.sayName()
  echo self.typicalSound . ', I am ' . self.name
endfu

let g:Dog={}
function g:Dog.new(name)
  let    l:newDog = g:Animal.new('Wuff', a:name)
  return l:newDog
endfu

let g:Cat={}
function g:Cat.new(name)
  let    l:newCat = g:Animal.new('Meow', a:name)
  return l:newCat
endfu



let s:animal_one   = Dog.new('Charlie')
let s:animal_two   = Cat.new('Misty')
let s:animal_three = Dog.new('Daisy'  )


call s:animal_one.sayName()
" 
" Wuff, I am Charlie


call s:animal_two.sayName()
"
" Meow, I am Misty


call s:animal_three.sayName()
"
" Wuff, I am Daisy



redir END
q
Github repository about-vim, path: /vimscript/object-oriented/cats-and-dogs.vim

See also

:help numbered-function, :help dictionary-function

Index