package Test10月.Test1007;
import org.junit.Test;
import java.util.Optional;
public class OptionalTest {
@Test
public void test1() {
TEST test = new TEST();
Optional<Test10月.Test1007.TEST> optional = Optional.of(test);
System.out.println(optional);
System.out.println("*******************");
test = null;
Optional<TEST> optional1 = Optional.ofNullable(test);
System.out.println(optional1);
}
@Test
public void test2() {
TEST test = new TEST();
System.out.println(test);
Optional<TEST> optional = Optional.ofNullable(test);
TEST test2 = optional.orElse(new TEST("lld"));
System.out.println(test2);
System.out.println("****************");
test = null;
Optional<TEST> optional1 = Optional.ofNullable(test);
TEST test3 = optional1.orElse(new TEST("lld"));
System.out.println(test3);
}
}
class TEST {
private String name;
public TEST() {
}
public TEST(String name) {
this.name = name;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public String toString() {
return "TEST{" + "name='" + name + '\'' + '}';
}
}
转载请注明原文地址:https://blackberry.8miu.com/read-34267.html