-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
9e62d2b
commit 01a90db
Showing
7 changed files
with
140 additions
and
0 deletions.
There are no files selected for viewing
53 changes: 53 additions & 0 deletions
53
...in-crums/jeorg-kotlin-crums-4-variance/src/main/kotlin/org/jesperancinha/ktd/Delegates.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
package org.jesperancinha.ktd | ||
|
||
import java.time.LocalDateTime | ||
|
||
interface Person { | ||
val name: String | ||
val address: String | ||
} | ||
|
||
interface Card { | ||
val cardNumber: Long | ||
val expiryDate: LocalDateTime | ||
} | ||
|
||
data class Client( | ||
override val name: String, | ||
override val address: String | ||
) : Person | ||
|
||
data class DebitCard( | ||
override val cardNumber: Long, | ||
override val expiryDate: LocalDateTime | ||
) : Card | ||
|
||
class Account( | ||
person: Person, | ||
card: Card, | ||
val number: String | ||
) : Person by person, Card by card | ||
|
||
class Delegates { | ||
companion object { | ||
@JvmStatic | ||
fun main(args: Array<String> = emptyArray()) { | ||
val account = Account( | ||
Client( | ||
name = "João", | ||
address = "World" | ||
), | ||
DebitCard( | ||
cardNumber = 1111111111111111, | ||
expiryDate = LocalDateTime.now() | ||
), | ||
number = "AAABBB12312313-3243242-23432" | ||
) | ||
println(account.number) | ||
println(account.name) | ||
println(account.address) | ||
println(account.cardNumber) | ||
println(account.expiryDate) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
49 changes: 49 additions & 0 deletions
49
...eorg-kotlin-crums-4-variance/src/main/kotlin/org/jesperancinha/ktd/delegates/Account.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
package org.jesperancinha.ktd.delegates; | ||
|
||
import java.time.LocalDateTime; | ||
|
||
public record Account( | ||
Person person, | ||
Card card, | ||
String number | ||
|
||
) implements Card, Person { | ||
@Override | ||
public LocalDateTime expiryDate() { | ||
return card.expiryDate(); | ||
} | ||
|
||
@Override | ||
public Long cardNumber() { | ||
return card.cardNumber(); | ||
} | ||
|
||
@Override | ||
public String name() { | ||
return person.name(); | ||
} | ||
|
||
@Override | ||
public String address() { | ||
return person.address(); | ||
} | ||
|
||
public static void main(String[] args) { | ||
var account = new Account( | ||
new Client( | ||
"João", | ||
"World" | ||
), | ||
new DebitCard( | ||
1111111111111111L, | ||
LocalDateTime.now() | ||
), | ||
"AAABBB12312313-3243242-23432" | ||
); | ||
System.out.println(account.number); | ||
System.out.println(account.name()); | ||
System.out.println(account.address()); | ||
System.out.println(account.cardNumber()); | ||
System.out.println(account.expiryDate()); | ||
} | ||
} |
9 changes: 9 additions & 0 deletions
9
...s/jeorg-kotlin-crums-4-variance/src/main/kotlin/org/jesperancinha/ktd/delegates/Card.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
package org.jesperancinha.ktd.delegates; | ||
|
||
import java.time.LocalDateTime; | ||
|
||
public interface Card { | ||
LocalDateTime expiryDate(); | ||
|
||
Long cardNumber(); | ||
} |
14 changes: 14 additions & 0 deletions
14
...jeorg-kotlin-crums-4-variance/src/main/kotlin/org/jesperancinha/ktd/delegates/Client.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
package org.jesperancinha.ktd.delegates; | ||
|
||
|
||
public record Client(String name, String address) implements Person{ | ||
@Override | ||
public String name() { | ||
return name; | ||
} | ||
|
||
@Override | ||
public String address() { | ||
return address; | ||
} | ||
} |
5 changes: 5 additions & 0 deletions
5
...rg-kotlin-crums-4-variance/src/main/kotlin/org/jesperancinha/ktd/delegates/DebitCard.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
package org.jesperancinha.ktd.delegates; | ||
|
||
import java.time.LocalDateTime; | ||
|
||
public record DebitCard(Long cardNumber, LocalDateTime expiryDate) implements Card{} |
7 changes: 7 additions & 0 deletions
7
...jeorg-kotlin-crums-4-variance/src/main/kotlin/org/jesperancinha/ktd/delegates/Person.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package org.jesperancinha.ktd.delegates; | ||
|
||
public interface Person { | ||
String name(); | ||
|
||
String address(); | ||
} |