????? (Inheritance and Polymorphism) - PowerPoint PPT Presentation

About This Presentation
Title:

????? (Inheritance and Polymorphism)

Description:

(Inheritance and Polymorphism) / / – PowerPoint PPT presentation

Number of Views:94
Avg rating:3.0/5.0
Slides: 139
Provided by: ShyhKa5
Category:

less

Transcript and Presenter's Notes

Title: ????? (Inheritance and Polymorphism)


1
?????(Inheritance and Polymorphism)
  • ???
  • ??????
  • ??????/???????/
  • ???????????

2
??
  1. ??
  2. ???protected
  3. ????
  4. ????????????
  5. OCP??-????
  6. ??
  7. ????????0.1?
  8. ?????

3
??
  • ????System.Object
  • LSP Liskov?????
  • ????
  • DIP ???????
  • ??
  • ISP ??????
  • ????
  • ??????

4
??
  1. ??
  2. ???protected
  3. ????
  4. ????????????
  5. OCP??-????
  6. ??
  7. ????????0.1?
  8. ?????

5
UsingInheritance.Calculator ??
  • 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
  • public int Multiply(int a, int b)
  • int result a b
  • return result

6
UsingInheritance.Program.Program.Main() ??(1/2)
  • AdvancedCalculator calculator new
    AdvancedCalculator()
  • . . .
  • switch (op)
  • case 1
  • result calculator.Add(operand1,operand2)
  • Console.WriteLine("0 1 2 ",
  • operand1, operand2, result)
  • break
  • . . .

7
UsingInheritance.Program.Program.Main() ??(2/2)
  • case 5
  • functionValue calculator.GetSine(angle)
  • Console.WriteLine(
  • "Sine of 0 (deg) 1",
  • angle, functionValue)
  • break
  • . . .

8
UsingInheritance.AdvancedCalculator ??
  • public class AdvancedCalculator Calculator
  • public double GetSine(double angle)
  • angle Math.PI / 180.0
  • return Math.Sin(angle)
  • . . .

9
?????UML???
10
?????????
  • class A
  • private int data1
  • private int data2
  • //other members are methods
  • class B A
  • private int data3
  • //other members are methods
  • class C B
  • private int data1
  • private int data4
  • //other members are methods

11
?????????
A a new A() B b new B() C c new C()
data1
a
data2
c
data1
data1
b
data2
data2
data3
data3
data1
data4
12
??
  • ???????????

13
??
  1. ??
  2. ???protected
  3. ????
  4. ????????????
  5. OCP??-????
  6. ??
  7. ????????0.1?
  8. ?????

14
UsingProtected.Program.Program.Main()??
  • DC d new DC()
  • d.SetX(3)
  • //Console.WriteLine( d.GetX() ) // Error!
  • //d.x 77 // Error!
  • d.Add2()

15
UsingProtected.Program??
  • class BC
  • private int x
  • public void SetX( int x ) this.x x
  • protected int GetX() return x
  • class DC BC
  • public void Add2()
  • int c GetX()
  • SetX( c2 )

16
??
  1. ??
  2. ???protected
  3. ????
  4. ????????????
  5. OCP??-????
  6. ??
  7. ????????0.1?
  8. ?????

17
????
  • sealed class SClass
  • . . . . . .

18
??
  1. ??
  2. ???protected
  3. ????
  4. ????????????
  5. OCP??-????
  6. ??
  7. ????????0.1?
  8. ?????

19
UsingConstructorsForInheritance.Program.Main() ??
  • Animal slug new Animal()
  • Animal tweety new Animal( "canary" )
  • Primate godzilla new Primate()
  • Primate human new Primate( 4 )
  • Human jill new Human()

20
UsingConstructorsForInheritance.Program ??(1/3)
  • class Animal
  • private string species
  • public Animal()
  • Console.WriteLine("Animal()")
  • species "Animal"
  • public Animal( string s )
  • Console.WriteLine("Animal(" s ")")
  • species s

