????? I (Classes and Objects I) - PowerPoint PPT Presentation

About This Presentation
Title:

????? I (Classes and Objects I)

Description:

I (Classes and Objects I) / / – PowerPoint PPT presentation

Number of Views:30
Avg rating:3.0/5.0
Slides: 70
Provided by: ShyhKa3
Category:

less

Transcript and Presenter's Notes

Title: ????? I (Classes and Objects I)


1
????? I(Classes and Objects I)
  • ???
  • ??????
  • ??????/???????/
  • ???????????

2
???????/?????
  • ??/??/??
  • ???
  • Magic number 7 ?? 2
  • ??????
  • ??
  • ????

3
????????(Object-Oriented Programming)
  • ??IC/???????( Code Reuse)
  • ??(Encapsulation)
  • ?????
  • ??
  • ??(Inheritance)
  • ??(Polymorphism)

4
?? DiceSimulation (1/4)
  • using System
  • namespace DiceSimulation
  • /
  • ??????????????????
  • 3/16/2008
  • ????
  • ??????????????
  • /
  • class Program
  • static void Main(string args)

5
?? DiceSimulation (2/4)
  • int count new int6
  • // ????????
  • for (int i 0 i lt 6 i)
  • counti 0
  • const int N 12000 // ?????
  • int faceValue
  • Dice dice new Dice()
  • // ??N?
  • for (int i 0 i lt N i)
  • dice.Toss()
  • faceValue dice.FaceValue
  • countfaceValue-1

6
?? DiceSimulation (3/4)
  • // ????
  • for (int i 0 i lt 6 i)
  • Console.WriteLine(" 0 appears 1 times
    ",
  • i 1, counti)

7
?? DiceSimulation (4/4)
  • public class Dice
  • int faceValue 1
  • Random rand new Random()
  • public int FaceValue
  • get return faceValue
  • set faceValue value
  • public void Toss()
  • faceValue rand.Next() 6 1

8
??????
static void Main(string args)
public class Dice . . . public void Toss()
faceValue rand.Next() 6 1 . .
.
dice.Toss()

public class Random . . . public int Next()
. . . . . .
9
??(Class)??
?? Dice
????(???? field)
faceValue, rand
????(???? function, ?? attribute)
FaceValue
Toss()
10
??(Stack)???(Heap)
. . .
Stack
11
????????
??(Stack)
int x 100
100
x
12
????????
??(Heap)
??(Stack)
string x abc
a
??
x
b
c
13
??(Object)??
dice1
dice1.faceValue
?????1
dice1.rand
??Toss????
dice2
dice2.fileName
?????2
dice2.rand
??Toss????
????Toss???
14
?????(Assignment)
dice1
dice1.faceValue
?????1
dice1.rand
??Toss????
dice2
dice2 dice1
dice2.fileName
?????2
dice2.rand
??Toss????
????Toss???
15
UML ?????
Dice
faceValue int rand Random
FaceValue Toss() void
16
??1
  • ?????????

Car
speed int
driver string
Speed Driver Accelerate() PutOnBrakes()
17
?? CalculatorTest (1/6)
  • using System
  • namespace CalculatorTest
  • / ????Calculator????????
  • 3/17/2007
  • /
  • class Program
  • static void Main(string args)
  • int op 0
  • int operand1
  • int operand2
  • int result
  • Calulator calculator new Calulator()

18
?? CalculatorTest (2/6)
  • do
  • Console.Write(
  • "???? 0. ?? 1. ? 2. ? 3. ? 4. ? ")
  • op int.Parse(Console.ReadLine())
  • if (op 0) break
  • if (op gt 4) continue
  • Console.Write("??????? ")
  • operand1 int.Parse(Console.ReadLine())
  • Console.Write("??????? ")
  • operand2 int.Parse(Console.ReadLine())
  • switch (op)

19
?? CalculatorTest (3/6)
  • case 1
  • result calculator.Add(operand1,
  • operand2)
  • Console.WriteLine("0 1 2 ",
  • operand1, operand2, result)
  • break
  • case 2
  • result calculator.Subtract(operand1,
  • operand2)
  • Console.WriteLine("0 - 1 2 ",
  • operand1, operand2, result)
  • break
  • case 3
  • result calculator.Multiply(operand1,
  • operand2)

