目录
范例2-8:定义Point类,使用Object作为属性类型。
范例2-14:使用Point类将泛型类型设置为String。
范例2-49:引用静态方法,本次将引用在String类里的valueOf()的静态方法(public static String valueOf(int x))。
范例2-50:普通方法引用,本次引用String类中的toUpperCase()方法(public String toUpper Case())。
范例2-54:消费型接口。消费型接口主要是接收参数但是不返回数据,所以本次映射System.out. println(String str)方法。
范例2-55:供给型接口。供给型接口是不接收任何参数的,所以本次引用String类的toUpperCase()方法(public String toUpperCase())。
第一章:Eclipse开发工具
1.2 JDT的使用(重点)
范例1-1:编写Hello类。
package com.yootk.demo; public class Hello { public static void main(String[] args) { System.out.println("更多课程请访问:www.yootk.com"); } } |
范例1-2:定义基础类结构。
class Book { private String title ; private double price ; } |
范例1-3:输出初始化参数。
package com.yootk.demo; public class InitParameter { public static void main(String[] args) { for (int x = 0 ; x < args.length ; x ++) { System.out.println(args[x]) ; } } } |
1.3 JUnit的使用(重点)
范例1-4:定义一个程序。
package com.yootk.util; public class MyMath { public static int div(int x, int y) throws Exception { return x / y; } } |
范例1-5:编写JUnit测试程序。
package cn.mldn.test; import org.junit.Test; import cn.mldn.util.MyMath; import junit.framework.TestCase; public class MyMathTest { @Test public void testDiv() { try { TestCase.assertEquals(MyMath.div(10, 2), 5); } catch (Exception e) { e.printStackTrace(); } } } |
第二章:Java新特性
2.1 可变参数
范例2-1:最初的解决方案。
package com.yootk.demo; public class TestDemo { public static void main(String[] args) { System.out.println(add(new int[]{1,2,3})); // 传递3个整型数据 System.out.println(add(new int[]{10,20})); // 传递2个整型数据 } /** * 实现任意多个整型数据的相加操作 * @param data 由于要接收多个整型数据,所以使用数组完成接收 * @return 多个整型数据的累加结果 */ public static int add(int [] data) { int sum = 0 ; for (int x = 0 ; x < data.length ; x ++) { sum += data[x] ; } return sum ; } } |
范例2-2:使用可变参数定义方法。
package com.yootk.demo; public class TestDemo { public static void main(String[] args) { // 可变参数支持接收数组 System.out.println(add(new int[]{1,2,3})); // 传递3个整型数据 System.out.println(add(new int[]{10,20})); // 传递2个整型数据 // 或者使用“,”区分不同的参数,接收的时候还是数组 System.out.println(add(1,2,3)); // 传递3个参数 System.out.println(add(10,20)); // 传递2个参数 System.out.println(add()); // 不传递参数 } /** * 实现任意多个整型数据的相加操作 * @param data 由于要接收多个整型数据,所以使用数组完成接收 * @return 多个整型数据的累加结果 */ public static int add(int ... data) { int sum = 0 ; for (int x = 0 ; x < data.length ; x ++) { sum += data[x] ; } return sum ; } } |
2.2 foreach循环
范例2-3:数组输出。
package com.yootk.demo; public class TestDemo { public static void main(String[] args) { int data[] = new int[] { 1, 2, 3, 4, 5 }; // 定义数组 for (int x = 0; x < data.length; x++) { // 循环输出数组 System.out.print(data[x] + "、"); } } } |
范例2-4:利用foreach循环实现输出。
package com.yootk.demo; public class TestDemo { public static void main(String[] args) { int data[] = new int[] { 1, 2, 3, 4, 5 }; // 定义数组 for (int x : data) { // 循环次数由数组长度决定 // 每一次循环实际上都表示数组的角标增长,会取得每一个数组的内容,并且将其设置给x System.out.println(x + "、"); // x就是每一个数组元素的内容 } } } |
2.3 静态导入
范例2-5:传统的做法。
package com.yootk.util; public class MyMath { public static int add(int x,int y) { return x + y ; } public static int div(int x,int y) { return x / y ; } } |
范例2-6:基本使用形式。
package com.yootk.demo; import com.yootk.util.MyMath; public class TestDemo { public static void main(String[] args) { System.out.println("加法操作:" + MyMath.add(10, 20)); System.out.println("除法操作:" + MyMath.div(10, 2)); } } |
范例2-7:静态导入。
package com.yootk.demo; // 将MyMath类中的全部static方法导入,这些方法就好比在主类中定义的static方法一样 import static com.yootk.util.MyMath.*; public class TestDemo { public static void main(String[] args) { // 直接使用方法名称访问 System.out.println("加法操作:" + add(10, 20)); System.out.println("除法操作:" + div(10, 2)); } } |
2.4 泛型
范例2-8:定义Point类,使用Object作为属性类型。
class Point { // 定义坐标 private Object x ; // 可以保存任意数据 private Object y ; // 可以保存任意数据 public void setX(Object x) { this.x = x; } public void setY(Object y) { this.y = y; } public Object getX() { return x; } public Object getY() { return y; } } |
范例2-9:在Point类里面保存整型数据。
public class TestDemo { public static void main(String[] args) { // 第一步:根据需要设置数据,假设此时的作用是传递坐标 Point p = new Point() ; // 实例化Point类数据 p.setX(10); // 设置坐标数据 p.setY(20); // 设置坐标数据 // 第二步:根据设置好的坐标取出数据进行操作 int x = (Integer) p.getX(); // 取出坐标数据 int y = (Integer) p.getY(); // 取出坐标数据 System.out.println("x坐标:" + x + ",y坐标:" + y); } } |
范例2-10:在Point类里面保存浮点型数据。
public class TestDemo { public static void main(String[] args) { // 第一步:根据需要设置数据,假设此时的作用是传递坐标 Point p = new Point() ; // 实例化Point类数据 p.setX(10.2); // 设置坐标数据 p.setY(20.3); // 设置坐标数据 // 第二步:根据设置好的坐标取出数据进行操作 double x = (Double) p.getX(); // 取出坐标数据 double y = (Double) p.getY(); // 取出坐标数据 System.out.println("x坐标:" + x + ",y坐标:" + y); } } |
范例2-11:在Point类里面保存使用字符串数据。
public class TestDemo { public static void main(String[] args) { // 第一步:根据需要设置数据,假设此时的作用是传递坐标 Point p = new Point() ; // 实例化Point类数据 p.setX("东经100度"); // 设置坐标数据 p.setY("北纬20度"); // 设置坐标数据 // 第二步:根据设置好的坐标取出数据进行操作 String x = (String) p.getX(); // 取出坐标数据 String y = (String) p.getY(); // 取出坐标数据 System.out.println("x坐标:" + x + ",y坐标:" + y); } } |
范例2-12:错误的程序。
public class TestDemo { public static void main(String[] args) { // 第一步:根据需要设置数据,假设此时的作用是传递坐标 Point p = new Point() ; // 实例化Point类数据 p.setX("东经100度"); // 设置坐标数据 p.setY(10) ; // 设置坐标数据,数据错误 // 第二步:根据设置好的坐标取出数据进行操作 String x = (String) p.getX(); // 取出坐标数据 String y = (String) p.getY(); // 取出坐标数据 System.out.println("x坐标:" + x + ",y坐标:" + y); } } |
范例2-13:使用泛型修改Point类。
package com.yootk.demo; // 此时设置的T在Point类定义上只表示一个标记,在使用时需要为其设置具体的类型 class Point<T> { // 定义坐标,Type = 简写T,是一个类型标记 private T x ; // 此属性的类型不知道,由Point类使用时动态决定 private T y ; // 此属性的类型不知道,由Point类使用时动态决定 public void setX(T x) { this.x = x; } public void setY(T y) { this.y = y; } public T getX() { return x; } public T getY() { return y; } } |
范例2-14:定义多个泛型。
class Point<P, R> { public R fun(P p) { return null ; } } |
范例2-14:使用Point类将泛型类型设置为String。
public class TestDemo { public static void main(String[] args) { // 第一步:根据需要设置数据,假设此时的作用是传递坐标 Point<String> p = new Point<String>() ; // 实例化Point类数据,设置泛型类型为String p.setX("东经100度"); // 设置坐标数据 p.setY("北纬20度") ; // 设置坐标数据 // 第二步:根据设置好的坐标取出数据进行操作 String x = p.getX(); // 取出坐标数据,不再需要强制转换 String y = p.getY(); // 取出坐标数据,不再需要强制转换 System.out.println("x坐标:" + x + ",y坐标:" + y); } } |
范例2-15:将泛型类型设置为整型(Integer)。
public class TestDemo { public static void main(String[] args) { // 第一步:根据需要设置数据,假设此时的作用是传递坐标 Point<Integer> p = new Point<Integer>() ; // 实例化Point类数据,设置泛型类型为String p.setX(10); // 设置坐标数据 p.setY(20) ; // 设置坐标数据 // 第二步:根据设置好的坐标取出数据进行操作 int x = p.getX(); // 取出坐标数据,不再需要强制转换 int y = p.getY(); // 取出坐标数据,不再需要强制转换 System.out.println("x坐标:" + x + ",y坐标:" + y); } } |
范例2-16:不设置泛型类型。
public class TestDemo { public static void main(String[] args) { // 第一步:根据需要设置数据,假设此时的作用是传递坐标 Point p = new Point() ; // 将使用Object类型描述泛型 p.setX(10); // 设置坐标数据,向上转型为Object p.setY(20) ; // 设置坐标数据,向上转型为Object // 第二步:根据设置好的坐标取出数据进行操作 Integer x = (Integer) p.getX(); Integer y = (Integer) p.getY(); System.out.println("x坐标:" + x + ",y坐标:" + y); } } |
范例2-17:定义一个支持泛型类型的类。
package com.yootk.demo; class Message<T> { // 类上使用泛型 private T msg; public void setMsg(T msg) { this.msg = msg; } public T getMsg() { return msg; } } |
范例2-18:使用泛型操作。
package com.yootk.demo; public class TestDemo { public static void main(String[] args) { Message<String> m = new Message<String>(); m.setMsg("www.yootk.com"); // 设置内容 fun(m); // 引用传递 } /** * 接收Message类对象,并且调用getter方法输出类中的msg属性内容 * @param temp 接收Message类对象的引用传递,此处设置的泛型类型为String */ public static void fun(Message<String> temp) { System.out.println(temp.getMsg()); } } |
范例2-19:错误的使用。
public class TestDemo { public static void main(String[] args) { Message<Integer> m = new Message<Integer>(); m.setMsg(30); // 设置内容 fun(m); // 程序错误,因为参数的泛型类型是String } public static void fun(Message<String> temp) { System.out.println(temp.getMsg()); } } |
范例2-20:使用通配符“?”解决参数传递问题。
public class TestDemo { public static void main(String[] args) { Message<Integer> m1 = new Message<Integer>(); Message<String> m2 = new Message<String>(); m1.setMsg(100); // 设置属性内容 m2.setMsg("www.yootk.com"); // 设置属性内容 fun(m1); // 引用传递 fun(m2); // 引用传递 } public static void fun(Message<?> temp) { // 不能设置,但是可以取出 System.out.println(temp.getMsg()); } } |
范例2-21:定义方法参数时不设置泛型类型。
public class TestDemo { public static void main(String[] args) { Message<Integer> m1 = new Message<Integer>(); Message<String> m2 = new Message<String>(); m1.setMsg(100); m2.setMsg("www.yootk.com"); fun(m1); fun(m2); } public static void fun(Message temp) { // 随意修改属性内容,逻辑错误 temp.setMsg("魔乐科技软件学院:www.mldn.cn"); System.out.println(temp.getMsg()); } } |
范例2-22:设置泛型的上限。
package com.yootk.demo; class Message<T extends Number> { // 设置泛型上限,只能够是Number或Number子类 private T msg; public void setMsg(T msg) { this.msg = msg; } public T getMsg() { return msg; } } public class TestDemo { public static void main(String[] args) { Message<Integer> m1 = new Message<Integer>(); // Integer是Number子类 m1.setMsg(100); fun(m1); // 引用传递 } public static void fun(Message<? extends Number> temp) { // 定义泛型上限 System.out.println(temp.getMsg()); } } |
范例2-23:设置泛型的下限。
package com.yootk.demo; class Message<T> { // 定义泛型 private T msg; public void setMsg(T msg) { this.msg = msg; } public T getMsg() { return msg; } } public class TestDemo { public static void main(String[] args) { Message<String> m1 = new Message<String>(); m1.setMsg("更多课程请访问:www.yootk.com"); // 设置属性内容 fun(m1); // 引用传递 } public static void fun(Message<? super String> temp) { // 定义泛型下限 System.out.println(temp.getMsg()); } } |
范例2-24:定义泛型接口。
package com.yootk.demo; /** * 定义泛型接口,由于类与接口命名标准相同,为了区分出类与接口,在接口前加上字母“I”,例如:IMessage * 如果定义抽象类,则可以在前面加上Abstract,例如:AbstractMessage * @author YOOTK * @param <T> print()方法使用的泛型类型 */ interface IMessage<T> { // 定义泛型接口 public void print(T t); } |
范例2-25:泛型方法定义。
package com.yootk.demo; public class TestDemo { public static void main(String[] args) { String str = fun("www.yootk.com"); // 泛型类型为String System.out.println(str.length()); // 计算长度 } /** * 此方法为泛型方法,T的类型由传入的参数类型决定 * 必须在方法返回值类型前明确定义泛型标记 * @param t 参数类型,同时也决定了返回值类型 * @return 直接返回设置进来的内容 */ public static <T> T fun(T t) { return t; } } |
2.5 枚举
范例2-26:定义颜色的枚举类。
package com.yootk.demo; enum Color { // 定义枚举类 RED, GREEN, BLUE; // 表示此处为实例化对象 } public class TestDemo { public static void main(String[] args) { Color red = Color.RED; // 直接取得枚举对象 System.out.println(red); } } |
范例2-27:利用多例设计模式实现类似枚举的操作。
package com.yootk.demo; class Color { private String title; private static final Color RED = new Color("红色"); private static final Color GREEN = new Color("绿色"); private static final Color BLUE = new Color("蓝色"); private Color(String title) { this.title = title; } public static Color getInstance(int ch) { switch (ch) { case 1: return RED; case 2: return GREEN; case 3: return BLUE; default: return null; } } public String toString() { return this.title; } } public class TestDemo { public static void main(String[] args) { Color red = Color.getInstance(1); System.out.println(red); } } |
范例2-28:使用Enum定义的方法。
package com.yootk.demo; enum Color { // 定义枚举类 RED, GREEN, BLUE; // 表示此处为实例化对象 } public class TestDemo { public static void main(String[] args) { Color red = Color.RED; // 直接取得枚举对象 System.out.println("枚举对象序号:" + red.ordinal()); System.out.println("枚举对象名称:" + red.name()); } } |
范例2-29:返回枚举中的全部内容。
package com.yootk.demo; enum Color { // 定义枚举类 RED, GREEN, BLUE; // 表示此处为实例化对象 } public class TestDemo { public static void main(String[] args) { for(Color c : Color.values()) { System.out.println(c.ordinal() + " - " + c.name()); } } } |
范例2-30:扩充枚举功能。
package com.yootk.demo; enum Color { RED("红色"), GREEN("绿色"), BLUE("蓝色"); // 定义枚举对象,必须写在首行 private String title; // 属性 private Color(String title) { // 构造方法,不能使用public声明 this.title = title; } public String toString() { // 覆写toString()方法 return this.title; } } public class TestDemo { public static void main(String[] args) { for (Color c : Color.values()) { // 取得全部枚举对象 System.out.print(c + "、"); // 直接输出对象调用toString() } } } |
范例2-31:枚举实现接口。
package com.yootk.demo; interface IMessage { public String getTitle() ; } enum Color implements IMessage { // 实现接口 RED("红色"), GREEN("绿色"), BLUE("蓝色"); // 定义枚举对象,都是IMessage接口实例 private String title; // 属性 private Color(String title) { // 构造方法,不能使用public声明 this.title = title; } public String getTitle() { // 覆写方法 return this.title ; } public String toString() { // 覆写toString()方法 return this.title; } } public class TestDemo { public static void main(String[] args) { IMessage msg = Color.RED ; // 实例化接口对象 System.out.println(msg.getTitle()); } } |
范例2-32:另外一种接口的实现。
package com.yootk.demo; interface IMessage { public String getTitle() ; } enum Color implements IMessage { // 实现接口 RED("红色") { // 适应匿名内部类的方式实现接口 public String getTitle() { return this + " - red"; } }, GREEN("绿色") { // 适应匿名内部类的方式实现接口 public String getTitle() { return this + " - green"; } }, BLUE("蓝色") { // 适应匿名内部类的方式实现接口 public String getTitle() { return this + " - blue"; } }; private String title; // 属性 private Color(String title) { // 构造方法,不能使用public声明 this.title = title; } public String toString() { // 覆写toString()方法 return this.title; } } public class TestDemo { public static void main(String[] args) { IMessage msg = Color.RED ; // 实例化接口对象 System.out.println(msg.getTitle()); } } |
范例2-33:定义抽象方法并覆写。
package com.yootk.demo; enum Color { RED("红色") { // 适应匿名内部类的方式实现接口 public String getTitle() { return this + " - red"; } }, GREEN("绿色") { // 适应匿名内部类的方式实现接口 public String getTitle() { return this + " - green"; } }, BLUE("蓝色") { // 适应匿名内部类的方式实现接口 public String getTitle() { return this + " - blue"; } }; private String title; // 属性 private Color(String title) { // 构造方法,不能使用public声明 this.title = title; } public String toString() { // 覆写toString()方法 return this.title; } public abstract String getTitle() ; // 定义抽象方法 } public class TestDemo { public static void main(String[] args) { System.out.println(Color.RED.getTitle()); } } |
范例2-34:在switch语句上使用枚举。
package com.yootk.demo; enum Color { RED, GREEN, BLUE; } public class TestDemo { public static void main(String[] args) { Color c = Color.RED; switch (c) { // 支持枚举判断 case RED: // 判断枚举内容 System.out.println("这是红色!"); break; case GREEN: // 判断枚举内容 System.out.println("这是绿色!"); break; case BLUE: // 判断枚举内容 System.out.println("这是蓝色!"); break; } } } |
范例2-35:在类设计结构中使用枚举。
package com.yootk.demo; enum Sex { MALE("男"),FEMALE("女") ; private String title ; private Sex(String title) { this.title = title ; } public String toString() { return this.title ; } } class Member { private String name ; private int age ; private Sex sex ; // 定义性别属性 public Member(String name,int age,Sex sex) { this.name = name ; this.age = age ; this.sex = sex ; } public String toString() { return "姓名:" + this.name + ",年龄:" + this.age + ",性别:" + this.sex ; } } public class TestDemo { public static void main(String[] args) { System.out.println(new Member("李兴华", 36, Sex.MALE)); } } |
2.6 Annotation
范例2-36:准确覆写。
class Book { @Override // 只要正确进行了覆写,就不会出现编译的语法错误 public String toString() { // 原本打算覆写toString() return "《名师讲坛 —— Oracle开发实战经典》" ; } } |
范例2-37:错误的程序。
class Book { public String tostring() { // 原本打算覆写toString() return "这是一本书!" ; } } |
范例2-38:声明过期操作。
package com.yootk.demo; class Book { @Deprecated // 此方法为过期操作 public void } } public class TestDemo { public static void main(String[] args) { Book book = new Book(); book. } } |
范例2-39:压制多个警告信息。
package com.yootk.demo; class Book<T> { private T title; public void setTitle(T title) { this.title = title; } public T getTitle() { return title; } } public class TestDemo { @SuppressWarnings({ "rawtypes", "unchecked" }) public static void main(String[] args) { Book book = new Book(); // 没有声明泛型,产生“rawtypes”警告信息 book.setTitle("HELLO"); // 出现警告信息,产生“unchecked”警告信息 } } |
2.7 接口定义加强
范例2-40:定义普通方法。
package com.yootk.demo; interface IMessage { // 定义接口 public void print(); // 这是一个接口里面原本定义的方法 default void fun() { // 在接口里面定义了一个普通的方法 System.out.println("更多课程请访问:www.yootk.com"); } } class MessageImpl implements IMessage { @Override public void print() { // 覆写print()方法 System.out.println("魔乐科技软件学院:www.mldn.cn"); } } public class TestDemo { public static void main(String[] args) { IMessage msg = new MessageImpl(); msg.print(); // 子类已覆写接口方法 msg.fun(); // 此方法是在接口里面直接定义的 } } |
范例2-41:定义static方法。
package com.yootk.demo; interface IMessage { // 定义接口 public void print(); // 这是一个接口里面原本定义的方法 static void get() { System.out.println("更多课程请访问:www.yootk.com"); } } public class TestDemo { public static void main(String[] args) { IMessage.get(); // 直接利用接口调用静态方法 } } |
2.8 Lamda表达式
范例2-42:Lamda表达式入门操作。
package com.yootk.demo; interface IMessage { public void print() ; } public class TestDemo { public static void main(String[] args) { // 此处为Lamda表达式,没有任何输入参数,只是进行输出操作 fun(() -> System.out.println("更多课程请访问:www.yootk.com")); } public static void fun(IMessage msg) { msg.print() ; } } |
范例2-43:使用匿名内部类操作。
package com.yootk.demo; interface IMessage { public void print() ; } public class TestDemo { public static void main(String[] args) { fun(new IMessage() { // 等价于Lamda表达式定义 @Override public void print() { System.out.println("更多课程请访问:www.yootk.com") ; } }); } public static void fun(IMessage msg) { msg.print() ; } } |
范例2-44:函数式接口。
@FunctionalInterface interface IMessage { public void print() ; } |
范例2-45:编写多行语句。
package com.yootk.demo; @FunctionalInterface interface IMessage { public void print() ; } public class TestDemo { public static void main(String[] args) { String info = "魔乐科技软件学院:www.mldn.cn" ; fun(() -> { System.out.println("更多课程请访问:www.yootk.com") ; System.out.println(info) ; // 输出方法中的变量 }); } public static void fun(IMessage msg) { msg.print() ; } } |
范例2-46:定义有参数有返回值的方法。
package com.yootk.demo; @FunctionalInterface interface IMessage { public int add(int x, int y); } public class TestDemo { public static void main(String[] args) { fun((s1, s2) -> { // 传递两个参数,此处只是一个参数标记 return s1 + s2; }); } public static void fun(IMessage msg) { System.out.println(msg.add(10, 20)); } } |
范例2-47:直接返回计算结果。
public static void main(String[] args) { // 直接返回两个参数的计算结果,省略return fun((s1, s2) -> s1 + s2); } |
范例2-48:传递可变参数。
package com.yootk.demo; @FunctionalInterface interface IMessage { public int add(int ... args); static int sum(int ... args) { // 此方法可以由接口名称直接调用 int sum = 0; for (int temp : args) { sum += temp; } return sum; } } public class TestDemo { public static void main(String[] args) { // 在Lamda表达式中直接调用接口里定义的静态方法 fun((int... param) -> IMessage.sum(param)); } public static void fun(IMessage msg) { System.out.println(msg.add(10, 20, 30)); // 传递可变参数 } } |
2.9 方法引用
范例2-49:引用静态方法,本次将引用在String类里的valueOf()的静态方法(public static String valueOf(int x))。
package com.yootk.demo; /** * 实现方法的引用接口 * @param <P> 引用方法的参数类型 * @param <R> 引用方法的返回类型 */ @FunctionalInterface interface IMessage<P, R> { /** * 此处为方法引用后的名字 * @param p 参数类型 * @return 返回指定类型的数据 */ public R zhuanhuan(P p); } public class TestDemo { public static void main(String[] args) { // 将String.valueOf()方法变为IMessage接口里的zhuanhuan()方法 // valueOf()方法可以接收int型数据,返回String型数据 IMessage<Integer, String> msg = String::valueOf; String str = msg.zhuanhuan(1000); // 调用引用方法进行操作 System.out.println(str.replaceAll("0", "9")); } } |
范例2-50:普通方法引用,本次引用String类中的toUpperCase()方法(public String toUpper Case())。
package com.yootk.demo; /** * 实现方法的引用接口 * @param <P> 引用方法的参数类型 * @param <R> 引用方法的返回类型 */ @FunctionalInterface interface IMessage<R> { public R upper() ; } public class TestDemo { public static void main(String[] args) { // String类的toUpperCase()定义:public String toUpperCase() // 此方法没有参数,但是有返回值,并且这个方法一定要在有实例化对象的情况下才可以调用 //“yootk”字符串是String类的实例化对象,所以可以直接调用toUpperCase()方法 // 将toUpperCase()函数的应用交给了IMessage接口 IMessage<String> msg = "yootk" :: toUpperCase ; String str = msg.upper() ; // 相当于“"yootk".toUpperCase()” System.out.println(str); } } |
范例2-51:引用特定类的方法。
package com.yootk.demo; @FunctionalInterface interface IMessage<P> { public int compare(P p1,P p2) ; } public class TestDemo { public static void main(String[] args) { IMessage<String> msg = String :: compareTo ; // 引用String类的普通方法 // 传递调用的参数,形式为:"A".compareTo("B") System.out.println(msg.compare("A", "B")); } } |
范例2-52:引用构造方法。
package com.yootk.demo; @FunctionalInterface interface IMessage<C> { public C create(String t, double p); // 引用构造方法 } class Book { private String title; private double price; public Book(String title, double price) { // 有两个参数的构造 this.title = title; this.price = price; } @Override public String toString() { return "书名:" + this.title + ",价格:" + this.price; } } public class TestDemo { public static void main(String[] args) { IMessage<Book> msg = Book::new; // 引用构造方法 // 调用的虽然是create(),但是这个方法引用的是Book类的构造 Book book = msg.create("Java开发实战经典", 79.8); System.out.println(book); } } |
2.10 内建函数式接口
范例2-53:使用功能型函数式接口——接收参数并且返回一个处理结果。本操作将利用Function接口引用,String类“public boolean startsWith(String str)”方法。
package com.yootk.demo; import java.util.function.Function; public class TestDemo { public static void main(String[] args) { Function<String, Boolean> fun = "##yootk"::startsWith; System.out.println(fun.apply("##")); // 相当于利用对象调用startsWith() } } |
范例2-54:消费型接口。消费型接口主要是接收参数但是不返回数据,所以本次映射System.out. println(String str)方法。
package com.yootk.demo; import java.util.function.Consumer; public class TestDemo { public static void main(String[] args) { Consumer<String> cons = System.out::print; cons.accept("更多课程,请访问:www.yootk.com"); } } |
范例2-55:供给型接口。供给型接口是不接收任何参数的,所以本次引用String类的toUpperCase()方法(public String toUpperCase())。
package com.yootk.demo; import java.util.function.Supplier; public class TestDemo { public static void main(String[] args) { Supplier<String> sup = "yootk"::toUpperCase; System.out.println(sup.get()); } } |
范例2-56:断言型接口。断言型接口主要是进行判断操作,本身需要接收一个参数,同时会返回一个判断结果(boolean型)。本次将引用String类中的equalsIgnoreCase()方法(public boolean equalsIgnoreCase(String str))。
package com.yootk.demo; import java.util.function.Predicate; public class TestDemo { public static void main(String[] args) { Predicate<String> pre = "yootk"::equalsIgnoreCase; System.out.println(pre.test("YOOTK")); } } |