21
UsingConstructorsForInheritance.Program??(2/3)
  • class Primate Animal
  • private int heartCham
  • public Primate() base()
  • Console.WriteLine( "Primate()" )
  • public Primate( int n ) base( "Primate" )
  • Console.WriteLine("Primate(" n ")")
  • heartCham n

22
UsingConstructorsForInheritance.Program ??(3/3)
  • class Human Primate
  • public Human() base( 4 )
  • Console.WriteLine( "Human()" )

23
????????
Primate human new Primate( 4 )
public Primate( int n ) base( "Primate" )
. . .
public Animal( string s ) . . .
24
??
  • ???????????UsingConstructorsForInheritance

25
??
  1. ??
  2. ???protected
  3. ????
  4. ????????????
  5. OCP??-????
  6. ??
  7. ????????0.1?
  8. ?????

26
??-????(OCPOpen-Closed Principle)
  • Software entities (classes, modules, functions,
    etc.) should be open for extension, but closed
    for modification
  • ?????????,?????????????

Robert C. Martin, Agile Software Development
Principles, Patterns, and Practices, Pearson
Education, 2003
27
??OCPViolationExample???
28
OCPViolationExample.Program.Program.Main() ??
  • Point center
  • center.x 15
  • center.y 20
  • Point topLeft
  • topLeft.x 30
  • topLeft.y 40
  • Shape list new Circle(2, center),
  • new Rectangle(3, 4, topLeft)
  • DrawAllShapes(list)

29
OCPViolationExample.Program.Program ?? (1/2)
  • static void DrawAllShapes(Shape list)
  • for (int i 0 i lt list.Length i)
  • Shape s listi
  • switch (s.type)
  • case ShapeType.CIRCLE
  • DrawCircle((Circle) s)
  • break
  • case ShapeType.RECTANGLE
  • DrawRectangle((Rectangle) s)
  • break

30
OCPViolationExample.Program.Program ?? (2/2)
  • static void DrawCircle(Circle c)
  • Console.WriteLine("Draw a circle")
  • static void DrawRectangle(Rectangle r)
  • Console.WriteLine("Draw a rectangle")

31
OCPViolationExample.Program ?? (1/2)
  • class Shape
  • public ShapeType type
  • public Shape(ShapeType t)
  • type t
  • class Circle Shape
  • private int radius
  • private Point center
  • public Circle(int radius, Point center)
  • base(ShapeType.CIRCLE)
  • this.radius radius
  • this.center center

32
OCPViolationExample.Program ?? (2/2)
  • class Rectangle Shape
  • private int width
  • private int height
  • private Point topLeft
  • public Rectangle(int width, int height,
  • Point topLeft)
  • base(ShapeType.RECTANGLE)
  • this.width width
  • this.height height
  • this.topLeft topLeft

33
OCPViolationExample?????
  • ??????Shape???(??,???????????)???????switch??
  • ??????Shape???,????enum??????switch??,????Program?
    ????Draw??

34
??
  1. ??
  2. ???protected
  3. ????
  4. ????????????
  5. OCP??-????
  6. ??
  7. ????????0.1?
  8. ?????

35
?????????
  • ??(packaging)
  • ??(inheritance)
  • ??(polymorphism)

36
??
  • ?????(compile-time binding)??????(run-time
    binding)
  • ????(static binding)?????(dynamic binding)
  • ?????
  • ??????
  • ??(virtual)???(override)
  • ???????
  • ??????

37
DrawingAllShapes???
38
DrawingAllShapes.Program.Program.Main() ??(1/2)
  • Shape list new Shape2
  • Point center
  • center.x 15
  • center.y 20
  • Point topLeft
  • topLeft.x 30
  • topLeft.y 40
  • Circle c new Circle(2, center)
  • Rectangle r new Rectangle(3, 4, topLeft)
  • Console.WriteLine("??????, ??")
  • Console.WriteLine("1 ??, ??")
  • Console.WriteLine("2 ??, ??")
  • int ans int.Parse(Console.ReadLine())