20
?? CalculatorTest (4/6)
  • Console.WriteLine("0 1 2 ",
  • operand1, operand2, result)
  • break
  • case 4
  • result calculator.Divide(operand1,
  • operand2)
  • Console.WriteLine("0 / 1 2 ",
  • operand1, operand2, result)
  • break
  • default
  • Console.WriteLine(
  • "Should not appear this message. Debug!!!")
  • break
  • while (true)

21
?? CalculatorTest (5/6)
  • /
  • ???
  • 3/17/2007
  • /
  • public class Calulator
  • public int Add(int a, int b)
  • int result a b
  • return result
  • public int Subtract(int a, int b)
  • int result a - b
  • return result

22
?? CalculatorTest (6/6)
  • public int Multiply(int a, int b)
  • int result a b
  • return result
  • public int Divide(int a, int b)
  • int result a / b
  • return result

23
?????
Program.Main()
result
calculator.Add()
result
return value
24
??SwapIntegers (1/2)
  • using System
  • namespace SwapIntegers
  • / ????????
  • ????????????
  • skj 4/5/2008
  • /
  • class Program
  • static void Main(string args)
  • int x 3
  • int y 5
  • Console.WriteLine("x 0, y 1", x,
    y)
  • Swap(ref x, ref y)
  • Console.WriteLine("x 0, y 1", x,
    y)

25
??SwapIntegers (2/2)
  • static void Swap(ref int x, ref int y)
  • int temp x
  • x y
  • y temp

26
?? PassByReferenceAndOut (1/4)
  • using System
  • namespace PassByReferenceAndOut
  • /
  • ???????out???????
  • 3/31/2008
  • /
  • class Program
  • static void Main(string args)
  • double length 100.0
  • Square s new Square()
  • s.Side length

27
?? PassByReferenceAndOut (2/4)
  • double area1 0.0
  • double perimeter1 0.0
  • s.GetAreaAndPerimeter(ref area1,
  • ref perimeter1)
  • Console.WriteLine(
  • "????? 0, ?? 1, ?? 2",
  • length, area1, perimeter1)
  • double area2
  • double perimeter2
  • s.GetAreaAndPerimeterUsingOut(out area2,
  • out perimeter2)
  • Console.WriteLine(
  • "????? 0, ?? 1, ?? 2", length,
    area2, perimeter2)
  • Console.ReadLine()

28
?? PassByReferenceAndOut (3/4)
  • /
  • ???
  • 3/16/2008
  • /
  • public class Square
  • double a
  • public double Side
  • get return a
  • set a value

29
?? PassByReferenceAndOut (4/4)
  • public void GetAreaAndPerimeter(ref double
    area,
  • ref double perimeter)
  • area Math.Pow(a, 2)
  • perimeter 4.0 a
  • public void GetAreaAndPerimeterUsingOut(
  • out double area, out double perimeter)
  • area Math.Pow(a, 2)
  • perimeter 4.0 a

30
??, ??, out ??
  • ??????( Pass by value )
  • ??????( Pass by reference )
  • out ??
  • ??????

31
??Time
Time
hour int min int sec int
SetTime() GetTime()
32
?? UsingThis (1/4)
  • using System
  • namespace UsingThis
  • /
  • ??this???
  • 3/17/2007
  • /
  • class Program
  • static void Main(string args)
  • Time t new Time()
  • t.SetTime(11, 30, 52)

33
?? UsingThis (2/4)
  • int hour
  • int min
  • int sec
  • t.GetTime(out hour, out min, out sec)
  • Console.WriteLine("????0 1 2",
  • hour, min, sec)
  • Console.ReadLine()
  • /
  • ??,?,?????
  • /

34
?? UsingThis (3/4)
  • public class Time
  • int hour
  • int min
  • int sec
  • public void SetTime(int hour, int min, int
    sec)
  • bool error ( hour lt 0 hour gt 24
  • min lt 0 min gt 59
  • sec lt 0 sec gt 59 )
  • if (!error)
  • this.hour hour
  • this.min min
  • this.sec sec

