理论知识部分:
1.接口:接口不是类,而是对类 的一组需求描述,由常量和一组抽象方法组成。
接口中不包括变量和有具体实现的方法。接口体中包含常量定义和方法定义,接口中只进 行方法的声明。
只要类实现了接口,则该类要遵从接口描述的统 一格式进行定义,并且可以在任何需要该接口的 地方使用这个类的对象。
声明方式:
public interface 接口名
{ …… }
接口以扩展,扩展方法:
public interface 接口1 extends接口2
{ …… }
可以使用extends来继承接口的常量和抽象方 法,扩展形成新的接口;
接口中的所有常量必须是public static final,方法必须是public abstract,这是系统默认的,
在类声明时用implements关键字声明使用一个或 多个接口
class Employee implementsPrintable
{ …… }
一个类使用了某个接口,那么这个类必须实现该 接口的所有方法,为这些方法提供方法体。
一个类可以实现多个接口,接口间应该用逗号分 隔开。 如:class Employee implements Cloneable,Comparable
若实现接口的类不是抽象类,则必须实现所有 接口的所有方法,即为所有的抽象方法定义方 法体。
2.接口的使用:
接口不能构造接口对象,但可以声明接口变量以 指向一个实现了该接口的类对象。
Comparable x= new Employee(…);
可以用instanceof检查对象是否实现了某个接口。
if (anObjectinstanceofComparable)
{ …… }
3.Comparator接口
所在包:java.util.*
Comparator接口定义
public interface Comparator<T>{
int compare(T o1,T o2);
......
}
用途:处理字符串按长度进行排序的操作。
4.Object类的Clone方法
Object类的clone()方法是一个native方法。
Object类中的clone()方法被protected修饰符修饰。
Object.clone()方法返回一个Object对象。
5.浅层拷贝:被拷贝对象的所有常量成员和基本类 型属性都有与原来对象相同的拷贝值,而若成员 域是一个对象,则被拷贝对象该对象域的对象引 用仍然指向原来的对象。
6.深层拷贝:被拷贝对象的所有成员域都含有与原 来对象相同的值,且对象域将指向被复制过的新对 象,而不是原有对象被引用的对象。换言之,深 层拷贝将拷贝对象内引用的对象也拷贝一遍。
7.Lambda表达式本质上是一个匿名方法。 public intadd(intx, inty) { return x + y; } 转成Lambda表达式后是这个样子: (intx, inty) -> x + y;
8.Lambda 表达式的语法基本结构
(arguments) -> body
9.内部类(inner class)是定义在一个类内部的类。
外层的类成为外部类(outer class)。
内部类主要用于事件处理。
内部类的声明格式如下:
[修饰符] class outerClass{
…
[修饰符] class innerClass{
…
}
…
}
实验部分:
实验内容和步骤:
实验1:
测试程序1:
EmployeeSortTest.java
package interfaces; import java.util.*; /** * This program demonstrates the use of the Comparable interface. * @version 1.30 2004-02-27 * @author Cay Horstmann */ public class EmployeeSortTest { public static void main(String[] args) { Employee[] staff = new Employee[3]; staff[0] = new Employee("Harry Hacker", 35000); staff[1] = new Employee("Carl Cracker", 75000); staff[2] = new Employee("Tony Tester", 38000); Arrays.sort(staff);//静态方法,按工资高低排序 // 打印所有员工信息 for (Employee e : staff) System.out.println("name=" + e.getName() + ",salary=" + e.getSalary()); } }
Employee.java
package interfaces; public class Employee implements Comparable<Employee> { private String name; private double salary; public Employee(String name, double salary) { this.name = name; this.salary = salary; } public String getName() { return name; } public double getSalary() { return salary; } public void raiseSalary(double byPercent) { double raise = salary * byPercent / 100; salary += raise; } /** * Compares employees by salary * @param other another Employee object * @return a negative value if this employee has a lower salary than * otherObject, 0 if the salaries are the same, a positive value otherwise */ public int compareTo(Employee other)//用雇员对象与other进行比较 { return Double.compare(salary, other.salary); } }
输出结果:
测试程序2:
package demo; interface A { double g=9.8; void show( ); } class C implements A { public void show( ) {System.out.println("g="+g);} } class InterfaceTest { public static void main(String[ ] args) { A a=new C( ); a.show( ); System.out.println("g="+C.g); } }
输出结果:
测试程序3:
package timer; /** @version 1.01 2015-05-12 @author Cay Horstmann */ import java.awt.*; import java.awt.event.*; import java.util.*; import javax.swing.*; import javax.swing.Timer; //消除javax.swing.Timer与java.util.timer之间产生的二义性 public class TimerTest { public static void main(String[] args) { ActionListener listener = new TimePrinter(); Timer t = new Timer(10000, listener);//构造一个定时器,每1000毫秒通告listener一次 t.start();//启动定时器 JOptionPane.showMessageDialog(null, "Quit program?");//显示一个包含一条消息和OK按钮的对话框 System.exit(0); } } class TimePrinter implements ActionListener//定义实现一个ActionListener接口的类 { public void actionPerformed(ActionEvent event) { System.out.println("At the tone, the time is " + new Date()); Toolkit.getDefaultToolkit().beep();//获得默认工具箱。发出一声响铃 } }
输出结果:
测试程序4:
CloneTest.java
package clone; /** * This program demonstrates cloning. * @version 1.10 2002-07-01 * @author Cay Horstmann */ public class CloneTest { public static void main(String[] args) { try { Employee original = new Employee("John Q. Public", 50000); original.setHireDay(2000, 1, 1); Employee copy = original.clone(); copy.raiseSalary(10); copy.setHireDay(2002, 12, 31); System.out.println("original=" + original); System.out.println("copy=" + copy); } catch (CloneNotSupportedException e) { e.printStackTrace(); } } }
Employee.java
package clone; import java.util.Date; import java.util.GregorianCalendar; public class Employee implements Cloneable { private String name; private double salary; private Date hireDay; public Employee(String name, double salary) { this.name = name; this.salary = salary; hireDay = new Date(); } public Employee clone() throws CloneNotSupportedException { // Object类对象的clone方法 Employee cloned = (Employee) super.clone(); // clone mutable fields cloned.hireDay = (Date) hireDay.clone(); return cloned; } /** * Set the hire day to a given date. * @param year the year of the hire day * @param month the month of the hire day * @param day the day of the hire day */ public void setHireDay(int year, int month, int day) { Date newHireDay = new GregorianCalendar(year, month - 1, day).getTime(); // Example of instance field mutation hireDay.setTime(newHireDay.getTime()); } public void raiseSalary(double byPercent) { double raise = salary * byPercent / 100; salary += raise; } public String toString() { return "Employee[name=" + name + ",salary=" + salary + ",hireDay=" + hireDay + "]"; } }
输出结果:
测试程序4:
package lambda; import java.util.*; import javax.swing.*; import javax.swing.Timer; /** * This program demonstrates the use of lambda expressions. * @version 1.0 2015-05-12 * @author Cay Horstmann */ public class LambdaTest { public static void main(String[] args) { String[] planets = new String[] { "Mercury", "Venus", "Earth", "Mars", "Jupiter", "Saturn", "Uranus", "Neptune" }; System.out.println(Arrays.toString(planets)); System.out.println("Sorted in dictionary order:"); Arrays.sort(planets); System.out.println(Arrays.toString(planets)); System.out.println("Sorted by length:"); Arrays.sort(planets, (first, second) -> first.length() - second.length()); System.out.println(Arrays.toString(planets)); Timer t = new Timer(1000, event -> System.out.println("The time is " + new Date())); t.start(); // 保持程序运行,直至用户选择OK JOptionPane.showMessageDialog(null, "Quit program?"); System.exit(0); } }
实验3: 编程练习
Identify.java:
package 第八周实验; import java.io.BufferedReader; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; import java.io.InputStreamReader; import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; import java.util.Scanner; public class Identify { private static ArrayList<Person> personlist; public static void main(String[] args) { personlist = new ArrayList<>(); Scanner scanner = new Scanner(System.in); File file = new File("E:\身份证号.txt"); try { FileInputStream fis = new FileInputStream(file); BufferedReader in = new BufferedReader(new InputStreamReader(fis)); String temp = null; while ((temp = in.readLine()) != null) { Scanner linescanner = new Scanner(temp); linescanner.useDelimiter(" "); String name = linescanner.next(); String number = linescanner.next(); String sex = linescanner.next(); String age = linescanner.next(); String hometown = linescanner.nextLine(); Person person = new Person(); person.setName(name); person.setnumber(number); person.setsex(sex); int A = Integer.parseInt(age); person.setage(A); person.sethometown(hometown); personlist.add(person); } } catch (FileNotFoundException e) { System.out.println("身份信息文件找不到"); e.printStackTrace(); } catch (IOException e) { System.out.println("身份信息文件读取错误"); e.printStackTrace(); } boolean isTrue = true; while (isTrue) { System.out.println("0.按姓名字典序输出人员信息;"); System.out.println("1.查询最大年龄人员信息;;"); System.out.println("2.查询最小年龄人员信息;"); System.out.println("3.寻找同乡;"); System.out.println("4.寻找年龄相近的人;"); System.out.println("5.退出。"); String W = scanner.next(); switch (W) { case "0": Collections.sort(personlist); System.out.println(personlist.toString()); break; case "1": int a = 0; int j, c1 = 0, d1 = 0; for (int i = 1; i < personlist.size(); i++) { j = personlist.get(i).getage(); if (j > a) { a = j; c1 = i; } } System.out.println("年龄最大:" + personlist.get(c1)); break; case "2": int b = 100; int c2 = 0,d2 = 0; for (int i = 1; i < personlist.size(); i++) { j = personlist.get(i).getage(); if (j < b) { b = j; d2 = i; } } System.out.println("年龄最小:" + personlist.get(d2)); break; case "3": System.out.println("籍贯:"); String search = scanner.next(); String place = search.substring(0, 3); int i = 0; for (; i < personlist.size(); i++) { if (personlist.get(i).gethometown().substring(1, 4).equals(place)) System.out.println("你的同乡是:" + personlist.get(i)); } break; case "4": System.out.println("年龄:"); int yourage = scanner.nextInt(); int nearaga = agenear(yourage); int value = yourage - personlist.get(nearaga).getage(); System.out.println("" + personlist.get(nearaga)); break; case "5": isTrue = false; System.out.println("退出程序!"); break; default: System.out.println("检查输入!"); } } } public static int agenear(int age) { int j = 0, b = 53, value = 0, c = 0; for (int i = 0; i < personlist.size(); i++) { value = personlist.get(i).getage() - age; if (value < 0) value = -value; if (value < b) { b = value; c = i; } } return c; } }
Person.java
package 第八周实验; public class Person implements Comparable<Person> { private String name; private String number ; private String sex ; private int age; private String hometown; public String getName() { return name; } public void setName(String name) { this.name = name; } public String getnumber() { return number; } public void setnumber(String number) { this.number = number; } public String getsex() { return sex ; } public void setsex(String sex ) { this.sex =sex ; } public int getage() { return age; } public void setage(int age) { this.age= age; } public String gethometown() { return hometown; } public void sethometown(String hometown) { this.hometown=hometown ; } public int compareTo(Person o) { return this.name.compareTo(o.getName()); } public String toString() { return name+" "+sex+" "+age+" "+number+" "+hometown+" "; } }
输出结果:
实验4:
实验程序1:
package innerClass; import java.awt.*; import java.awt.event.*; import java.util.*; import javax.swing.*; import javax.swing.Timer; /** * This program demonstrates the use of inner classes. * @version 1.11 2015-05-12 * @author Cay Horstmann */ public class InnerClassTest { public static void main(String[] args) { TalkingClock clock = new TalkingClock(1000, true); clock.start(); // 保持程序运行,直至用户选择"Ok" JOptionPane.showMessageDialog(null, "Quit program?"); System.exit(0); } } /** * A clock that prints the time in regular intervals. */ class TalkingClock { private int interval; private boolean beep; /** * Constructs a talking clock * @param interval the interval between messages (in milliseconds) * @param beep true if the clock should beep */ public TalkingClock(int interval, boolean beep) { this.interval = interval; this.beep = beep; } /** * Starts the clock. */ public void start() { ActionListener listener = new TimePrinter(); Timer t = new Timer(interval, listener); t.start(); } public class TimePrinter implements ActionListener { public void actionPerformed(ActionEvent event) { System.out.println("At the tone, the time is " + new Date()); if (beep) Toolkit.getDefaultToolkit().beep(); } } } InnerClassTest
输出结果:
实验程序2:
package anonymousInnerClass; import java.awt.*; import java.awt.event.*; import java.util.*; import javax.swing.*; import javax.swing.Timer; /** * This program demonstrates anonymous inner classes. * @version 1.11 2015-05-12 * @author Cay Horstmann */ public class AnonymousInnerClassTest { public static void main(String[] args) { TalkingClock clock = new TalkingClock(); clock.start(1000, true); // 保持程序运行,直至用户选择"Ok" JOptionPane.showMessageDialog(null, "Quit program?"); System.exit(0); } } /** * A clock that prints the time in regular intervals. */ class TalkingClock { /** * Starts the clock. * @param interval the interval between messages (in milliseconds) * @param beep true if the clock should beep */ public void start(int interval, boolean beep) { ActionListener listener = new ActionListener() { public void actionPerformed(ActionEvent event) { System.out.println("At the tone, the time is " + new Date()); if (beep) Toolkit.getDefaultToolkit().beep(); } }; Timer t = new Timer(interval, listener); t.start(); } }
输出结果:
实验程序3:
package staticInnerClass; /** * This program demonstrates the use of static inner classes. * @version 1.02 2015-05-12 * @author Cay Horstmann */ public class StaticInnerClassTest { public static void main(String[] args) { double[] d = new double[20]; for (int i = 0; i < d.length; i++) d[i] = 100 * Math.random(); ArrayAlg.Pair p = ArrayAlg.minmax(d); System.out.println("min = " + p.getFirst()); System.out.println("max = " + p.getSecond()); } } class ArrayAlg { /** * A pair of floating-point numbers */ public static class Pair { private double first; private double second; /** * Constructs a pair from two floating-point numbers * @param f the first number * @param s the second number */ public Pair(double f, double s) { first = f; second = s; } /** * Returns the first number of the pair * @return the first number */ public double getFirst() { return first; } /** * Returns the second number of the pair * @return the second number */ public double getSecond() { return second; } } /** * Computes both the minimum and the maximum of an array * @param values an array of floating-point numbers * @return a pair whose first element is the minimum and whose second element * is the maximum */ public static Pair minmax(double[] values) { double min = Double.POSITIVE_INFINITY; double max = Double.NEGATIVE_INFINITY; for (double v : values) { if (min > v) min = v; if (max < v) max = v; } return new Pair(min, max); } }
输出结果:
总结:
接口与抽象类的区别有:
接口不能实现任何方法,而抽象类可以。类可以实现许多接口,但只有一个父类。接口不是类分级结构的一部分,无任何联系的类可以实现相同的接口。抽象类是用abstract来声明,没有具体实例对象的类,不 能用new来创建对象。可包含常规类所包含的任何东西。 抽象类必须由子类继承,如果abstract类的子类不是抽 象类,那么子类必须重写父类中所有的abstract方法。接口是用interface声明,是抽象方法和常量值定义的集 合。从本质上讲,接口是一种特殊的抽象类,这种抽象 类中只包含常量和方法的定义,而没有变量和方法的定义。接口中只能定义抽象方法,而且这些方法默认为是public的。只要类实现了接口,就可以在任何需要该接口的地方使用这个类的对象。
拷贝和克隆:
拷贝:当拷贝一个变量时,原始变量与拷贝变量引用的是同一个对象。当改变一个变量所引用的对象,则会对另一个变量造成影响。
克隆:当克隆一个对象时,是重新的创建了和该对象内容相同的对象。在对象中如果包含了子对象的引用,拷贝的结果就会使得两个域引用同一个子对象,此时,原始对象和克隆对象将共享这一部分信息。这样,当克隆对象改变这部分时,就会造成原始对象中数据的改变。
我还学习到,在理解一些知识点时,可以通过设计测试程序来帮助自己理解,虽然我对于自己设计测试程序还没有掌握,但是我会向助教老师学习这种良好的习惯。