39
DrawingAllShapes.Program.Program.Main() ??(2/2)
  • switch (ans)
  • case 1
  • list0 c
  • list1 r
  • break
  • case 2
  • list0 r
  • list1 c
  • break
  • default
  • . . .
  • DrawAllShapes(list)

40
DrawingAllShapes.Program.Program ??
  • static void DrawAllShapes(Shape list)
  • int i
  • for (i 0 i lt list.Length i)
  • listi.Draw()

41
DrawingAllShapes.Program ??(1/3)
  • class Shape
  • public Shape()
  • virtual public void Draw()

42
DrawingAllShapes.Program ??(2/3)
  • class Circle Shape
  • private int radius
  • private Point center
  • public Circle(int radius, Point center)
  • this.radius radius
  • this.center center
  • override public void Draw()
  • Console.WriteLine("Draw a circle")

43
DrawingAllShapes.Program ??(3/3)
  • class Rectangle Shape
  • private int width
  • private int height
  • private Point topLeft
  • public Rectangle(int width, int height,
  • Point topLeft)
  • this.width width
  • this.height height
  • this.topLeft topLeft
  • override public void Draw()
  • Console.WriteLine("Draw a rectangle")

44
??
  • ???DrawingAllShapes????Triangle,???????????

45
??
  1. ??
  2. ???protected
  3. ????
  4. ????????????
  5. OCP??-????
  6. ??
  7. ????????0.1?
  8. ?????

46
BlackJack_0_1 ???
47
BlackJack_0_1.BlackJackTest ??(1/2)
  • public static bool Scenario1_OK()
  • Card cards new Card(Suit.SPADE, 1),
  • new Card(Suit.HEART, 11),
  • new Card(Suit.DIAMOND, 10)
  • Deck deck new Deck(cards)
  • Player player new Player()
  • Dealer dealer new Dealer()

48
BlackJack_0_1.BlackJackTest ??(2/2)
  • player.SaveACard(deck.DealACard())
  • dealer.SaveACard(deck.DealACard())
  • player.SaveACard(deck.DealACard())
  • return(
  • player.GetStatus() Status.BLACK_JACK
  • dealer.GetStatus() Status.PASS)

49
BlackJack_0_1.Game ?? (1/6)
  • const int N_PLAYERS 2
  • Deck deck
  • Player players new PlayerN_PLAYERS
  • public Game()
  • players0 new Player("Jeng")
  • playersN_PLAYERS-1 new Dealer()

50
BlackJack_0_1.Game ?? (2/6)
  • private void Play()
  • int i
  • // ?????
  • for (i 0 i lt N_PLAYERS i)
  • playersi.SaveACard(
  • deck.DealACard())
  • playersi.Dump()

51
BlackJack_0_1.Game ?? (3/6)
  • // ?????
  • for (i0 i lt N_PLAYERS i)
  • playersi.SaveACard(
  • deck.DealACard())
  • playersi.Dump()

52
BlackJack_0_1.Game ?? (4/6)
  • // ??????
  • for(i0 iltN_PLAYERS i)
  • while (playersi.GetStatus()
  • Status.PASS
  • playersi.WantOneMoreCard()
  • deck.HasMoreCard())
  • playersi.SaveACard(deck.DealACard())
  • playersi.Dump()
  • if(IsBlackJackOrBurst(playersi))
  • return

53
BlackJack_0_1.Game ?? (5/6)
  • // ?????
  • Player dealer playersN_PLAYERS-1
  • for(i0 iltN_PLAYERS-1 i)
  • if (dealer.GetTotalPoints() gt
  • playersi.GetTotalPoints())
  • Console.WriteLine( dealer.Name
  • "?"playersi.Name)
  • else
  • Console.WriteLine(
  • playersi.Name"?"dealer.Name)

54
BlackJack_0_1.Game ?? (6/6)
  • private bool IsBlackJackOrBurst(
  • Player player)
  • bool isBlackJack false
  • if (player.GetStatus()Status.BLACK_JACK)
  • isBlackJack true
  • Console.WriteLine(player.Name
  • " BlackJack!!!")
  • bool isBurst false
  • if (player.GetStatus() Status.BURST)
  • isBurst true
  • Console.WriteLine(player.Name" ?!!!")
  • return (isBlackJack isBurst)

