Skip to content

Commit

Permalink
TypeInference
Browse files Browse the repository at this point in the history
  • Loading branch information
tiagogomes187 committed Aug 30, 2023
1 parent c80338a commit fd483e9
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 0 deletions.
1 change: 1 addition & 0 deletions .idea/modules.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 11 additions & 0 deletions TypeInference/TypeInference.iml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?xml version="1.0" encoding="UTF-8"?>
<module type="JAVA_MODULE" version="4">
<component name="NewModuleRootManager" inherit-compiler-output="true">
<exclude-output />
<content url="file://$MODULE_DIR$">
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
</content>
<orderEntry type="jdk" jdkName="1.8" jdkType="JavaSDK" />
<orderEntry type="sourceFolder" forTests="false" />
</component>
</module>
59 changes: 59 additions & 0 deletions TypeInference/src/TypeInferenceExemplo.java
Original file line number Diff line number Diff line change
@@ -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<Produto> 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<Produto> 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<>());

}

}

0 comments on commit fd483e9

Please sign in to comment.