test vari
This commit is contained in:
64
src/com/oracle/java8/associate/Auto.java
Normal file
64
src/com/oracle/java8/associate/Auto.java
Normal file
@@ -0,0 +1,64 @@
|
||||
package com.oracle.java8.associate;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public class Auto {
|
||||
private static int conteggio;
|
||||
private String marca;
|
||||
|
||||
static {
|
||||
System.out.println("Blocco statico");
|
||||
}
|
||||
|
||||
{
|
||||
System.out.println("Blocco di inizializzazione");
|
||||
}
|
||||
|
||||
public Auto() {
|
||||
System.out.println("Costruttore");
|
||||
this.marca = "";
|
||||
conteggio++;
|
||||
}
|
||||
|
||||
public Auto(String marca) {
|
||||
this.marca = marca;
|
||||
}
|
||||
|
||||
public String getMarca() {
|
||||
return this.marca;
|
||||
}
|
||||
|
||||
public void setMarca(String marca) {
|
||||
this.marca = marca;
|
||||
}
|
||||
|
||||
public static int getConteggio() {
|
||||
return conteggio;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void finalize() throws Throwable {
|
||||
--conteggio;
|
||||
super.finalize();
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
List<Auto> autos = new ArrayList<>();
|
||||
for (int i = 0; i < 5; i++) {
|
||||
Auto a1 = new Auto();
|
||||
autos.add(a1);
|
||||
System.out.println(Auto.getConteggio());
|
||||
}
|
||||
autos.clear();
|
||||
wait(0.1);
|
||||
System.out.println(Auto.getConteggio());
|
||||
}
|
||||
|
||||
private static void wait(double second) {
|
||||
try {
|
||||
System.gc();
|
||||
Thread.sleep((long) (second * 1000));
|
||||
} catch (Exception e) {}
|
||||
}
|
||||
}
|
||||
@@ -6,7 +6,7 @@ class Automobile {
|
||||
}
|
||||
}
|
||||
|
||||
class Car extends Automobile {
|
||||
class Car extends Auto {
|
||||
protected String drive() {
|
||||
return "Driving car";
|
||||
}
|
||||
|
||||
92
src/com/oracle/java8/associate/Eridarieta.java
Normal file
92
src/com/oracle/java8/associate/Eridarieta.java
Normal file
@@ -0,0 +1,92 @@
|
||||
package com.oracle.java8.associate;
|
||||
|
||||
public class Eridarieta {
|
||||
|
||||
public static void main(String[] args) {
|
||||
Animale[] animali = {
|
||||
creaAnimale("Cane"),
|
||||
creaAnimale("Coccodrillo"),
|
||||
creaAnimale("Coccodrillo"),
|
||||
creaAnimale("Cane")
|
||||
};
|
||||
|
||||
for (Animale a : animali) {
|
||||
if (a instanceof Animale) {
|
||||
System.out.println(a.getClass().getSimpleName() + " sono un animale");
|
||||
}
|
||||
if (a instanceof Mammifero) {
|
||||
System.out.println(a.getClass().getSimpleName() + " sono un mammifero");
|
||||
}
|
||||
if (a instanceof Rettile) {
|
||||
System.out.println(a.getClass().getSimpleName() + " sono un rettile");
|
||||
}
|
||||
|
||||
a.muove();
|
||||
}
|
||||
}
|
||||
|
||||
static Animale creaAnimale(String tipo) {
|
||||
switch (tipo) {
|
||||
case "Cane":
|
||||
return new Cane();
|
||||
case "Coccodrillo":
|
||||
return new Coccodrillo();
|
||||
default:
|
||||
throw new RuntimeException("Tipo animale non esistente");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
interface Animale {
|
||||
void muove();
|
||||
}
|
||||
|
||||
abstract class Mammifero implements Animale {
|
||||
protected int gambe;
|
||||
|
||||
public int getGambe() {
|
||||
return gambe;
|
||||
}
|
||||
|
||||
public void setGambe(int gambe) {
|
||||
this.gambe = gambe;
|
||||
}
|
||||
}
|
||||
|
||||
abstract class Rettile implements Animale {
|
||||
protected int temperatura;
|
||||
|
||||
public int getTemperatura() {
|
||||
return temperatura;
|
||||
}
|
||||
|
||||
public void setTemperatura(int temperatura) {
|
||||
this.temperatura = temperatura;
|
||||
}
|
||||
}
|
||||
|
||||
class Cane extends Mammifero {
|
||||
|
||||
public Cane() {
|
||||
super();
|
||||
this.gambe = 4;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void muove() {
|
||||
System.out.println(String.format("Corre sulle %d gambe", getGambe()));
|
||||
}
|
||||
}
|
||||
|
||||
class Coccodrillo extends Rettile {
|
||||
|
||||
public Coccodrillo() {
|
||||
super();
|
||||
this.temperatura = 30;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void muove() {
|
||||
System.out.println(String.format("Ha una temperatura di %d°C", temperatura));
|
||||
}
|
||||
}
|
||||
51
src/com/oracle/java8/associate/Overloading.java
Normal file
51
src/com/oracle/java8/associate/Overloading.java
Normal file
@@ -0,0 +1,51 @@
|
||||
package com.oracle.java8.associate;
|
||||
|
||||
public class Overloading {
|
||||
|
||||
public static void main(String[] args) throws Exception {
|
||||
X o = new X();
|
||||
|
||||
o.m(1.0, 2); // 2
|
||||
o.m(1, 2); // 4
|
||||
o.m(1, 0f); // 1
|
||||
o.m(3., 8); // 2
|
||||
o.m(0L, 9); // 3
|
||||
|
||||
o.m();
|
||||
o = new Y();
|
||||
o.m();
|
||||
}
|
||||
}
|
||||
|
||||
class X {
|
||||
|
||||
public void m(int x, double y) {
|
||||
System.out.println("1");
|
||||
}
|
||||
|
||||
public void m(double x, long y) {
|
||||
System.out.println("2");
|
||||
}
|
||||
|
||||
public void m(float x, int y) {
|
||||
System.out.println("3");
|
||||
}
|
||||
|
||||
public void m(int x, int y) {
|
||||
System.out.println("4");
|
||||
}
|
||||
|
||||
public Object m() throws Exception {
|
||||
System.out.println("m() => X");
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
class Y extends X {
|
||||
|
||||
@Override
|
||||
public String m() throws InstantiationException {
|
||||
System.out.println("m() => Y");
|
||||
return null;
|
||||
}
|
||||
}
|
||||
@@ -64,7 +64,7 @@ public class ArrayTest {
|
||||
|
||||
//String s = a6[-1]; ArrayIndexOutOfBoundsException
|
||||
|
||||
//Field f = new int[0].getClass().getField("length"); java.lang.NoSuchFieldException: length
|
||||
//Field m = new int[0].getClass().getField("length"); java.lang.NoSuchFieldException: length
|
||||
|
||||
// non funziona
|
||||
int[] a7 = unmodifieble(new int[]{1, 2, 3, 4, 5});
|
||||
|
||||
@@ -1,6 +1,8 @@
|
||||
package com.oracle.java8.associate.test;
|
||||
|
||||
import java.util.function.Consumer;
|
||||
import java.util.function.Function;
|
||||
import java.util.function.Supplier;
|
||||
|
||||
public class FunctionalTest {
|
||||
|
||||
@@ -14,6 +16,15 @@ public class FunctionalTest {
|
||||
EmployeeFactory factory = Employee::new;
|
||||
Employee employee = factory.getEmployee("John Hammond", 25);
|
||||
System.out.println(employee);
|
||||
|
||||
Function<Object, String> convert = FunctionalTest::convert;
|
||||
String ciao = convert.apply("Ciao");
|
||||
System.out.println(ciao);
|
||||
}
|
||||
|
||||
private static String convert(Object o) {
|
||||
Function<Object, String> f = obj -> obj.toString();
|
||||
return f.apply(o);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -20,5 +20,13 @@ public class StringTest {
|
||||
System.out.println(sb.capacity());
|
||||
System.out.println(sb.toString());
|
||||
System.out.println(s.substring(0, 4));
|
||||
|
||||
String s1 = "Java";
|
||||
String[] s2 = {"J", "a", "v", "a"};
|
||||
String s3 = "";
|
||||
for (String elem : s2) {
|
||||
s3 = s3 + elem;
|
||||
}
|
||||
System.out.println(s1 == s3);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user