反射机制写泛型数组
package resources;
import java.lang.reflect.*;
import java.util.*;
public class CopyOftest {//反射编写泛型数组
public static void main(String[] args) {
// TODO Auto-generated method stub
int[] a = {1,2,3};
a = (int[]) goodCopyOf(a,10);
System.out.println(Arrays.toString(a));
String[] b = {"Tom", "Dick", "Harry"};
b= (String[]) goodCopyOf(b, 10);
System.out.println(Arrays.toString(b));
}
public static Object[] badCopyOf(Object[] a, int newLength)
{
var newArray = new Object[newLength];
System.arraycopy(a, 0, newArray, 0,Math.min(a.length, newLength));
return newArray;
}
public static Object goodCopyOf(Object a, int newLength)
{
Class cl = a.getClass();
if(!cl.isArray()) return null;
Class componentType = cl.getComponentType();
int length = Array.getLength(a);
Object newArray = Array.newInstance(componentType, newLength);
System.arraycopy(a, 0, newArray, 0, Math.min(length, newLength));
return newArray;
}
}
Comparator接口:完成对字符串数组按照字符长度排序
package resources;
import java.util.*;
public class CopyOftest {//反射编写泛型数组
public static void main(String[] args) {
// TODO Auto-generated method stub
String[] friends = {"Ken", "Peter", "Mary", "Jordan", "Marior", "bossshp", "An"};
var comp = new LengthComparator();
Arrays.sort(friends, new LengthComparator());
for (String e: friends)
{
System.out.println(e);
}
}
//书上直接写的是class,即由默认为public class,但是实操会报错,No enclosing instance of type CopyOftest is accessible...查了一下,是因为静态类不能调用动态的内部类,具体原因不是太清楚,内部类还没看到。
//或者是将LengthComparator类放在test外更简单
public static class LengthComparator implements Comparator<String>
{
public int compare(String first, String second)
{
return first.length() - second.length();
}
}
}
枚举类
package hellofirst;
import java.util.*;
public class EnumTest {
public static void main(String[] args) {
// TODO Auto-generated method stub
var in = new Scanner(System.in);
System.out.println("Enter a size: (SMALL, MEDIUM, LARGE, EXTRA_LARGE)");
String input = in.next().toUpperCase();
Size size = Enum.valueOf(Size.class, input);
System.out.println("size= " + size);
System.out.println("abbreviation= " + size.getAbbreviation());
if (size == Size.EXTRA_LARGE)
{
System.out.println("Good job--you paid attention to the _");
}
}
}
enum Size
{
SMALL("S"), MEDIUM("M"), LARGE("L"), EXTRA_LARGE("XL");
private String abbreviation;
private Size(String abbreviation) {this.abbreviation = abbreviation;}
public String getAbbreviation() {return abbreviation;}
}
回调:实现简单的定时器接口
package Timer;
import java.awt.*;
import java.awt.event.*;
import java.time.*;
import javax.swing.*;
public class TimerTest {
public static void main(String[] args) {
// TODO Auto-generated method stub
var Listener = new TimePrinter();
var timer = new Timer(1000, Listener);
timer.start();
JOptionPane.showMessageDialog(null, "Quit program?");
System.exit(0);
}
}
class TimePrinter implements ActionListener
{
public void actionPerformed(ActionEvent event)
{
System.out.println("At the tone, the time is" + Instant.ofEpochMilli(event.getWhen()));
Toolkit.getDefaultToolkit().beep();
}
}
匿名内部类:
package secTest;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.time.*;
import javax.swing.*;
public class LambdaTest {
public static void main(String[] args)
{
var clock = new TalkingClock();
clock.start(1000, true);
JOptionPane.showMessageDialog(null, "Quit program?");
System.exit(0);
}
}
class TalkingClock
{
public void start(int interval, boolean beep)
{
//匿名内部类部分,比直接的内部类要简明,但是最好还是用lambda表达式
var listener = new ActionListener()
{
public void actionPerformed(ActionEvent event)
{
System.out.println("At this tone, the time is " + Instant.ofEpochMilli(event.getWhen()));
if(beep) Toolkit.getDefaultToolkit().beep();
}
};//不要忘记这个分号,跟cpp有个地方很像,想不起来了一下子。。。
var timer = new Timer(interval, listener);
timer.start();
}
}
上面的lambda表达式如下
var timer = new Timer(interval, event -> {
System.out.println("...");if(beep) ...;}
);