55
BlackJack_0_1.Player ??(1/5)
  • private Card hand new Card11
  • private int nCards
  • private Status status
  • private int totalPoints
  • private string name
  • public Player()
  • nCards 0
  • name "???"

56
BlackJack_0_1.Player ??(2/5)
  • public Player(string name)
  • nCards 0
  • this.name name
  • public string Name
  • get return name

57
BlackJack_0_1.Player ??(3/5)
  • virtual public bool WantOneMoreCard()
  • Console.Write("??????? (y/n) ")
  • string answer Console.ReadLine()
  • return (answer "Y" answer "y")

58
BlackJack_0_1.Player ??(4/5)
  • public void Dump()
  • int i
  • Console.Write(name" ? ")
  • for (i 0 i lt nCards i)
  • handi.Dump()
  • Console.Write("\t")
  • if ((i 1) 5 0)
  • Console.WriteLine()

59
BlackJack_0_1.Player ??(5/5)
  • Console.WriteLine()
  • Console.WriteLine(name " ??? "
  • totalPoints)

60
BlackJack_0_1.Dealer??
  • class Dealer Player
  • public Dealer() base("??")
  • override public bool WantOneMoreCard()
  • return (base.GetTotalPoints() lt 17)

61
??
  1. ??
  2. ???protected
  3. ????
  4. ????????????
  5. OCP??-????
  6. ??
  7. ????????0.1?
  8. ?????

62
UsingBase.Program???
63
UsingBase.Program?? (1/3)
  • // Define the base class
  • class Car
  • public virtual void DescribeCar()
  • System.Console.WriteLine(
  • "Four wheels and an engine.")

64
UsingBase.Program?? (2/3)
  • // Define the derived classes
  • class ConvertibleCar Car
  • public new virtual void DescribeCar()
  • base.DescribeCar()
  • Console.WriteLine(
  • "A roof that opens up.")

65
UsingBase.Program?? (3/3)
  • class Minivan Car
  • public override void DescribeCar()
  • base.DescribeCar()
  • Console.WriteLine(
  • "Carries seven people.")

66
UsingBase.Program.Program.Main()?? (1/2)
  • // new and override make no differences here
  • Car car1 new Car()
  • car1.DescribeCar()
  • Console.WriteLine("----------")
  • ConvertibleCar car2 new ConvertibleCar()
  • car2.DescribeCar()
  • Console.WriteLine("----------")
  • Minivan car3 new Minivan()
  • car3.DescribeCar()
  • Console.WriteLine("----------")

67
UsingBase.Program.Program.Main()?? (2/2)
  • // they are different in polymorphysm
  • Car cars new Car3
  • cars0 new Car()
  • cars1 new ConvertibleCar()
  • cars2 new Minivan()
  • foreach (Car vehicle in cars)
  • Console.WriteLine("Car object "
  • vehicle.GetType())
  • vehicle.DescribeCar()
  • Console.WriteLine("----------")

68
?????
  • ?? override
  • ????Polymorphism (??)
  • ?????
  • ?? new
  • ??????????????????
  • ???????

69
??
  • ???????????UsingBase

70
??
  • ????System.Object
  • LSP Liskov?????
  • ????
  • DIP ???????
  • ??
  • ISP ??????
  • ????
  • ??????

71
????System.Object
  • ?????
  • ????
  • Equals
  • GetHashCode
  • GetType
  • ReferenceEquals
  • ToString
  • Finalize

72
InheritingObject.Program.Program.Main()??
  • Test t1 new Test()
  • Test t2 new Test()
  • bool isEqual t1.Equals(t2)
  • Console.WriteLine(t1.ToString())
  • Console.WriteLine("t1 ?t2 ???" isEqual)

73
InheritingObject.Program ??
  • class Test
  • override public string ToString()
  • return "??InheritingObject.Test"

74
Boxing ? Unboxing
  • int x 10
  • Object obj (Object) x // boxing
  • obj 20
  • int j (int)obj // unboxing