35
?? UsingThis (4/4)
  • else
  • Console.WriteLine(
  • "Time.SetTimegtgt??????")
  • public void GetTime(out int hour, out int
    min,
  • out int sec)
  • hour this.hour
  • min this.min
  • sec this.sec

36
?????? this
t
?????
hour
min
sec
this
SetTime()????
????SetTime???
37
???????(Extreme Programming)
  1. ???????????
  2. ??????,????
  3. ?????????????????
  4. ???????????????
  5. ????????

38
??(1/4)
  • ????
  • ???? Card deck new Card2
  • ?deck????????Card??
  • ????Swap???????
  • ?????????????,?????Swap???????

39
??(2/4)
  • ??Swap???
  • Procedure Swap( card1, card2 )
  • 1. temp card1
  • 2. card1 card2
  • 3. card2 temp
  • return

card1
temp
card2
40
??(3/4)
  • ?????????

Card
suit char
faceValue int
SetCard() GetCard()
41
??(4/4)
  • ?????Card???????
  • ?????????
  • ?Swap???????????,????
  • ??????????????

42
?? OverloadingDemo (1/4)
  • using System
  • namespace OverloadingDemo
  • /
  • ???????
  • 3/17/2007
  • /
  • class Program
  • static void Main(string args)
  • Adder adder new Adder()
  • int i 0

43
?? OverloadingDemo (2/4)
  • do
  • Console.Write(
  • "???????? 0. ?? 1. ?? 2. ??? ")
  • i int.Parse(Console.ReadLine())
  • if (i 0) break
  • if (i gt 2) continue
  • switch (i)
  • case 1
  • Console.Write("???????")
  • int a int.Parse(Console.ReadLine())
  • Console.Write("???????")
  • int b int.Parse(Console.ReadLine())
  • adder.AddAndDisplay(a, b)
  • break

44
?? OverloadingDemo (3/4)
  • case 2
  • Console.Write("????????")
  • double ad
  • double.Parse(Console.ReadLine())
  • Console.Write("????????")
  • double bd
  • double.Parse(Console.ReadLine())
  • adder.AddAndDisplay(ad, bd)
  • break
  • while (true)
  • / ?????
  • 3/17/2007
  • /

45
?? OverloadingDemo (4/4)
  • public class Adder
  • public void AddAndDisplay(int a, int b)
  • int result a b
  • Console.WriteLine("0 1 2", a, b,
    result)
  • public void AddAndDisplay(double a, double b)
  • double result a b
  • Console.WriteLine("0 1 2", a, b,
  • result)

46
??????
  • ????????????
  • ??????/???????????
  • ??/???????????????????
  • ? Mission Impossible, RPG

47
????
  • ????N_ROW??N_COL??????,?????????????????????????2?
    3?????
  • ?????? 9?18,?????8?11?8,?????27???????????????,??
    ??????????????????

3 4 2
5 7 6
48
????????
  • 1. ????
  • 2. ??????
  • 3. ??????
  • 4. ??????
  • 5. ????

49
????
  • ??
  • ??????
  • ?
  • ??????
  • ?
  • ??????

50
?? Table
Table
content int,
nRow int nCol int
Content GetRowSum() GetColSum() GetTotalSum()
51
?? ClassDemo.Program (1/5)
  • using System
  • namespace ClassDemo
  • /
  • ????N_ROW??N_COL??????,
  • ?????????????????
  • ??????
  • ?????
  • 1. ????
  • 2. ??????
  • 3. ??????
  • 4. ??????
  • 5. ????

52
?? ClassDemo.Program (2/5)
  • ?? 2 X 3 ??
  • ---------------------------
  • 3 4 2
  • ---------------------------
  • 5 7 6
  • ---------------------------
  • ????
  • ------------------
  • 9 18
  • ------------------
  • ????
  • ---------------------------
  • 8 11 8
  • ---------------------------

53
?? ClassDemo.Program (3/5)
  • ????
  • 27
  • /
  • class Program
  • static void Main(string args)
  • const int N_ROW 2
  • const int N_COL 3
  • int, table new intN_ROW, N_COL
  • table0, 0 3
  • table0, 1 4
  • table0, 2 2
  • table1, 0 5

