กก object-oriented programming (oop) · public class car public color as string private speed...

43

Upload: others

Post on 30-Mar-2020

0 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: กก Object-Oriented Programming (OOP) · Public Class Car Public color As String Private Speed As Short '45 ก?K( KK 0 @ Speed i% L")* Car Public HorsePower As Short 'HorsePower
Page 2: กก Object-Oriented Programming (OOP) · Public Class Car Public color As String Private Speed As Short '45 ก?K( KK 0 @ Speed i% L")* Car Public HorsePower As Short 'HorsePower

�������� ���กก�� Object-Oriented Programming (OOP)

� "#�$�$�%&�'�����(" (Object)

� "#�$�$�%&�'")�* (Class)

� "#�$�$�%ก���.��/��0�.1 (Inheritance)

� "#�$�$�%&�' Object-Oriented Programming (OOP)

� 4�56%7�0&�' OOP

� Polymorphism

� Overloading

� Overriding

� ���กก�� Abstract Class

� Shared Members

� Interfaceก��4�5ก�?�.��@��0�A*

4�56%7�0&�' OOP

� ก���&B%�64�Cก�$C�� OOP 1#% VB.NET� ��.J$@�*��'")�*

� *��'K�(�K�K��0@B�L�")�*

� *��'�����("��ก")�*

� �K.J$�$M�1L�")�*

� �K.J$K�(�K�K��0@B�C)5�$M�1L�"��

� Inheritance

� ก��4�5ก�?�.��@��0�A*

� ก����B%กL7'���.��@��0�A*

� Namespace

Page 3: กก Object-Oriented Programming (OOP) · Public Class Car Public color As String Private Speed As Short '45 ก?K( KK 0 @ Speed i% L")* Car Public HorsePower As Short 'HorsePower

���กก�� Object-Oriented Programming (OOP)