75
??
  • ????System.Object
  • LSP Liskov?????
  • ????
  • DIP ???????
  • ??
  • ISP ??????
  • ????
  • ??????

76
Liskov ?????(LSP Liskov Substitution Principle)
  • Subtype must be substitutable for their base
    types
  • ???????
  • ???S?????o1,???T???o2,????????T?????P?,P??????o1??
    o2???
  • ??LSP????OCP

Robert C. Martin, Agile Software Development
Principles, Patterns, and Practices, Pearson
Education, 2003
77
IS-A ?????
78
LSPViolationExample.Program?? (1/4)
  • class Rectangle
  • private int width
  • private int height
  • virtual public int Width
  • set width value
  • virtual public int Height
  • set height value

79
LSPViolationExample.Program?? (2/4)
  • public int Area()
  • return width height

80
LSPViolationExample.Program?? (3/4)
  • class Square Rectangle
  • override public int Width
  • set base.Width value
  • base.Height value
  • override public int Height
  • set base.Width value
  • base.Height value

81
LSPViolationExample.Program?? (4/4)
  • class Program
  • static void Main(string args)
  • Square s new Square()
  • Test(s)
  • static void Test(Rectangle r)
  • r.Width 5
  • r.Height 4
  • Debug.Assert(r.Area() 20)

82
??????????
  • ???????????
  • Rectangle.Width?????
  • Debug.Assert( (width value)
  • (height old.height))
  • ??LSP???????????
  • ????????????????????,????
  • ????????????????????,????

83
??
  • ????System.Object
  • LSP Liskov?????
  • ????
  • DIP ???????
  • ??
  • ISP ??????
  • ????
  • ??????

84
????
85
AbstractClassExample.Program.Program.Main() ??
  • double a 5.0
  • Square sq new Square(a)
  • Console.WriteLine("???sq????" sq.Area())
  • Circle c new Circle(a)
  • Console.WriteLine("??c????" c.Area())

86
AbstractClassExample.Program?? (1/3)
  • public abstract class Shape
  • private string shape
  • public Shape(string shape)
  • this.shape shape
  • Console.WriteLine("??" shape)
  • abstract public double Area()

87
AbstractClassExample.Program?? (2/3)
  • public class Square Shape
  • double a
  • public Square(double a) base("???")
  • this.a a
  • public override double Area()
  • return a a

88
AbstractClassExample.Program?? (3/3)
  • public class Circle Shape
  • double r
  • public Circle(double r) base("??")
  • this.r r
  • public override double Area()
  • return Math.PI r r

89
??
  • ???DrawingAllShapes????Shape??????,??Shape.Draw()?
    ?????

90
????
  • ???
  • ????
  • ???
  • ?????????

91
??
  • ????System.Object
  • LSP Liskov?????
  • ????
  • DIP ???????
  • ??
  • ISP ??????
  • ????
  • ??????

92
???????(DIP Dependency-Inversion Principle)
  • High-level modules should not depend on low-level
    modules. Both should depend on abstractions.
  • Abstractions should not depend on details.
    Details should depend on abstractions.

Robert C. Martin, Agile Software Development
Principles, Patterns, and Practices, Pearson
Education, 2003
93
????DIP???
94
???(State Chart)
95
DIPViolationExample.Program?? (1/4)
  • public enum LampStatus
  • OFF 0,
  • ON 1
  • public enum ButtonStatus
  • RELEASED 0,
  • PRESSED 1

96
DIPViolationExample.Program?? (2/4)
  • public class Lamp
  • private LampStatus status LampStatus.OFF
  • public LampStatus Status
  • get return status
  • public void TurnOn()
  • status LampStatus.ON
  • public void TurnOff()
  • status LampStatus.OFF

97
DIPViolationExample.Program?? (3/4)
  • public class Button
  • private ButtonStatus status
  • ButtonStatus.RELEASED
  • private Lamp lamp
  • public Button(Lamp lamp)
  • this.lamp lamp
  • public ButtonStatus Status
  • get return status

