????? ?? - PowerPoint PPT Presentation

About This Presentation
Title:

????? ??

Description:

Title: 1. - Last modified by: zcp30 Created Date: 8/28/2000 6:52:12 AM Document presentation format: – PowerPoint PPT presentation

Number of Views:29
Avg rating:3.0/5.0
Slides: 51
Provided by: tist242
Category:
Tags: interface

less

Transcript and Presenter's Notes

Title: ????? ??


1
1. ??-?? ?????
2
1.1 ??
  • ????? ??
  • ??? ??
  • ???
  • ?? ??
  • ?? ?? ?????
  • ???? ??? ???? ??
  • ?? ???? ??? ?? ???? ?? ???? ??

3
?? ??
  • ??? ???? ???(??)
  • ? ??? ??
  • ????? ?? ????
  • ??? ??? ???? ??

4
????? ??? ??? ??
5
  • ??? ??
  • ??? ?? ??? ??? ?? ?? ???? ??? ??? ?? ?? ?? ??
  • ??? ?? ?? ??, ?? ??, ??? ?? ?? ??
  • ???? ??
  • ?? ??? ???? ? ??? ???? ???? ?? ??
  • ??? ??? ?? ??
  • ???? ??
  • ????? ???? ???? ?? ??
  • ?? ???? ??, ? ????? ??? ?? ??
  • ??
  • ? ????? ?? ?? ??? ?? ??
  • ???
  • ?? ?? ??? ??? ?? ??? ???
  • ????
  • ??? ??, ???(??? ?), ??? ??

6
????? ?? 
??? ???? ???? ?? ? ???
7
  • ?? ?? ?? ???
  • ??? ??
  • ??? ??? ??, ? ???? ??
  • ?? ??? ??
  • ???? ??
  • Keep it simple, stupid ?? ??
  • ????? ??? ?? ?? ???? ???? Java ???? ??
  • ?? ??? args ??? ??
  • Java Convert lt??gt lt??gt
  • Netbeans IDE choose project click right mouse
    choose propertied choose run category put
    input values in arguments field
  • ??? ??? ??

8
Convert ????? ???
9
?? ?? ???? ??? ???
10
1.2 ??-?? ??
  • ??, ???, ????? ??
  • Java? ??? ?? ????? ??
  • ?? ?? ????? -gt ???
  • ?? ??? Java ?????? ??
  • ?? ?? ? ??? 1.1
  • ???
  • / C ??? /
  • // C ???
  • / Javadoc ??? /
  • ?? ????? Javadoc ?? ????? ??? ??
  • xx.java ??? xx.html ??? ??
  • Javadoc ? ??? java ??(?????? ???)? ? ? ?? ?? ???
  • ??? ??? ??? ??? ???? ??

11
?? ?? ??? ?? Java ?????
  • 1 /2 An interface for representing
    temperatures, with functionality3 for
    converting their values between Celsius and
    Fahrenheit.4 _at_author John R. Hubbard5 _at_see
    MyTemperature6 /8 public interface Temperature
    9 / _at_return the Celsius value for this
    temperature. / 10 public double
    getCelsius()12 / _at_return the Fahrenheit
    value for this temperature. / 13 public double
    getFahrenheit()15 / _at_param celsius the
    Celsius value for this temperature. /16
    public void setCelsius(double celsius)18 /
    _at_param fahrenheit the Fahrenheit value for this
    temperature./19 public void
    setFahrenheit(double fahrenheit)20

12
??? 1.1?? ??? Javadoc ??
13
  • ?? ?? ???? ????
  • ?? ??
  • F 9C/5 32
  • C 5(F-32)/9
  • ???? 1.1, 1.2

14
1.3 ????? ??? ??
  • ?? ?? ?? ?? ?? ?? ??
  • ??? ??? ?? 1.5
  • ??? ?? ???? 1.1
  • ?? ?? ???? 1.2
  • Temperature ????? ??? 1.1
  • ?? ?? ? ????? ???? ?? ??? ???? ??? ?
  • Temperature ??? ???
  • double ??? private ???? ??
  • ??? ?? ??? ?? ?
  • 4? ???, ???, toString(), round() ???? ??
  • ?? ??? 1.2

15
Temperature ?????? ??
  • LISTING 1.2 An Implementation of the Temperature
    Interface
  • 1 public class MyTemperature implements
    Temperature
  • 2 private double celsius // stores temperature
    as a Celsius value
  • 3
  • 4 public MyTemperature(double value, char
    scale)
  • 5 if (scale'C') setCelsius(value)
  • 6 else setFahrenheit(value)
  • 7
  • 8
  • 9 public double getCelsius()
  • 10 return celsius
  • 11
  • 12
  • 13 public double getFahrenheit()
  • 14 return 9celsius/5 32.0
  • 15