54
?? ClassDemo.Program (4/5)
  • table1, 1 7
  • table1, 2 6
  • Table t new Table()
  • t.Content table
  • int rowSum t.GetRowSum()
  • int colSum t.GetColSum()
  • int totalSum t.GetTotalSum()
  • for (int i 0 i lt N_ROW i)
  • Console.WriteLine("Sum of row " i
  • " is " rowSumi)

55
?? ClassDemo.Program (5/5)
  • for (int j 0 j lt N_COL j)
  • Console.WriteLine("Sum of column " j
  • " is " colSumj)
  • Console.WriteLine("Total sum is "
    totalSum)
  • Console.ReadLine()

56
?? ClassDemo.Table (1/5)
  • using System
  • namespace ClassDemo
  • /
  • ??
  • skj 3/31/2008
  • /
  • public class Table
  • private int, content
  • int nRow
  • int nCol

57
?? ClassDemo.Table (2/5)
  • public int, Content
  • get return content
  • set
  • content value
  • nRow content.GetUpperBound(0) 1
  • nCol content.GetUpperBound(1) 1

58
?? ClassDemo.Table (3/5)
  • public int GetRowSum()
  • int rowSum new intnRow
  • for (int i 0 i lt nRow i)
  • rowSumi 0
  • for (int j 0 j lt nCol j)
  • rowSumi contenti, j
  • return rowSum

59
?? ClassDemo.Table (4/5)
  • public int GetColSum()
  • int colSum new intnCol
  • for (int j 0 j lt nCol j)
  • colSumj 0
  • for (int i 0 i lt nRow i)
  • colSumj contenti, j
  • return colSum

60
?? ClassDemo.Table (5/5)
  • public int GetTotalSum()
  • int totalSum 0
  • for (int i 0 i lt nRow i)
  • for (int j 0 j lt nCol j)
  • totalSum contenti, j
  • return totalSum

61
??/?????
  • ????????????????????????????/??(CRC?)
  • ????????????????????????/?????(CRC?)
  • ??????????????????(CRC?)
  • ??????????UML?????
  • ????????????????????

62
?? CRC ?
Object Name withdrawManager
Collaborators
Responsibilities Ask user for amount to
withdraw Verify amount with
AccountManager Tell cash dispenser to release
cash
accountManager cashDispenser
63
?? CRC?
Class Name WithdrawManager
Collaborators
Responsibilities Ask user for amount to
withdraw Verify amount with
AccountManager Tell CashDispenser to release
cash
AccountManager CashDispenser
64
UML ???( Class Diagram )
G. Booch, J. Rumbaugh, and I. Jacobson, The
Unified Modeling Language User Guide, Addison
Wesley, 1999.
65
??(Aggregation)???
  • class Company
  • //
  • Department department
  • // created by Company objects
  • Office office
  • // created by Company objects
  • //

66
??(Association)???
  • class Department
  • //
  • Person manager // created outside Department
  • Person member // created outside Department
  • //
  • class Person
  • //
  • Department department // created outside Person
  • //

67
???????????
  • F. P. Brooks, Jr., The Mythical Man-Month
    Essays on Software Engineering, Addison-Wesley,
    1975
  • G. Booch, J. Rumbaugh, and I. Jacobson, The
    Unified Modeling Language User Guide, Addison
    Wesley, 1999.
  • I. Jacobson, G. Booch, and J. Rumbaugh, The
    Unified Software Development Process, Addison
    Wesley, 1999.
  • R. C. Lee and W. M. Tepfenhart, UML and C, A
    Practical Guide to Object-Oriented Development,
    2nd ed., Prentice Hall, 2001.
  • ????, W. C. Wake ??, ??????--???, ??, 2002.

68
?? (1/2)
  • ??????????????, ???????

track1
track2
69
?? (2/2)
  • ??????
  • while(true)
  • ?????????????????
  • ????????(????????)
  • ????????????
  • if ??????,
  • ?????
  • break
  • ???????
Write a Comment
User Comments (0)
About PowerShow.com