mirror of
https://github.com/bvanroll/scalaUdemy.git
synced 2025-12-12 20:06:10 +00:00
method notations
This commit is contained in:
7
.idea/dictionaries/Beppe.xml
generated
Normal file
7
.idea/dictionaries/Beppe.xml
generated
Normal file
@@ -0,0 +1,7 @@
|
||||
<component name="ProjectDictionaryState">
|
||||
<dictionary name="Beppe">
|
||||
<words>
|
||||
<w>beppe</w>
|
||||
</words>
|
||||
</dictionary>
|
||||
</component>
|
||||
65
src/exercises/ooExcercises.scala
Normal file
65
src/exercises/ooExcercises.scala
Normal file
@@ -0,0 +1,65 @@
|
||||
package exercises
|
||||
|
||||
object ooExcercises extends App {
|
||||
|
||||
|
||||
|
||||
|
||||
// novel and a writer class
|
||||
// writer: first name, surname, year of birth
|
||||
// =method full name
|
||||
// novel: name, year of release, author
|
||||
// -method authorAge (age at year of release), isWrittenBy(author), copy(new year of release) = new instance of Novel with new year of release
|
||||
|
||||
val au1 = new writer("beppe", "vanrolleghem", 1996)
|
||||
val au2 = new writer("beppe2", "vanrolleghem2", 1997)
|
||||
val nov1 = new novel("somebook",2004,au1)
|
||||
println(nov1.isWrittenBy(au2))
|
||||
println(au1.fullName())
|
||||
println(nov1.authorAge())
|
||||
val nov2 = nov1.copy(2020)
|
||||
println(nov2.authorAge())
|
||||
|
||||
|
||||
|
||||
// counter class
|
||||
// receives an int value
|
||||
// method returns current count
|
||||
//method to increment/decrement => new Counter
|
||||
//overload inc/dec to receive an amount = new counter
|
||||
|
||||
|
||||
var c = new counter(5)
|
||||
println(c.currentCount())
|
||||
c = c.increment()
|
||||
c = c.increment()
|
||||
println(c.currentCount())
|
||||
println(c.decrement(3).currentCount())
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
class writer(firstName: String, surName: String, val yearOfBirth: Int) {
|
||||
def fullName(): String = s"$firstName $surName"
|
||||
|
||||
}
|
||||
|
||||
class novel(name: String, yearOfRelease: Int, author: writer) {
|
||||
def authorAge(): Int = yearOfRelease - author.yearOfBirth
|
||||
def isWrittenBy(author: writer): Boolean = this.author == author
|
||||
def copy (newYearOfRelease: Int): novel = new novel(name, newYearOfRelease, author)
|
||||
}
|
||||
|
||||
|
||||
|
||||
class counter(i: Int) {
|
||||
def currentCount(): Int = i
|
||||
def increment(): counter = new counter(i+1)
|
||||
def decrement(): counter = new counter(i-1)
|
||||
def increment(in: Int): counter = new counter(i+in)
|
||||
def decrement(de: Int): counter = new counter(i-de)
|
||||
}
|
||||
47
src/lectures/part2oop/MethodNotations.scala
Normal file
47
src/lectures/part2oop/MethodNotations.scala
Normal file
@@ -0,0 +1,47 @@
|
||||
package lectures.part2oop
|
||||
|
||||
object MethodNotations extends App {
|
||||
|
||||
|
||||
class Person(val name: String, favoriteMovie: String) {
|
||||
def likes(movie: String): Boolean = movie == favoriteMovie
|
||||
def +(person: Person): String = s"${this.name} is hanging out with ${person.name}"
|
||||
def unary_! : String = name.reverse
|
||||
def isAlive: Boolean = true
|
||||
def apply(): String = s"Hi, my name is $name and I like $favoriteMovie"
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
val mary = new Person("Mary", "Inception")
|
||||
println(mary.likes("Inception"))
|
||||
println(mary likes "Inception")
|
||||
val tom = new Person("Tom", "Fight Club")
|
||||
println(mary + tom)
|
||||
println(mary.+(tom))
|
||||
|
||||
//this functionality is nice, but i will never use it because using a . is way more clear to me
|
||||
|
||||
println(1 + 2)
|
||||
println(1.+(2))
|
||||
|
||||
|
||||
// all operators are methods.
|
||||
val x = -1
|
||||
val y = 1.unary_-
|
||||
println(x == y)
|
||||
|
||||
//unary only works with - + ~ !
|
||||
//this on the other hand seems really usefull for certain things
|
||||
println(!mary)
|
||||
|
||||
//println(mary isAlive) //postfix notation
|
||||
|
||||
|
||||
//apply
|
||||
println(mary.apply())
|
||||
println(mary()) //so apply is kindof a default method
|
||||
|
||||
|
||||
}
|
||||
38
src/lectures/part2oop/OOBasics.scala
Normal file
38
src/lectures/part2oop/OOBasics.scala
Normal file
@@ -0,0 +1,38 @@
|
||||
package lectures.part2oop
|
||||
|
||||
object OOBasics extends App {
|
||||
|
||||
val person = new Person("Beppe",23)
|
||||
println(person.age)
|
||||
println(person.x)
|
||||
person.greet()
|
||||
person.greet("Fred")
|
||||
|
||||
}
|
||||
|
||||
|
||||
class NoFieldPerson(name: String, age: Int)
|
||||
|
||||
// class parameters != fields
|
||||
|
||||
class fieldPerson(name: String, val age: Int) //person.age not accessible, fieldPerson.age is accessible
|
||||
|
||||
class Person(name: String, val age: Int) {
|
||||
//body
|
||||
val x = 2
|
||||
println(1 + 3)
|
||||
|
||||
//this, fields etc...
|
||||
def greet(name: String): Unit = println(s"${this.name} says: Hi, $name")
|
||||
|
||||
|
||||
//overload
|
||||
def greet(): Unit = println(s"Hi, I am $name")
|
||||
|
||||
//multiple constructors
|
||||
//new constructor calls original constructor here
|
||||
//means only usable for easyer default constructors, which can be easily fixed by using default parameters
|
||||
def this(name: String) = this(name, 0)
|
||||
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user