98
DIPViolationExample.Program?? (4/4)
  • public void Press()
  • if (status ButtonStatus.RELEASED)
  • status ButtonStatus.PRESSED
  • lamp.TurnOn()
  • public void Release()
  • if( status ButtonStatus.PRESSED )
  • status ButtonStatus.RELEASED
  • lamp.TurnOff()

99
DIPViolationExample.Program.Program.Main() ??
(1/3)
  • Lamp lamp1 new Lamp(1)
  • Button button new Button(lamp1)
  • Random rand new Random()
  • for (int n 0 n lt 100 n)
  • Console.Write("time n " n "\t")

100
DIPViolationExample.Program.Program.Main() ??
(2/3)
  • if (rand.Next() 2 1)
  • if (button.Status
  • ButtonStatus.PRESSED)
  • button.Release()
  • else
  • button.Press()

101
DIPViolationExample.Program.Program.Main() ??
(3/3)
  • if (lamp1.Status LampStatus.OFF)
  • Console.WriteLine("lamp1 is off")
  • else
  • Console.WriteLine("lamp1 is on")
  • Console.WriteLine()

102
??
  • ????System.Object
  • LSP Liskov?????
  • ????
  • DIP ???????
  • ??
  • ISP ??????
  • ????
  • ??????

103
??
104
UsingInterface.Program.Program.Main() ??
  • double a 5.0
  • Square sq new Square(a)
  • Console.WriteLine("???sq????" sq.Area())
  • Circle c new Circle(a)
  • Console.WriteLine("??c????" c.Area())

105
UsingInterface.Program??(1/3)
  • interface Shape
  • double Area()

106
UsingInterface.Program??(2/3)
  • public class Square Shape
  • double a
  • public Square(double a)
  • this.a a
  • public double Area()
  • return a a

107
UsingInterface.Program??(3/3)
  • public class Circle Shape
  • double r
  • public Circle(double r)
  • this.r r
  • public double Area()
  • return Math.PI r r

108
?? vs. ???? (1/2)
  • interface Shape
  • double Area()
  • --------------------------------------------
  • public abstract class Shape
  • private string shape
  • public Shape(string shape)
  • this.shape shape
  • Console.WriteLine("??" shape)
  • abstract public double Area()

109
?? vs. ???? (2/2)
  • public class Square Shape
  • . . .
  • public double Area()
  • return a a
  • --------------------------------------------
  • public class Square Shape
  • . . .
  • public override double Area()
  • return a a

110
????DIP???
111
ButtonAndLamp.Program (1/4)
  • public interface SwitchableDevice
  • void TurnOn()
  • void TurnOff()
  • public enum LampStatus
  • OFF 0,
  • ON 1
  • public enum ButtonStatus
  • RELEASED 0,
  • PRESSED 1

112
ButtonAndLamp.Program (2/4)
  • public class Lamp SwitchableDevice
  • private LampStatus status LampStatus.OFF
  • public LampStatus Status
  • get return status
  • public void TurnOn()
  • status LampStatus.ON
  • public void TurnOff()
  • status LampStatus.OFF

113
ButtonAndLamp.Program (3/4)
  • public class Button
  • private ButtonStatus status
  • ButtonStatus.RELEASED
  • private SwitchableDevice device
  • public Button(SwitchableDevice device)
  • this.device device
  • public ButtonStatus Status
  • get return status

114
ButtonAndLamp.Program (4/4)
  • public void Press()
  • if (status ButtonStatus.RELEASED)
  • status ButtonStatus.PRESSED
  • device.TurnOn()
  • public void Release()
  • if (status ButtonStatus.PRESSED)
  • status ButtonStatus.RELEASED
  • device.TurnOff()

115
??
  • ???ButtonAndLamp????Fan,?Button?????Fan????

116
??
  • ????System.Object
  • LSP Liskov?????
  • ????
  • DIP ???????
  • ??
  • ISP ??????
  • ????
  • ??????

117
??????(ISP Interface-Segregation Principle)
  • Clients should not be forced to depend on methods
    that they do not use
  • ???????????????????

118
????ISP???
119
ISPViolationExample.Program?? (1/4)
  • interface TimerClient
  • void TimeOut()
  • interface Door TimerClient
  • void Lock()
  • void Unlock()
  • bool IsOpen()
  • enum DoorStatus
  • CLOSED 0,
  • OPEN 1

120
ISPViolationExample.Program?? (2/4)
  • class Timer
  • private int t
  • private int timeout
  • private TimerClient client
  • public Timer(int timeout,
  • TimerClient client)
  • this.timeout timeout
  • this.client client
  • t 0

121
ISPViolationExample.Program?? (3/4)
  • public void Advance()
  • t
  • if (t timeout 0)
  • client.TimeOut()
  • class TimedDoor Door
  • private DoorStatus status
  • DoorStatus.CLOSED

122
ISPViolationExample.Program?? (4/4)
  • public bool IsOpen()
  • return (status DoorStatus.OPEN)
  • public void Lock()
  • if (IsOpen()) status DoorStatus.CLOSED
  • public void Unlock()
  • if (!IsOpen()) status DoorStatus.OPEN
  • public void TimeOut()
  • Lock()

123
ISPViolationExample.Program.Program.Main()
??(1/2)
  • TimedDoor tDoor new TimedDoor()
  • int timeout 10
  • Timer timer new Timer(timeout, tDoor)
  • int n
  • const int N 20
  • tDoor.Unlock()
  • for (n 0 n lt N n)

124
ISPViolationExample.Program.Program.Main()
??(2/2)
  • if (tDoor.IsOpen())
  • Console.WriteLine(
  • "n " n "\t tDoor is open")
  • else
  • Console.WriteLine(
  • "n " n "\t tDoor is closed")
  • timer.Advance()

125
??
  • ????System.Object
  • LSP Liskov?????
  • ????
  • DIP ???????
  • ??
  • ISP ??????
  • ????
  • ??????

126
??MultiInterface???
127
MultiInterface.Program.Program.Main()??
  • Floatplane fp new Floatplane()
  • fp.Sail()
  • fp.Fly()

128
MultiInterface.Program?? (1/2)
  • interface Plane
  • void Fly()
  • interface Ship
  • void Sail()

129
MultiInterface.Program?? (2/2)
  • public class Floatplane Plane, Ship
  • public Floatplane()
  • Console.WriteLine("??????")
  • public void Sail()
  • Console.WriteLine("????")
  • public void Fly()
  • Console.WriteLine("????")

130
??
  • ??????, ?????????Clock_Radio,????Clock?GetTime()??
    ?Radio?PlayMusic()??

131
????ISP???
132
TimedDoorSimulation.Program??
  • interface TimerClient
  • void TimeOut()
  • interface Door
  • void Lock()
  • void Unlock()
  • bool IsOpen()
  • . . .
  • class TimedDoor Door, TimerClient
  • . . .

133
??
  • ????System.Object
  • LSP Liskov?????
  • ????
  • DIP ???????
  • ??
  • ISP ??????
  • ????
  • ??????

134
??CastMultiInterfaces???
135
CastMultiInterfaces.Program.Program.Main()??
  • double a 5.0
  • Square sq new Square(a)
  • Rhombus rhomb sq as Rhombus
  • Console.WriteLine(
  • "sq???????????"rhomb.Area() )
  • if( sq is Rectangle )
  • Rectangle rec (Rectangle) sq
  • Console.WriteLine(
  • "sq???????????"rec.Area() )

136
CastMultiInterfaces.Program??(1/3)
  • interface Rectangle
  • double Area()
  • interface Rhombus
  • double Area()

137
CastMultiInterfaces.Program??(2/3)
  • public class Square Rectangle, Rhombus
  • private double a
  • private double d
  • public Square(double a)
  • this.a a
  • d Math.Sqrt(2.0) a

138
CastMultiInterfaces.Program??(3/3)
  • double Rectangle.Area()
  • return a a
  • double Rhombus.Area()
  • return 0.5 d d
Write a Comment
User Comments (0)
About PowerShow.com