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:
parent
fb4f9b36a5
commit
089f30d53a
@ -44,6 +44,18 @@ public class Computer {
|
|||||||
this.healty = healty;
|
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
|
@Override
|
||||||
public String toString() {
|
public String toString() {
|
||||||
return "Computer{" + "age=" + age + ", color='" + color + '\'' + ", healty=" + healty + '}';
|
return "Computer{" + "age=" + age + ", color='" + color + '\'' + ", healty=" + healty + '}';
|
||||||
|
@ -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) ;
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
@ -22,7 +22,7 @@ public class TestComputerUtils {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testFilter() {
|
public void testConstructorReference() {
|
||||||
|
|
||||||
Computer c1 = new Computer(2015, "white");
|
Computer c1 = new Computer(2015, "white");
|
||||||
Computer c2 = new Computer(2009, "black");
|
Computer c2 = new Computer(2009, "black");
|
||||||
@ -53,7 +53,7 @@ public class TestComputerUtils {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testRepair() {
|
public void testStaticMethodReference() {
|
||||||
|
|
||||||
Computer c1 = new Computer(2015, "white", 35);
|
Computer c1 = new Computer(2015, "white", 35);
|
||||||
Computer c2 = new Computer(2009, "black", 65);
|
Computer c2 = new Computer(2009, "black", 65);
|
||||||
@ -66,4 +66,24 @@ public class TestComputerUtils {
|
|||||||
Assert.assertEquals("Computer repaired", new Integer(100), c1.getHealty());
|
Assert.assertEquals("Computer repaired", new Integer(100), c1.getHealty());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testInstanceMethodArbitraryObjectParticularType() {
|
||||||
|
|
||||||
|
Computer c1 = new Computer(2015, "white", 35);
|
||||||
|
Computer c2 = new MacbookPro(2009, "black", 65);
|
||||||
|
List<Computer> inventory = Arrays.asList(c1, c2);
|
||||||
|
inventory.forEach(Computer::turnOnPc);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testSuperMethodReference() {
|
||||||
|
|
||||||
|
final TriFunction<Integer, String, Integer, MacbookPro> integerStringIntegerObjectTriFunction = MacbookPro::new;
|
||||||
|
final MacbookPro macbookPro = integerStringIntegerObjectTriFunction.apply(2010, "black",100);
|
||||||
|
Double initialValue=new Double(999.99);
|
||||||
|
final Double actualValue = macbookPro.calculateValue(initialValue);
|
||||||
|
Assert.assertEquals(766.659, actualValue,0.0);
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user