Last active
April 25, 2016 15:18
-
-
Save lrlucena/bc80195517d5f900413dd82bcdb0b35c to your computer and use it in GitHub Desktop.
Scala para Desenvolvimento Web
This file contains hidden or 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
// Importar os métodos de StdIn (readInt, readLine, ...) | |
import scala.io.StdIn._ | |
// Variáveis (o tipo é inferido a partir do valor atribuído) | |
val x = 10 // Constante (é a forma preferível) | |
val x: Int = 10 // | |
var y = 20 | |
y = y + 10 | |
// Entrada de dados | |
val a = readLine // Lê uma linha de texto | |
val b, c, d = readInt // Lê 3 números inteiros (um por linha) | |
// Saída de dados | |
println("Olá mundo") | |
val nome = "João" | |
println(s"Olá ${nome}") | |
println(s"1 + 2 = ${1 + 2}") | |
// If usado como um comando | |
if (x>y) | |
println(x) | |
else | |
println(y) | |
// If usado como uma expressão | |
val maior = if (x>y) x else y | |
println(maior) | |
// While | |
while (y>20){ | |
y = y -1 | |
println(y) | |
} | |
// Match (casamento de padrões) | |
y match { | |
case 0 => println("zero") | |
case 1 => println("um") | |
case n if n < 0 => println(s"$n é negativo") | |
case _ => println("Outro valor") | |
} | |
// For como comando | |
for (i <- 1 to 10) { | |
println(i) | |
} | |
// For com mais de um gerador | |
for (i <- 1 to 3 | |
j <- 1 to 4) { | |
println(i*j) | |
} | |
// For como expressão (gera uma lista de números) | |
val pares = for (i <- 1 to 6) yield i * 2 | |
// Funções | |
def soma(a: Int, b: Int) = a + b | |
println(soma(2, 3)) | |
def maior(a: Int, b: Int, c: Int) = { | |
val x = if (a > b) a else b | |
val y = if (x > c) x else c | |
y | |
} | |
def soma3(a: Int = 0, b: Int = 0, c: Int = 0) = a + b + c | |
println(soma3(2, 3)) | |
println(soma3(a = 4, c = 3)) | |
// Funções recursivas (precisa colocar o tipo de retorno) | |
def fat(n: Int): Int = if (n < 2) 1 else fat(n-1) |
This file contains hidden or 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
// Classes e Objetos | |
class Triangulo(base: Double, altura: Double) { | |
def area = base * altura / 2 | |
} | |
val t1 = new Triangulo(10, 20) | |
println(t1.area) | |
class Tri(val base: Double, var altura: Double){ | |
def area = base * altura / 2 | |
} | |
val t2 = new Tri(10, 20) | |
println(t2.base) | |
t2.altura = 30 | |
println(t2.area) | |
class Retangulo1(var base: Double, var altura: Double){ | |
} | |
class Retangulo2(private var _base: Double, private var _altura: Double){ | |
def base = _base | |
def altura = _ altura | |
def altura_=(valor: Double) = if (valor>0) _altura = valor | |
} | |
val ret1 = new Retangulo1(2, 3) | |
ret1.altura = 3 | |
val ret2 = new Retangulo3(2, 3) | |
ret2.altura = 3 |
This file contains hidden or 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
// Herança | |
class Empregado(val nome: String, salarioFixo: Double){ | |
def salario = salarioFixo | |
} | |
class Gerente(val nome: String, val salarioFixo: Double, | |
gratificacao: Double) extends Empregado(nome, salarioFixo){ | |
override def salario = salarioFixo + gratificacao | |
} | |
val a = new Empregado("Joao", 8000.00) | |
val b = new Gerente("Ana", 12000.00, 2000.00) | |
println(s"O salario de ${a.nome} eh ${a.salario}") | |
println(s"O salario de ${b.nome} eh ${b.salario}") |
This file contains hidden or 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
// Objetos (Padrão Singleton, agrega métodos e atributos estáticos) | |
object Escola { | |
val notaMinima = 60 | |
def media(n1: Int, n2: Int) = (n1*2 + n2*3)/5 | |
def maior(n1: Int, n2: Int) = if (n1>n2) n1 else n2 | |
} | |
val nota = Escola.media(100, 90) | |
println(nota) | |
if (nota >= Escola.notaMinima) println("Aprovado") | |
println(Escola.maior(x, y)) |
This file contains hidden or 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
trait Forma { | |
def area: Double | |
def area_cm = area * 10000 | |
} | |
class Quadrado(lado: Double) extends Forma { | |
def area = lado * lado | |
} | |
trait Cor { | |
val cor: String | |
} | |
class Circulo(raio: Double, val cor: String) extends Forma with Cor { | |
def area = Math.PI * raio * raio | |
} | |
abstract class ClassAbstrata(x: Int) { | |
def f(a: Int): Int // Metodo abstrato | |
def id(a: Int) = a // Metodo concreto | |
} | |
object interfaces extends App { | |
def imprimir(c: Cor) = println(c.cor) | |
val quad = new Quadrado(10.0) | |
println(s"Area = ${quad.area_cm} cm2") | |
val circ = new Circulo(5, "Azul") | |
imprimir(circ) | |
} |
This file contains hidden or 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
// Açúcar Sintático: | |
// Metodo apply : a.apply(1) ==> a(1) | |
// Metodo update: a.update(1,10) ==> a(1) = 10 | |
class Aluno(val nome: String) { | |
var nota1 = 0.0 | |
var nota2 = 0.0 | |
def media = (nota1 * 2 + nota2 * 3) / 5 | |
def apply(n: Int) = n match { | |
case 1 => nota1 | |
case 2 => nota2 | |
case _ => 0.0 | |
} | |
def update(n: Int, valor: Double) = n match { | |
case 1 => nota1 = valor | |
case 2 => nota2 = valor | |
case _ => | |
} | |
} | |
object Apply extends App { | |
val joao = new Aluno("Joao") | |
println(joao(1)) // println(joao.apply(1)) | |
joao(1) = 10.0 // joao.update(1, 10.0) | |
joao(2) = 8.0 // joao.update(2, 8.0) | |
println(joao.media) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment