Late Binding and Email Handling Examples

slide1 n.w
1 / 35
Embed
Share

"Explore examples of late binding, email handling, and downcasting in Java with classes like Email, RichEmail, and QuantityItem. Learn how to forward emails and save attachments effectively."

  • Java Programming
  • Email Handling
  • Late Binding
  • Downcasting

Uploaded on | 0 Views


Download Presentation

Please find below an Image/Link to download the presentation.

The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author. If you encounter any issues during the download, it is possible that the publisher has removed the file from their server.

You are allowed to download the files provided on this website for personal or commercial use, subject to the condition that they are used lawfully. All files are the property of their respective owners.

The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author.

E N D

Presentation Transcript


  1. Quiz class Item { private String name; public Item(String name){ this.name = name; } } class QuantityItem extends Item { private int quantity; public QuantityItem(int quantity, String name){ this.quantity = quantity; } } class Quiz { public static void main(String[] args){ QuantityItem A = new Item("item A"); Item B = new QuantityItem("Item B", 10); } }

  2. Quiz class Item { private String name; public Item(String name){ this.name = name; } constructor . } class QuantityItem extends Item { private int quantity; public QuantityItem(String name, int quantity){ super(name); this.quantity = quantity; } } . . class Quiz { public static void main(String[] args){ QuantityItem A = new Item("item A"); Item B = new QuantityItem("Item B", 10); } }

  3. lab Email, RichEmail Email public Email forward(String recipient){ Email fwdEmail = new Email(this.recipient, recipient, this.text); return fwdEmail; } sender recipient text read forward getRecipient getText public RichEmail forward(String recipient){ RichEmail fwdEmail = new RichEmail(getRecipient(), recipient, getText()); return fwdEmail; } RichEmail attachment forward saveAttachment

  4. Email , Email, RichEmail, forward RichEmail. class EmailExample { public static void main(String[] args){ Email[] emails = new Email[2]; emails[0] = new Email("Him", "Me", "Hi there"); emails[1] = new RichEmail("Her", "Me", "Hello!", "notes.txt"); readEmails(emails); RichEmail rEmail = (RichEmail)emails[1].forward("You"); rEmail.read(); String fileLocation = rEmail.saveAttachment("My Documents"); System.out.println("File saved at " + fileLocation); } private static void readEmails(Email[] emails){ for (int i =0; i < emails.length; i ++){ emails[i].read(); System.out.println(); } } } Late Binding forward RichEmail. H forward forward Email Email. downcasting. email.

  5. . . (50 ), 10 (40 ), 10 (30 ). 50%. .

  6. Ticket number toString getPrice AdvanceTicket numOfDays getPrice toString StudentAdvance getPrice toString

  7. Ticket number toString getPrice AdvanceTicket WalkInTicket numOfDays getPrice toString getPrice toString StudentAdvance getPrice toString

  8. ? Ticket number toString getPrice AdvanceTicket WalkInTicket numOfDays getPrice toString getPrice toString StudentAdvance StudentWalkIn getPrice toString getPrice toString

  9. ? public abstract double getPrice(); public double finalPrice() { if (student){ return getPrice()*0.5; } return getPrice(); } Ticket number student: boolean toString finalPrice getPrice WalkInTicket AdvanceTicket numOfDays getPrice toString getPrice toString

  10. Stack

  11. public class IntStackElement { private int value; private IntStackElement next = null; public IntStackElement(int value){ this.value = value; } public int getValue(){ return value; } public IntStackElement getNext(){ return next; } public void setNext(IntStackElement element){ next = element; } }

  12. public class IntStack { private IntStackElement head; private int size = 0; public int pop(){ if (size == 0){ // head == null System.out.println("Pop from empty stack"); System.exit(-1); } int value = head.getValue(); head = head.getNext(); size --; return value; } public void push(int value){ IntStackElement element = new IntStackElement(value); element.setNext(head); head = element; size ++; } }

  13. Stack Person Stack StackElement.

  14. class PersonStackElement { private Person value; private PersonStackElement next; public PersonStackElement(Person val){ value = val; } public void setNext(PersonStackElement element){ next = element; } public PersonStackElement getNext(){ return next; } public Person getValue(){ return value; } }

  15. public class PersonStack { private PersonStackElement head; private int size = 0; public Person pop(){ if (size == 0){ // head == null System.out.println("Pop from empty stack"); System.exit(-1); } Person value = head.getValue(); head = head.getNext(); size --; return value; } public void push(Person value){ PersonStackElement element = new PersonStackElement(value); element.setNext(head); head = element; size ++; } }

  16. Stack Stack . ? : ObjectStack Object, ?

  17. class ObjectStackElement { private Object value; private ObjectStackElement next; public ObjectStackElement(Object val){ value = val; } public void setNext(ObjectStackElement element){ next = element; } public ObjectStackElement getNext(){ return next; } public Object getValue(){ return value; } }

  18. public class ObjectStack { private ObjectStackElement head; private int size = 0; public Object pop(){ if (size == 0){ // head == null System.out.println("Pop from empty stack"); System.exit(-1); } Object value = head.getValue(); head = head.getNext(); size --; return value; } public void push(Object value){ ObjectStackElement element = new ObjectStackElement(value); element.setNext(head); head = element; size ++; } }

  19. public class ObjectStackTest { public static void main(String[] args){ ObjectStack stack = new ObjectStack(); Person p = new Person( Alice , 1); Integer i = new Integer(10); String s = a random string ; stack.push(p); stack.push(i); stack.push(s); } } . (downcasting) . .

  20. (Generic) ( ) : public class Example<T> { } Example Example Example<String> ex = new Example<String>();

  21. public class Example<T>{ private T data; ex String public Example(T data){ this.data = data; } constructor < > <String> public void setData(T data){ this.data = data; } public T getData(){ return data; } public static void main(String[] args){ Example<String> ex = new Example<String>( hello world ); System.out.println(ex.getData()); } }

  22. class StackElement<> { private value; private StackElement< > next; public StackElement( val){ value = val; } public void setNext(StackElement< > element){ next = element; } public StackElement< > getNext(){ return next; } public getValue(){ return value; } }

  23. public class Stack<> { private StackElement< > head; private int size = 0; public pop(){ if (size == 0){ // head == null System.out.println("Pop from empty stack"); System.exit(-1); } value = head.getValue(); head = head.getNext(); size --; return value; } public void push( value){ StackElement< > element = new StackElement< >(value); element.setNext(head); head = element; size ++; } }

  24. public class StackTest { public static void main(String[] args){ Stack<Person> personStack = new Stack<Person>(); personStack.push(new Person("Alice", 1)); personStack.push(new Person("Bob",2)); System.out.println(personStack.pop()); System.out.println(personStack.pop()); Stack<Integer> intStack = new Stack<Integer>(); intStack.push(new Integer(10)); intStack.push(new Integer(20)); System.out.println(intStack.pop()); System.out.println(intStack.pop()); Stack<String> stringStack = new Stack<String>(); stringStack.push("string 1"); stringStack.push("string 2"); System.out.println(stringStack.pop()); System.out.println(stringStack.pop()); } } .

  25. public class KeyValuePair<K,V>{ private K key; private V value; }

  26. 1. ( . . int, double, boolean wrapper classes , Integer, Boolean, Double) 2. . . ., StackElement<String>[] A = new StackElement<String>[2]; 3. . . ., obj = new T(); T[] a = new T[10]; ! !

  27. Pair . public class Pair<T>{ private T first; private T second; }

  28. first second. interface myComparable public class Pair<T extends myComparable>{ private T first; private T second; public void order(){ if (first.compareTo(second) > 0){ T temp = first; first = second; second = temp; } } } extends implements

  29. first second. interface Comparable public class Pair<T extends Comparable< >>{ private T first; private T second; public void order(){ if (first.compareTo(second) > 0){ T temp = first; first = second; second = temp; } } } H Comparable< > Java To T

  30. , interface . Employee public class SomeClass public class SomeClass <T extends Employee & Comparable<T>> { } <T extends Employee> { } Employee interface Comparable interfaces ,

  31. . . public class OrderedPair<T> extends Pair<T> { } Pair<Employee> Pair<HourlyEmployee>

  32. Wildcard ?, . public void someMethod(Pair<?>){ } Pair . Employee. public void someMethod( Pair<? extends Employee>){ }

Related


More Related Content