Added example of Method Reference for a super method of a particular object and fixed for instance method of an arbitrary object of a particular type.

This commit is contained in:
giuseppe.bueti
2016-01-26 00:14:08 +01:00
parent fb4f9b36a5
commit 089f30d53a
3 changed files with 69 additions and 2 deletions

View File

@@ -44,6 +44,18 @@ public class Computer {
this.healty = healty;
}
public void turnOnPc() {
System.out.println("Computer turned on");
}
public void turnOffPc() {
System.out.println("Computer turned off");
}
public Double calculateValue(Double initialValue) {
return initialValue/1.50;
}
@Override
public String toString() {
return "Computer{" + "age=" + age + ", color='" + color + '\'' + ", healty=" + healty + '}';

View File

@@ -0,0 +1,35 @@
package com.baeldung.doublecolon;
import java.util.function.Function;
public class MacbookPro extends Computer{
public MacbookPro(int age, String color) {
super(age, color);
}
public MacbookPro(Integer age, String color, Integer healty) {
super(age, color, healty);
}
@Override
public void turnOnPc() {
System.out.println("MacbookPro turned on");
}
@Override
public void turnOffPc() {
System.out.println("MacbookPro turned off");
}
@Override
public Double calculateValue(Double initialValue){
Function<Double,Double> function = super::calculateValue;
final Double pcValue = function.apply(initialValue);
System.out.println("First value is:" +pcValue);
return pcValue + (initialValue/10) ;
}
}