�����(" (Object) "�� �����("4�5ก��N41#%K�(�K�K��0@B� (Property) ����"OP*$��@.�QK�5@�# C)5�$M�1 (Method) �K�J�L7RS�'���%T�'��UJ'�%T�'L1ก�������("����V

�$M�1(Method) �5RS�'��ก(@T��$�J�$B�B�#�@0(Event) L1V �ก.1&U�� �5N$TRS�61%K)ก���1(1&�1 �7T� �B�#�@0 W").ก4OX$ Change IconYZ �5RS�L��$M�1 Change Icon RS�'���7T� �B�#�@0 W").ก4OX$ Change IconYZ �5RS�L��$M�1 Change Icon RS�'��

Page 4: กก Object-Oriented Programming (OOP) · Public Class Car Public color As String Private Speed As Short '45 ก?K( KK 0 @ Speed i% L")* Car Public HorsePower As Short 'HorsePower

Object

Property

Method Method

Interface

Method Method

Method

Page 5: กก Object-Oriented Programming (OOP) · Public Class Car Public color As String Private Speed As Short '45 ก?K( KK 0 @ Speed i% L")* Car Public HorsePower As Short 'HorsePower

������ �� �� ��� ���������� ���������� �������������������������������������� ����

Object ����

��������

���������

� ��

Interface

��������������������� ��������

��� �����

�����

Method

Page 6: กก Object-Oriented Programming (OOP) · Public Class Car Public color As String Private Speed As Short '45 ก?K( KK 0 @ Speed i% L")* Car Public HorsePower As Short 'HorsePower

������ �� �� ������ !������ก���# ������$�������%����� #�������

Object ������

���������������������

����������

Interface

����������������� ������$�������%����� #��������� ��&�'!����

��������ก���� �� ��!� �

��"��������

�����������������

��ก

Page 7: กก Object-Oriented Programming (OOP) · Public Class Car Public color As String Private Speed As Short '45 ก?K( KK 0 @ Speed i% L")* Car Public HorsePower As Short 'HorsePower

#��� ��ก"�

Page 8: กก Object-Oriented Programming (OOP) · Public Class Car Public color As String Private Speed As Short '45 ก?K( KK 0 @ Speed i% L")* Car Public HorsePower As Short 'HorsePower

#��� �$% "

Page 9: กก Object-Oriented Programming (OOP) · Public Class Car Public color As String Private Speed As Short '45 ก?K( KK 0 @ Speed i% L")* Car Public HorsePower As Short 'HorsePower

")�* (Class) �$�%[U' @�C������K.$K0�&B%#&�'�����("

Class �5[กL7�4\�@�C��L�ก��*��'�����(" C)5�5L7'�������(" N$TN1L7'��")�*61%@�'

Page 10: กก Object-Oriented Programming (OOP) · Public Class Car Public color As String Private Speed As Short '45 ก?K( KK 0 @ Speed i% L")* Car Public HorsePower As Short 'HorsePower

�������������(Inheritance) �$�%[U' ก��*��R�1"OP*$��@.��กC$T*T)ก ������ก@�C���1.$( Bse Class) N4%�'@�C��L�$T (Child Class)(Child Class)

ก���4)BJ%�C4)'CกN& �K.J$�@.$�5N�ก(@�$L� Child Class RBJ Inheritance $� �5��B%ก#T��4\�ก��RS� Overriding

Page 11: กก Object-Oriented Programming (OOP) · Public Class Car Public color As String Private Speed As Short '45 ก?K( KK 0 @ Speed i% L")* Car Public HorsePower As Short 'HorsePower

")�*�*����7.�@C&�*���Base Class RB#BM��$1� �[%�@0 4 4�5@

")�*�*����7.�@C&�%�#Child Class RB#B��C�� �[*4��0@

Inherit Inherit Inherit

")�*�*����7.�@C&�%�#Child Class RB#B��C�� �[*4��0@

Page 12: กก Object-Oriented Programming (OOP) · Public Class Car Public color As String Private Speed As Short '45 ก?K( KK 0 @ Speed i% L")* Car Public HorsePower As Short 'HorsePower

Object-Oriented Programming

(OOP) � �����

ก���&B%�64�Cก�$RBJ�S����C�#".1&�' Object-

Oriented (OO) $�L74�56%7�0 7T#%L�Oriented (OO) $�L74�56%7�0 7T#%L�*�$��[K�]��64�Cก�$N1�%T�'�4\��5��$�ก%.J'&U��

Page 13: กก Object-Oriented Programming (OOP) · Public Class Car Public color As String Private Speed As Short '45 ก?K( KK 0 @ Speed i% L")* Car Public HorsePower As Short 'HorsePower

4�56%7�0&�' OOP

� 7T#%)1�#)�L�ก��K�]��64�Cก�$N4N1$�ก ���J�'��ก*�$��[�S���������("RBJ"���J�K�]��N#C)#$�L7'��N1 (reuseability)

� *�$��[K�]��@T�%�1 (extensibility) ��ก�����("RBJ$B�%TC)#N1� *�$��[K�]��@T�%�1 (extensibility) ��ก�����("RBJ$B�%TC)#N1

� *�$��[1C)CกN&64�Cก�$N1'T�% ���J�'��ก�����("�5$B"#�$*$��P0L�@�#$����' ก��CกN&64�Cก�$*�$��[CกN&�QK�5�����("RBJ@�'ก���RT����� UJ'�5N$Tก�5R�@T�*T#���J�V &�'64�Cก�$C@T�%T�'L1

Page 14: กก Object-Oriented Programming (OOP) · Public Class Car Public color As String Private Speed As Short '45 ก?K( KK 0 @ Speed i% L")* Car Public HorsePower As Short 'HorsePower

*��'K�(�K�K��0@B�L�")�*� �����()*#�*�� Car.Vb �*�Form.Vb �+%������ก,���*�� Car

� �* ก���-���%��.+���$�,� Solution Explorer

� �* ก���/ Add->Add Class

� �*%�ก Class ,���� Templates �*�,��%�� Car

� ����� *�* ก�-�� Car.vb ,� Solution Explorer

� �+ ��+�$�+�+��# �'����(�����*�� Car->Public Color as String( ���ก�0+�$�+�+��# �' Color ���,��*�� Car)���ก�0+�$�+�+��# �' Color ���,��*�� Car)

ก�����ก�0 ���������*����1���� Public ������ก�� Field �%������22/ก���/�����(��.�� ����ก�����ก (� ���3����4�����,��*��

� 2�� Private ������ก�� Property ก�������%������ ���-!���ก.+�5�����# Get ��%� Set ���,��*���-���'�

�*�ก��� OOP -���(���(���ก����������� Field ��%� Property � ������ก���6�� Property �-���'�

Page 15: กก Object-Oriented Programming (OOP) · Public Class Car Public color As String Private Speed As Short '45 ก?K( KK 0 @ Speed i% L")* Car Public HorsePower As Short 'HorsePower

*��'�����("��ก")�*� ก��4�5ก�?�����(" (Object Declaration)

� ������ [Dim|Public|Private] <7�J������("> As <7�J�")�*@�C��>

� �7T� Public TestCar As Car

� ก��*��'�����("� ������ <7�J������("> = New <7�J�")�*@�C��>()

� �7T� TestCar = New Car()

� ����*�$��[4�5ก�?�����("K��$ก��*��'�����("N1i�%L�"S�*�J'�1B%#ก�� � ����*�$��[4�5ก�?�����("K��$ก��*��'�����("N1i�%L�"S�*�J'�1B%#ก�� � �4C�� [Dim|Public|Private] <7�J������("> As New <7�J�")�*@�C��>()

� �7T� Public TestCar As New Car()

� ก����B%กL7'��� �4C�� <7�J������(">.<7�J�K�(�K�K��0@B������$M�1>

� �7T� TestCar.Color = WBlueZ

MessageBOx.Show(WTestCar $B*B = W& TestColor.Color()

%ก�).ก�����("61%กS���11�'�B� 7�J� Instance = Nothing

Page 16: กก Object-Oriented Programming (OOP) · Public Class Car Public color As String Private Speed As Short '45 ก?K( KK 0 @ Speed i% L")* Car Public HorsePower As Short 'HorsePower

ก���K.J$�$M�1L�")�*� ������

[ReadOnly] property <�%��+�$�+�+��# �'>() as<�� �����/*>Get

Return < �������� Private ���,��*��>

End Get

Set(Byval Inputvalue as short)

< �������� Private ���,��*�� = InputValue< �������� Private ���,��*�� = InputValue

End Set

End Property

Page 17: กก Object-Oriented Programming (OOP) · Public Class Car Public color As String Private Speed As Short '45 ก?K( KK 0 @ Speed i% L")* Car Public HorsePower As Short 'HorsePower

��������ก��������*���+%���!���&��+%'�-�������ก*�.��ก���������0����ก.���ก���*�ก

,�� Add Class Module ������,�.����$ก # (�*%�ก��ก���/ Project -> Add Class) �'��%���*����1� AreaCircle

Public Class AreaCircle

Private vArea As Long

Public WriteOnly Property Radius() As Long

Set(ByVal value As Long)

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

Set(ByVal value As Long)

vArea = 3.14 * value * value

End Set

End Property

Public ReadOnly Property Area() As Long

Get

Return vArea

End Get

End Property

End Class

Button1.Click

Dim Mycircle As AreaCircle

Mycircle = New AreaCircle()

Mycircle.Radius = TextBox1.Text

TextBox2.Text = Mycircle.Area

Mycircle = Nothing

End Sub

Page 18: กก Object-Oriented Programming (OOP) · Public Class Car Public color As String Private Speed As Short '45 ก?K( KK 0 @ Speed i% L")* Car Public HorsePower As Short 'HorsePower

Color HorsePowerGearPosition Speed

Class Car

Property

ChangGear FillFuel

GearPosition SpeedFuelLevel

IsLMoving

Accerelate

Page 19: กก Object-Oriented Programming (OOP) · Public Class Car Public color As String Private Speed As Short '45 ก?K( KK 0 @ Speed i% L")* Car Public HorsePower As Short 'HorsePower

*��'�����("��ก")�*� *��'�����(" TestCar ��ก")�* Car 1�'�4

Page 20: กก Object-Oriented Programming (OOP) · Public Class Car Public color As String Private Speed As Short '45 ก?K( KK 0 @ Speed i% L")* Car Public HorsePower As Short 'HorsePower
Page 21: กก Object-Oriented Programming (OOP) · Public Class Car Public color As String Private Speed As Short '45 ก?K( KK 0 @ Speed i% L")* Car Public HorsePower As Short 'HorsePower

Public Class Car

Public color As String

Private Speed As Short '4�5ก�?K�(�K�K��0@B� Speed i�%L�")�* Car

Public HorsePower As Short 'HorsePower �4\�C�� Public

Private GearPosition As String = "#T�'"

Private FuelLevel As Short

Private FuelQuantity As Short

Public Sub FillFuel(ByVal FueIQuantity As Short)

FuelLevel += FuelQuantity '�K.J$ FuelLevel @�$�S��#� FuelQuantity

End Sub

ReadOnly Property CurrentGear() As String

Get

Return GearPosition

End GetPrivate FuelQuantity As Short

Public Sub ChangeGear(ByVal NewGearPosition As String) 'sub ChangeGear()

GearPosition = NewGearPosition 'กS���1L�@�#C4� GearPosition=@�#C4�NewGearPosition

End Sub

End Get

End Property

ReadOnly Property CurrentFuelLevel() As Short

Get

Return FuelLevel

End Get

End Property

Page 22: กก Object-Oriented Programming (OOP) · Public Class Car Public color As String Private Speed As Short '45 ก?K( KK 0 @ Speed i% L")* Car Public HorsePower As Short 'HorsePower

Public Function IsMoving() As Boolean

If Speed = 0 Then

IsMoving = False

Else

IsMoving = True

End If

Property CurrentSpeed() As Short

Get

Return Speed

End Get

Set(ByVal InputValue As Short)

Speed = InputValueEnd If

End Function

Public Sub Accerelate(ByVal DiffSpeed As Short)

Speed += DiffSpeed

End Sub

Speed = InputValue

End Set

End Property

End Class

Page 23: กก Object-Oriented Programming (OOP) · Public Class Car Public color As String Private Speed As Short '45 ก?K( KK 0 @ Speed i% L")* Car Public HorsePower As Short 'HorsePower

�&B%�64�Cก�$ก��A��0$$B1�'�B�

Page 24: กก Object-Oriented Programming (OOP) · Public Class Car Public color As String Private Speed As Short '45 ก?K( KK 0 @ Speed i% L")* Car Public HorsePower As Short 'HorsePower

Public TestCar As Car '4�5ก�?�����(" TestCar ��ก")�* Car

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

TestCar = New Car() '*��'�����(" TestCar 61%L7"S�*�J' New

End Sub

Private Sub btnAccerelate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAccerelate.Click

MessageBox.Show("Speed ��.J$@� =" & TestCar.CurrentSpeed())

TestCar.Accerelate(20)

MessageBox.Show("Speed �)�'��T'"#�$��(# =" & TestCar.CurrentSpeed())End Sub

Private Sub btnReadColor_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnReadColor.Click

TestCar.color = "Blue"

MessageBox.Show("TestCar $B*B =" & TestCar.color)

End Sub

TestCar.CurrentSpeed())

TestCar.Accerelate(-5)

MessageBox.Show("Speed �)�')1"#�$��(# =" & TestCar.CurrentSpeed())

End Sub

Page 25: กก Object-Oriented Programming (OOP) · Public Class Car Public color As String Private Speed As Short '45 ก?K( KK 0 @ Speed i% L")* Car Public HorsePower As Short 'HorsePower

Private Sub btnChangeGear_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnChangeGear.Click

MessageBox.Show("@S�C��T'�กB%�04���O��� =" & TestCar.CurrentGear())

TestCar.ChangeGear(1)

MessageBox.Show("�)�'�4)BJ%��กB%�0 @S�C��T'�กB%�0 =" & TestCar.CurrentGear())

MessageBox.Show("4�.$�P��S�$�� =" & TestCar.CurrentFuelLevel())

End Sub

Private Sub btnCheckIsMoving_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCheckIsMoving.Click�กB%�0 =" & TestCar.CurrentGear())

End Sub

Private Sub btnFillFuel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnFillFuel.Click

MessageBox.Show("4�.$�P��S�$�� =" & TestCar.CurrentFuelLevel())

TestCar.FillFuel(50)

btnCheckIsMoving.Click

MessageBox.Show("�[#.J' =" & TestCar.IsMoving())

TestCar.Accerelate(10)

MessageBox.Show("�[#.J' =" & TestCar.IsMoving())

End Sub �

Page 26: กก Object-Oriented Programming (OOP) · Public Class Car Public color As String Private Speed As Short '45 ก?K( KK 0 @ Speed i% L")* Car Public HorsePower As Short 'HorsePower

Polymorphism �$�%[U' ก��RBJ���*�$��[�&B%��$M�17�J��1B%#ก��L�*�$��[���K���$.�@��0N1�)�%7�.1C)5ก���&B%��$M�17�J��1B%#ก��N1�)�%7�.1C)5ก���&B%��$M�17�J��1B%#ก��")�*RBJ inherit $� C@TRS�'��"�)5�%T�'ก��

Page 27: กก Object-Oriented Programming (OOP) · Public Class Car Public color As String Private Speed As Short '45 ก?K( KK 0 @ Speed i% L")* Car Public HorsePower As Short 'HorsePower

Overloading �$�%[U' ก���&B%��$M�17�J��1B%#RBJ*�$��[���"T�K���$.�@��0@T�'7�.1ก��N1������ [Public|Private|Protected] Overloads [Sub | Function]<7�J�A�'ก07��>(<7�J�K���$.�@��0> as <7�.1&�$)>]�&��

Public Overloads Sub TestPrint(ByVal Input As Integer)Public Overloads Sub TestPrint(ByVal Input As Integer)Public Overloads Sub TestPrint(ByVal Input As String) Public Overloads Sub TestPrint(ByVal Input As Boolean)

Overriding �$�%[U' ก��กS���1�$M�1��UJ'V $Bก��RS�'��RBJ�4)BJ%�C4)'N4��กL�")�*�1.$(RBJ����.��/��0�.@) �����4\�ก���4)BJ%�C4)'�4C��ก��RS�'��@�$RBJกS���1N#L� Base Class

Page 28: กก Object-Oriented Programming (OOP) · Public Class Car Public color As String Private Speed As Short '45 ก?K( KK 0 @ Speed i% L")* Car Public HorsePower As Short 'HorsePower

����� � Overloading �+ ����4�� TestPrint ,��*�� SportCar �����'

Public Class SportCar

Inherits Car

Public Overloads Sub TestPrint(ByVal Input As Integer)

MessageBox.Show("�����ก TestPrint: Input As Integer =" & Input)

End Sub

Public Overloads Sub TestPrint(ByVal input As Boolean)

MessageBox.Show("�����ก TestPrint: Input As Boolean=" & input)

End Sub

End ClassPrivate Sub btnTestOverloading_Click(ByVal sender As

System.Object, ByVal e As System.EventArgs) Handles End Sub

Public Overloads Sub TestPrint(ByVal input As String)

MessageBox.Show("�����ก TestPrint: Input As String =" & input)

End Sub

System.Object, ByVal e As System.EventArgs) Handles btnTestOverloading.Click

Dim a As Integer = 10

Dim b As String = "VB.Net"

Dim c As Boolean = True

TestCar.TestPrint(a)

TestCar.TestPrint(b)

TestCar.TestPrint(c)

End Sub

Page 29: กก Object-Oriented Programming (OOP) · Public Class Car Public color As String Private Speed As Short '45 ก?K( KK 0 @ Speed i% L")* Car Public HorsePower As Short 'HorsePower

� ��������� Overriding

Page 30: กก Object-Oriented Programming (OOP) · Public Class Car Public color As String Private Speed As Short '45 ก?K( KK 0 @ Speed i% L")* Car Public HorsePower As Short 'HorsePower

Abstract Class �$�%[U'")�*RBJ$B�$M�1L1�$M�1��UJ'����K�(�K�K��0@B�L1K�(�K�K��0@B���UJ' �S��4\�@�'N1���ก�� Override 61%")�*RBJ*��R�1N4��ก")�*���� 1�'@�#�%T�'

Page 31: กก Object-Oriented Programming (OOP) · Public Class Car Public color As String Private Speed As Short '45 ก?K( KK 0 @ Speed i% L")* Car Public HorsePower As Short 'HorsePower

Share Members

Share Method �$�%[U' L�VB.NET �5*�$��[��B%กL7�$M�1C)5K�(�K�K��0@B�&�'�����("61%N$T@�'*��'�.�*CR� 0&�'")�*กT�� 1�'@�#�%T�'

Page 32: กก Object-Oriented Programming (OOP) · Public Class Car Public color As String Private Speed As Short '45 ก?K( KK 0 @ Speed i% L")* Car Public HorsePower As Short 'HorsePower

Share Members

Share Variable �$�%[U' L�ROกV�.�*CR� 0&�'")�*L7'��@�#C4��T#$ก�� �7T� [�@�'ก��L�ก���4)BJ%�C4)'@�#C4�i�%L������("��UJ' RS�L�@�#C4�����L������("(�.�*CR� 0) ��J�V �4)BJ%�C4)'"T�N41#% 1�'@�#�%T�'

Page 33: กก Object-Oriented Programming (OOP) · Public Class Car Public color As String Private Speed As Short '45 ก?K( KK 0 @ Speed i% L")* Car Public HorsePower As Short 'HorsePower

Interface �$�%[U' 7T#%C%ก*T#� define class ก��*T#� implementation ��ก��กก�� Interface�54�5ก��1#%6"�'*��'&�'")�* �7T� Property,Method,Parameter@T�'V C@T�5N$T$B*T#�ก���&B%�6"164�Cก�$ *�$��[*��'")�*�)�%V")�*��ก�.��@��0�A*N1 61%RBJC@T)5")�*���L�RS�'��@T�'Vก��N4

�4C��ก��4�5ก�?�.��@��0�A*

Public Interface <7�J��.��@��0�A*>

Y.

End Interface

�4C��ก����B%กL7'���.��@��0�A*

Public Class <7�J�")�*>

Implements <7�J��.��@��0�A*>

End ClassEnd Interface

�7T�(���[�1N4)

Public Interface IManageFile �4�5ก�?�.��@��0�A*7�J� IManageFile

Sub Open(ByVal FileName As String)

Sub Close()

End Interface

End Class

Page 34: กก Object-Oriented Programming (OOP) · Public Class Car Public color As String Private Speed As Short '45 ก?K( KK 0 @ Speed i% L")* Car Public HorsePower As Short 'HorsePower
Page 35: กก Object-Oriented Programming (OOP) · Public Class Car Public color As String Private Speed As Short '45 ก?K( KK 0 @ Speed i% L")* Car Public HorsePower As Short 'HorsePower

Namespace �$�%[U' ก����1�$#1�$TL�ก��")�*@T�'V �%T�'�4\��5�� 61%")�*RBJ$B#�@[O4�5*'"0ก��L7'����$���ก���5[ก�#��#$N#L���$*�4`�1B%#ก��

�4C��

NameSpace <7�J���$*�47>

Public Class <7�J�")�*RBJ 1>

Y.

End ClassEnd Class

Y.

Public Class <7�J�")�*RBJ n>

Y.

End Class

End NameSpace

Page 36: กก Object-Oriented Programming (OOP) · Public Class Car Public color As String Private Speed As Short '45 ก?K( KK 0 @ Speed i% L")* Car Public HorsePower As Short 'HorsePower

� �����6 *�$��[��'[U'")�* Car C)5 SportCar N161%L77�J��@($V#T�

Mycar.Car C)5 Mycar.SportCar @�$)S�1��

Page 37: กก Object-Oriented Programming (OOP) · Public Class Car Public color As String Private Speed As Short '45 ก?K( KK 0 @ Speed i% L")* Car Public HorsePower As Short 'HorsePower

����������������*��!�����ก���!���&���/*����� VAT �*��� ��&�� �-�� ��������%��

��� VAT �*�� .������/*-���!���1�-�� ����������(��� �!����� ����-��5%'� (Amount), ���� ����� �*�����#�5$� # VAT .�� %VAT ��'���ก3/�,��(�ก!������,���� Default �%� 7% ���ก���������/*������ก!����,�������2���(�� 2 �� �%��� ��&-��5%'�, %VAT �*��/*��-�'������%����� VAT �*��

Page 38: กก Object-Oriented Programming (OOP) · Public Class Car Public color As String Private Speed As Short '45 ก?K( KK 0 @ Speed i% L")* Car Public HorsePower As Short 'HorsePower

Public Class CBill

Private vPrice As Double

Private vAmount As Double

Private vVat As Double

Public WriteOnly Property PricePerUnit() As Double

Set(ByVal value As Double)

vPrice = value

End Set

End Property

Public Property VAT() As Double

Get

Public Property Amount() As Double

Get

Return vAmount

End Get

Set(ByVal value As Double)

vAmount = value

End Set

End Property

Public ReadOnly Property Total() As DoubleGet

Return vVat

End Get

Set(ByVal Value As Double)

If Value <> 0 Then

vVat = Value

Else

vVat = 7

End If

End Set

End Property

Get

Dim vt As Long

vt = vVat * (vPrice * vAmount) / 100

Total = (vPrice * vAmount) + vt

Return Total

End Get

End Property

End Class

Page 39: กก Object-Oriented Programming (OOP) · Public Class Car Public color As String Private Speed As Short '45 ก?K( KK 0 @ Speed i% L")* Car Public HorsePower As Short 'HorsePower

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

For Each sender In Me.Controls

If TypeOf sender Is TextBox Then

sender.text = " "

End If

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

Dim MyBill As CBill

MyBill = New CBill()

MyBill.VAT = Val(TextBox1.Text)

MyBill.PricePerUnit = Val(TextBox2.Text)

MyBill.Amount = Val(TextBox3.Text)End If

Next

End Sub

Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click

Form1_Load(sender, e)

End Sub

MyBill.Amount = Val(TextBox3.Text)

TextBox4.Text = MyBill.Total

MyBill = Nothing

End Sub

Page 40: กก Object-Oriented Programming (OOP) · Public Class Car Public color As String Private Speed As Short '45 ก?K( KK 0 @ Speed i% L")* Car Public HorsePower As Short 'HorsePower

���-�� 1 �������.���ก����1��*���ก����ก��ก��9�ก (Deposit),2��(Withdraw) �*���$������ �,���:�� (Balance) ��;�.����$�,���*���+ ���*��������,�.���ก��ก!������1��%�� Cbanking.vb ����/�

���78ก��:;���

Page 41: กก Object-Oriented Programming (OOP) · Public Class Car Public color As String Private Speed As Short '45 ก?K( KK 0 @ Speed i% L")* Car Public HorsePower As Short 'HorsePower

2. �'�&B%�64�Cก�$)�ก�P5&�' Abstract Class &�'ก��".1�'.�"T�RS�'��)T#'�#)�61%$BK��ก'��C��K��ก'��4�5�S�C)5C��K��ก'��7�J#"��# [�K��ก'��4�5�S�".17�J#6$')5 500 ��R C)5K��ก'��7�J#"��#7�J#6$')5 300 ��R

C����ก��1R�%�R

Page 42: กก Object-Oriented Programming (OOP) · Public Class Car Public color As String Private Speed As Short '45 ก?K( KK 0 @ Speed i% L")* Car Public HorsePower As Short 'HorsePower

3. �'�&B%�64�Cก�$�4��&�"P.@RBJ$B*BJ��)BJ%$ *�$��)BJ%$ #'ก)$ #'�B �)�%��)BJ%$ �4\�@� 61%L�$Bก��RS�'��C�� Inheritance , Overloading, Overriding, Share Members, Interface C)5 Namesspace UJ'����5�K.J$*T#�RBJ�กBJ%#&�'N1@�$"#�$��$�5*$�7T� ก����K���RBJ ก���S��4$�@T�ก�� ก��"S��#P�*����#' �4\�@� �K�J�L�"��C�� OOP @�$6�R%0RBJกS���1&�'@�

C����ก��1R�%�R

�K�J�L�"��C�� OOP @�$6�R%0RBJกS���1&�'@�

Page 43: กก Object-Oriented Programming (OOP) · Public Class Car Public color As String Private Speed As Short '45 ก?K( KK 0 @ Speed i% L")* Car Public HorsePower As Short 'HorsePower

4. กS���1L���ก?Uก��@��'6�R%0L�ก���&B%�64�Cก�$RBJ*�1")�'ก��MO�ก.�L�4���O��� 61%*�$��[�S��������"���'�B�4�5%Oก@0L7N1 1�'@�#�%T�'C����ก��1R�%&�RBJ1-3 K��$R��'�&B%�64�Cก�$@��6�R%0RBJ@��'1#%

C����ก��1R�%�R