16
  • 17 public void setCelsius(double celsius)
  • 18 this.celsius celsius
  • 19
  • 21 public void setFahrenheit(double fahrenheit)
  • 22 this.celsius 5(fahrenheit - 32)/9
  • 23
  • 25 public String toString()
  • 26 // Example "25.0 C 77.0 F"
  • 27 return round(getCelsius()) " C "
    round(getFahrenheit()) " F"
  • 28
  • 30 private double round(double x)
  • 31 // returns x, rounded to one digit on the
    right of the decimal
  • 32 return Math.round(10x)/10.0
  • 33
  • 34

17
1.4 ???? ???
  • ?? ?????? ?? ??? ??? ??
  • ??? ?? ??? ??? ?? ??? ??? ??
  • ??? ?? ? ???? ???? ??? ?? ??
  • ??? ???? ????
  • ??? 1.3

18
Temperature ????? ?? ???? ???
  • LISTING 1.3 Testing the Solution to the
    Temperature Problem
  • 1 public class Convert
  • 2 public static void main(String args)
  • 3 if (args.length!2) exit()
  • 4 double value Double.parseDouble(args0)
    // convert string
  • 5 char scale Character.toUpperCase(args1.cha
    rAt(0))
  • 6 if (scale ! 'C' scale ! 'F') exit()
  • 7 Temperature temperature new
    MyTemperature(value,scale)
  • 8 System.out.println(temperature)
  • 9
  • 11 private static void exit()
  • 12 // prints usage message and then
    terminates the program
  • 13 System.out.println(
  • 14 "Usage java Convert lttemperaturegt
    ltscalegt"
  • 15 "\nwhere"
  • 16 "\tlttemperaturegt is the temperature
    that you want to convert"
  • 17 "\n\tltscalegt is either \"C\" or \"F\"."
  • 18 "\nExample java Convert 67 F"
  • 19 )

19
  • ??(stub)
  • ?? ??? ?? ? ??? ???? ?? ?? ??
  • ???? ??? ? ??? ???? ??? ??? ??? ??? ?? ??? ????
    ??? ????
  • ? ??? ??? ? (x,y)? ???? ???? ????
  • ??? ?? ???? ?? ??? 1.4
  • ???? toString() ??? ??? ?? ???? ??? ??
  • 12?? ?? ??? ????? ??
  • void ??? ? ??
  • ?-void ??? return ?? ??
  • ? ??? ???? ???? ???? ???
  • ???? ?? ???? ???? ???? ?? ????? ??? -gt ?? ????
    ??? ? ??? ? ????? ??

20
  • Point(double, double)           // ? (x,y)? ??
  • void add(double, double)      // ??? ?? (dx,dy)?
    ??
  • void add(Point)                 // ??? ?? ??
  • double amplitude()              // ??? ?? ??
  • boolean equals(Object)        // Object.equals()
    ???? ?????
  • void expand(double)            // ??? ??? ??
  • double x()                      // x-??? ??
  • double y()                      // y-??? ??
  • double modulus()               // (0,0)? ?? ???
    ??
  • void moveTo(double, double)   // ??? (x,y) ??? ??
  • void moveTo(Point)            // ??? ??? ??
  • void rotate(double)             // ??? ??? ? ??
    ??
  • String toString()                //
    Object.toString() ???? ?????

21
??? ?? Point ???? ??
  • LISTING 1.4 Implementing a Point Class with
    Stubs
  • 1 public class Point
  • 2 private double x, y
  • 3 public static final Point ORIGIN new
    Point()
  • 5 private Point()
  • 8 public Point(double x, double y)
  • 9 this.x x
  • 10 this.y y
  • 13 public void add(double u, double v)
  • 14 public void add(Point p)
  • 15 public double amplitude() return 0.0
  • 16 public static double distance(Point p,
    Point q) return 0.0
  • 17 public boolean equals(Object object)
    return false
  • 18 public void expand(double c)
  • 19 public double x() return 0.0
  • 20 public double y() return 0.0
  • 21 public double modulus() return 0.0
  • 22 public void moveTo(double x, double y)
  • 23 public void moveTo(Point p)

22
1.5 ????? ??? ???
  • ??(prime number)? ?? ????
  • ??
  • 1?? ? ??? 1? ? ?? ???? ??(divisor)? ?? ?
  • 2, 3, 5, 7, 11, 13, 17, 19, 23, 29,
  • ??? ?? ???? ???? 1.3
  • ??
  • ??? 1.5
  • ???
  • 1?? 30??? ?? ?? ???
  • 1?? 100??? ?? ?? ???
  • ???(boundary value)
  • ?? ???? ?????? ?? ???? ?? ??? ?
  • ?? ??? ???? ???, ?? ????? ?? ???, null ?? ?
  • ???? ??? ??? ???? ??? 1.6

