Core Java (14) interface, object cloning, callback

Source: Internet
Author: User
Tags comparable date now
Interface

The interface technology is used to describe the functions of a class, rather than the specific implementation of each function.

A class can implement one or more interfaces, and use the objects that implement the corresponding interfaces at any time where excuses are needed.

To enable a class to implement an interface, you usually need:

  1. Declare a class to implement a given interface
  2. Define all methods in the interface
In the following example, compareto is implemented:
Package COM. xujin; public class main {public static void main (string... ARGs) {employee e = new employee ("Lily", 1000); system. out. print (E. compareto (new employee ("Bob", 2000); // 1} class employee implements comparable <employee> {public employee (string name, double salary) {This. name = Name; this. salary = salary;} public int compareto (employee other) {If (salary> Other. salary) Return-1; if (salary <Other. salary) return 1; return 0;} // defines the variable private double salary; private string name ;}

Interface features. An interface is not a class and cannot be instantiated using new. However, you can declare the interface variable, comparable x = new employee ();

Comparable x = new employee ("Jim", 4000); // here X is a system of the employee type. out. println (X. getclass (). getname (); // COM. xujin. employee

In this way, the created X is of the employee type.

The interface can contain non-static method (public) and static instance domain (public static final ). Although each class can inherit only one superclass, it can implement multiple interfaces. Java uses interfaces to implement most of the functions of Multi-inheritance.
class Employee implements Cloneable, Comparable{......}

Object cloning deep copy: Object cloning, also known as deep copy. A new object is created, and the state of the new object is the same as that of the original object. When the new object is modified, the status of the original object is not affected. Shortest: When copying a variable, the original variable and the copy variable reference the same object. This is a shortest copy, and the Copied object will still have an impact on the original object. Only one class implements the cloneable interface can call the clone method clone. In the following example, staff is a small copy, while copy is a deep copy.

Package COM. xujin; public class main {public static void main (string... ARGs) throws clonenotsupportedexception {Employee A = new employee ("Lily", 1000); employee Staff = A; // both A and staff reference the same object. setname ("Bob"); system. out. println (a); // COM. xujin. employee [name = Bob, salary = 1000.0] system. out. println (staff); // COM. xujin. employee [name = Bob, salary = 1000.0] employee copy =. clone ();. setname ("Jim");. setsalary (2000); system. out. println (a); // COM. xujin. employee [name = Jim, salary = 2000.0] system. out. println (copy); // COM. xujin. employee [name = Bob, salary = 1000.0]} class employee implements cloneable {public employee (string name, double salary) {This. name = Name; this. salary = salary;} Public String getname () {return this. name;} public double getsalary () {return this. salary;} public void setname (string name) {This. name = Name;} public void setsalary (double salary) {This. salary = salary;} Public String tostring () {return getclass (). getname () + "[name =" + name + ", salary =" + salary + "]";} public employee clone () throws clonenotsupportedexception {return (employee) super. clone () ;}// define the variable private double salary; private string name ;}
Interfaces and callback are a common program design mode. You can specify the actions to be taken when a specific event occurs. In the following example, Timer t can run the actionreceivmed method in the listen listener at a given interval.
ackage com.xujin;import java.awt.Toolkit;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import java.util.Date;import javax.swing.JOptionPane;import javax.swing.Timer;public class Main{public static void main(String...args){ActionListener listener = new TimePrinter();Timer t = new Timer(1000, listener);t.start();JOptionPane.showMessageDialog(null, "Quit?");System.exit(0);}}class TimePrinter implements ActionListener{public void actionPerformed(ActionEvent event){Date now = new Date();System.out.println("At the tone,the time is " + now);Toolkit.getDefaultToolkit().beep();}}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.