From fd483e9976da0e743b2ecc5f23494fcd6c6d5a16 Mon Sep 17 00:00:00 2001 From: Tiago Gomes Date: Tue, 29 Aug 2023 21:14:00 -0300 Subject: [PATCH] TypeInference --- .idea/modules.xml | 1 + TypeInference/TypeInference.iml | 11 ++++ TypeInference/src/TypeInferenceExemplo.java | 59 +++++++++++++++++++++ 3 files changed, 71 insertions(+) create mode 100644 TypeInference/TypeInference.iml create mode 100644 TypeInference/src/TypeInferenceExemplo.java diff --git a/.idea/modules.xml b/.idea/modules.xml index 5af87a8..fe6ee97 100644 --- a/.idea/modules.xml +++ b/.idea/modules.xml @@ -12,6 +12,7 @@ + diff --git a/TypeInference/TypeInference.iml b/TypeInference/TypeInference.iml new file mode 100644 index 0000000..37cc804 --- /dev/null +++ b/TypeInference/TypeInference.iml @@ -0,0 +1,11 @@ + + + + + + + + + + + \ No newline at end of file diff --git a/TypeInference/src/TypeInferenceExemplo.java b/TypeInference/src/TypeInferenceExemplo.java new file mode 100644 index 0000000..ebff083 --- /dev/null +++ b/TypeInference/src/TypeInferenceExemplo.java @@ -0,0 +1,59 @@ +import java.util.ArrayList; +import java.util.List; + +class Produto { + private String nome; + private Double preco; + + public Produto(String nome, Double preco) { + this.nome = nome; + this.preco = preco; + } + + public String getNome() { + return nome; + } + + public void setNome(String nome) { + this.nome = nome; + } + + public Double getPreco() { + return preco; + } + + public void setPreco(Double preco) { + this.preco = preco; + } +} + +class ImprimeProdutos { + + public static void imprime(List lista) { + if(!lista.isEmpty()) { + lista.forEach(p -> System.out.println(p.getNome())); + } + else { + System.out.println("Lista vazia"); + } + } +} + +public class TypeInferenceExemplo { + + public static void main(String[] args) { + + List lista = new ArrayList<>(); + + lista.add(new Produto("TV 42'", 2000.00)); + lista.add(new Produto("Geladeira 470L'", 3200.00)); + lista.add(new Produto("Fogão 4 bocas", 900.00)); + lista.add(new Produto("Videogame", 1999.00)); + lista.add(new Produto("Microondas", 550.00)); + + //Inferência de tipo na chamada ao método especializado imprime da classe ImprimeProdutos + ImprimeProdutos.imprime(new ArrayList<>()); + + } + +} \ No newline at end of file