23
??? ?? ????
  • ? ???? 1.3 ??
  • ---------------------------------
  • ?? ?? n
  • ?? n? ???? ?? ??
  •    1. nlt2?? ??? ??
  •    2. nlt4?? ?? ??
  •    3. n? ???? ??? ??
  •    4. 3?? n??? ?? ?? d? ?? ?? 5? ??
  •    5. n? d? ?? ? ??? ??? ??
  •    6. ?? ??
  • ??(prime number) ??
  • 1 ?? ? ??? 1? ? ?? ???? ??(divisor)? ?? ?
  • ?? 10?? ??? 2, 3, 5, 7, 11, 13, 17, 19, 23, 29??.

24
?? ??
  • LISTING 1.5 Finding Prime Numbers
  • 1 public static boolean isPrime(int n)
  • 2 if (n lt 2) return false
  • 3 if (n lt 4) return true
  • 4 if (n2 0) return false
  • 5 for (int d3 dd lt n d 2)
  • 6 if (nd 0) return false
  • 7 return true
  • 8

25
?? ????? ???
  • LISTING 1.6 Testing the Prime Number Algorithm
  • 1 public class TestPrimeAlgorithm
  • 2 public static void main(String args)
  • 3 for (int n-10 n lt 100 n)
  • 4 if (isPrime(n)) System.out.print(n " ")
  • 5
  • 6
  • 7 public static boolean isPrime(int n)
  • 8 // See Listing 1.5 on page 14.
  • 9
  • 10

26
UML
  • UML (Unified Modeling Language)
  • ?? ?? ?????? ????? ?? ??? ?? ??
  • OMG (Object Management Group)? ??? 1997?? ?????
    ??
  • ???? ???? ?????
  • 3? ???? ?? ??? ??, ???, ????
  • ?? ?? (public), -(private), (protected)
  • ??? ?? ??(???)? ??
  • ??? ??? ??(??)? ??
  • ? Person ???
  • UML ????? ?? 1.7
  • ??? ?? ??? 1.7
  • ? ?? ?? ???? ?????
  • Person ???? Car ??? ?? ?? 1.9

27
Person ???? ?? UML ??
28
Person ???
  • 1 public class Person
  • 2 protected boolean male
  • 3 protected String name
  • 4 protected Phone phone
  • 5 protected int yob
  • 7 public Person(String name, boolean male, int
    yob)
  • 8 this.name name
  • 9 this.male male
  • 10 this.yob yob 11
  • 13 public String getName()
  • 14 return name 15
  • 17 public Phone getPhone()
  • 18 return phone 19
  • 21 public int getYob()
  • 22 return yob 23
  • 25 public boolean isMale()
  • 26 return male 27
  • 29 public void setPhone(Phone phone)
  • 30 this.phone phone 31

29
UML ????
??? ??
30
Person ???? Car ???
31
1.7 ??, ??, ??
  • ???? ?? ???? ???? ? ? ?? ??
  • ?? (inheritance)
  • ?? ??? ??(specialization)
  • ? ? is-a ????, ?? is-a ??, ? is-a ????
  • ?? (aggregation)
  • ?? ???? ???? ???
  • ? ??? ???? ??, NATO ??? ????? ??
  • has-a ?? ?? has an ??
  • ?? (composition)
  • ?? ???? ???? ?
  • ?? ??? ??, ??? ??? ??, ??? ????? ??
  • contains-a ?? ?? contains ????

32
????? ??? ?? UML ??
33
?, ?? ?? ???? ?? ???
  • ??, ??? ??, ??, ??? ?? ???? ??
  • ??? ??? ??? ??? ???
  • ??? ??? ??
  • ??? ??? ? ?? ???? ??? ??? ??
  • ??? ??? ?? ??? ???? ??
  • ??? ??? ? ??? ??? ???? ? ??? ???? ??? ??? ??
  • ??? ?? ??? ??? ????? ??

34
?? ?? ???? ?? 5? ???
35
?????? ??
36
  • ??? ??
  • ??? B extend ??? A
  • ??? ??
  • ??? A ??? ??? B ??? ??? ??
  • ??? ??
  • ?? ???? ??
  • ? Student ???? Transcript(???) ???

37
??? ??? ??
  • LISTING 1.8 Implementing Inheritance and
    Aggregation
  • 1 public class Student extends Person //
    Student inherits Person
  • 2 protected String country // Student
    aggregates String
  • 3 protected int credits
  • 4 protected double gpa
  • 5
  • 6 public Student(String name, boolean male, int
    yob, String country)
  • 7 super(name, male, yob)
  • 8 this.country country
  • 9
  • 10

