Advertisement
ZivkicaI

KlasaVoScala

Nov 26th, 2019
498
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Scala 0.78 KB | None | 0 0
  1. package exercises.part2oop
  2.  
  3. object ExamplesOOP extends App {
  4.   val writer=new Writer("John", "Green", 1981)
  5.   val novel=new Novel("Great Travel", 1998, writer)
  6.   writer.fullName()
  7.   println(novel.isWrittenBy(writer))
  8.   println(novel.authorAge)
  9.  
  10. }
  11.  
  12.   class Writer(firstName:String, surname:String, val year:Int){
  13.     def fullName():Unit={
  14.     println(s"$firstName" + " " + s"$surname")
  15.     }
  16.   }
  17.  
  18. class Novel(name:String, yearOfRelease:Int, author:Writer){
  19.   def authorAge = yearOfRelease - author.year
  20.   def isWrittenBy(author:Writer) :Boolean= {
  21.     if(author==this.author) true
  22.     else false
  23.   }//ne mora na metodite da im gi pishuvame return Types
  24.   def copy(newYear:Int): Novel=new Novel(name,newYear, author)  //nova instanca od Novel kje vrati so smeneta godina
  25. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement