Sponsored Link •
|
Advertisement
|
Adjective interface: Way to do callbacks. Include performance of these kinds of services in my contract.
interface
sinterface
s to say "What objects can
do" or "What can be done to an object."
interface
s don't say "What objects are."
interface
s say what objects can do no matter what
objects are
interface
ExamplesComparable
means: an object can compare
itself with another object
of the same type if I invoke compareTo()
on the
object
public interface Comparable { int compareTo(Object o); }
Cloneable
means: I can clone an object (meta-semantics)
public interface Cloneable { }
Comparable
also means: I can sort an array or collection
with Arrays.sort()
or Collections.sort()
(meta-semantics)
interface
s out of Thin Airinterface
s
Sum (k=0 to n) n! / (n - k)! k!, where n = num methods
interface
s to accept as parameters to, or provide
as return types of, your class's methods
interface
parameter types: your class/interface more versatile
interface
return types: your class/interface easier to evolve, subclass
OverdraftAccount
interface
s arise when you define the semantic contract
of a class (define how the class will be used)
OverdraftAccount
gets overdrawn (or paid back)?
1 // In file ex3/OverdraftAccount.java 2 3 public class OverdraftAccount extends Account { 4 //... 74 75 public void addOverdraftListener(OverdraftListener l) { 76 //... 77 } 78 79 public void removeOverdraftListener( 80 OverdraftListener l) { 81 //... 82 } 83 84 } 1 // In file ex3/OverdraftEvent.java 2 3 public class OverdraftEvent 4 extends java.util.EventObject { 5 6 private long overdraft; 7 8 // overdraft is the current value of overdraft 9 // in US pennies. overdraft will be either some 10 // positive number or zero. If zero, it indicates 11 // that the overdraft has just been paid back. 12 // If positive, it means that the account was just 13 // overdrawn. (If $50 has been loaned to the 14 // account holder, overdraft will be 5000) 15 public OverdraftEvent(Object source, long overdraft) { 16 17 super(source); 18 this.overdraft = overdraft; 19 } 20 21 public long getOverdraft() { 22 return overdraft; 23 } 24 25 } 1 // In file ex3/OverdraftListener.java 2 3 public interface OverdraftListener 4 extends java.util.EventListener { 5 6 void overdraftOccurred(OverdraftEvent e); 7 8 void overdraftRepaid(OverdraftEvent e); 9 10 }
Sponsored Links
|