38
?? ??(Implementing Composition)
  • 1 public class Student extends Person //
    inheritance
  • 2 private String country // aggregation
  • 3 private int credits
  • 4 private double gpa
  • 5 private final Transcript transcript new
    Transcript()
  • 7 public Student(String name, boolean male,
    int yob, String country)
  • 8 super(name, male, yob)
  • 9 this.country country
  • 10
  • 12 public void updateTranscript(Section
    section, Grade grade)
  • 13 transcript.add(section, grade)
  • 14
  • 16 public void printTranscript()
  • 17 System.out.println(transcript)
  • 18
  • 20 private class Transcript // composition
  • 21 // internal fields...
  • 22 void add(Section section, Grade grade)
    //...
  • 23 public String toString() //...

39
????? ??
40
1.8 ?? ??? ?? ??
  • ?? (immutable) ??
  • ???? ??? ? ?? ??
  • ? Java?? String ??
  • ?? (mutable) ??
  • ???? ??? ? ?? ??
  • ? Java?? StringBuffer ??
  • ??? ??? ??? -gt ?? ???? ?
  • ? ??? 1.7? Person ???
  • ???? ?? ???? ??
  • set ???? ???? ???? ??
  • ?? ??? private? ??
  • ?? ??? final? ?? ?? 1.13, ??? 1.10 -gt ?? ???
  • Phone ??? Person ???? ??? ????? ?? ?? ??? ?? ???
    1.12
  • ?? ???? ??
  • ??? ?????? ?? ???? ??? ? ??
  • ? java.math.BigInteger ???

41
??? Person ???
42
??? Person ???
  • LISTING 1.10 A Revised Person Class
  • 1 public class Person
  • 2 protected final boolean male
  • 3 protected final String name
  • 4 protected final Phone phone
  • 5 protected final int yob
  • 7 public Person(String name, boolean male, int
    yob, Phone phone)
  • 8 this.name name
  • 9 this.male male
  • 10 this.yob yob
  • 11 this.phone phone 12
  • 14 public String getName()
  • 15 return name 16
  • 18 public Phone getPhone()
  • 19 return phone 20
  • 22 public int getYob()
  • 23 return yob 24
  • 26 public boolean isMale()
  • 27 return male 28

43
?? Phone ???
  • LISTING 1.11 A Mutable Phone Class
  • 1 class Phone
  • 2 private String areaCode, number
  • 3
  • 4 public Phone(String areaCode, String number)
  • 5 this.areaCode areaCode
  • 6 this.number number
  • 7
  • 8
  • 9 public void setAreaCode(String areaCode)
  • 10 this.areaCode areaCode
  • 11
  • 12

44
?? Person ??
45
?? Person ???
  • LISTING 1.12 An Immutable Person Class
  • 1 public class Person
  • 2 // INVARIANT all instances are
    immutable
  • 3 protected final boolean male
  • 4 protected final String name
  • 5 protected final Phone phone
  • 6 protected final int yob
  • 8 public Person(String name, boolean male, int
    yob, Phone phone)
  • 9 this.name name
  • 10 this.male male
  • 11 this.yob yob
  • 12 this.phone new Phone(phone)
  • 13
  • 14 // Lines 14-28 from Listing 1.10 on page 23
    go here.
  • 16

46
?? Person ??
47
??? Phone ??? ???? ?? Person ??
  • LISTING 1.13 Immutable Person Objects Sharing
    the Same Phone Object
  • 1 public Person(String name, boolean male, int
    yob, Person friend)
  • 2 this.name name
  • 3 this.male male
  • 4 this.yob yob
  • 5 this.phone friend.phone
  • 6

48
??? Person ???? ?? ??? ????
  • LISTING 1.14 A Test Driver for the Revised
    Person Class
  • 1 public class TestPerson
  • 2 public static void main(String args)
  • 3 Phone p new Phone("804", "3790550")
  • 4 Person john new Person("John Adams", true,
    1980, p)
  • 5 System.out.println(john)
  • 6 Person jane new Person("Jane Adams", false,
    1981, john)
  • 7 System.out.println(jane)
  • 8 john null
  • 9 System.out.println(jane)
  • 10
  • 11

49
  • ?? ??
  • Mr. John Adams (1980), tel. (804)379-0550Ms.
    Jane Adams (1981), tel. (804)379-0550Ms. Jane
    Adams (1981), tel. (804)379-0550

50
??? Phone ??? ???? ? Person ??
Phone ??? ???? Person ???
Write a Comment
User Comments (0)
About PowerShow.com