Jump to content

OOP Question


Recommended Posts

I have two classes which share a few functions. How do I make one class inherit from another? Please no jokes about wills, ahem, jrowe =P !

Should I make a class that defines all electrical devices which have Name and State as properties and AnnounceName, AnnounceState, and Toggle as methods and then make two objects (light and switch) based on that class?

OR, should I do as I stated in the previous line but make a private controller class that monitors the state of the switch and changes the light bulb accordingly?

The user programmer will only have access to the switch object. The lightbulb stays hidden. But I want the lightbulb to automagically turn on and off when someone toggles the switch.

#region LightBulb
#include <AutoItObject.au3>

Func LightBulb()
    Local $oSelf = _AutoItObject_Create()
    ; properties
    _AutoItObject_AddProperty($oSelf, "Name", $ELSCOPE_PRIVATE, "Light Bulb")
    _AutoItObject_AddProperty($oSelf, "State", $ELSCOPE_PRIVATE,)
    ; methods
    _AutoItObject_AddMethod($oSelf, "AnnounceState", "_announceState", True)
    _AutoItObject_AddMethod($oSelf, "AnnounceName", "_announceName")
    _AutoItObject_AddMethod($oSelf, "ToggleState", "_toggleState", True)
    Return $oSelf
EndFunc ;==>LightBulb

Func _displayName()
    MsgBox(0, "Name:", $oSelf.Name)
EndFunc ;==>_displayName

Func _toggleState($oSelf)
    $oSelf.State = Not $oSelf.State
EndFunc ;==>_toggleState
#endregion LightBulb

#region LightSwitch
Func LightSwitch()
    Local $oSelf = _AutoItObject_Create()
    ; properties
    _AutoItObject_AddProperty($oSelf, "Name", $ELSCOPE_PRIVATE, "Light Switch")
    _AutoItObject_AddProperty($oSelf, "State", $ELSCOPE_PRIVATE, False)
    ; methods
    _AutoItObject_AddMethod($oSelf, "Status", "_status")
    _AutoItObject_AddMethod($oSelf, "AnnounceName", "_announceName")
    _AutoItObject_AddMethod($oSelf, "ToggleState", "_toggleState")
    Return $oSelf
EndFunc ;==>LightSwitch

Func _displayName()
    MsgBox(0, "Name:", $oSelf.Name)
EndFunc ;==>_displayName

Func _status($oSelf)
    Return $oSelf.State
EndFunc ;==>_status

Func _toggleState($oSelf)
    $oSelf.State = Not $oSelf.State
EndFunc ;==>_toggleState
#endregion LightSwitch
Edited by jaberwocky6669
Link to comment
Share on other sites

AutoIt still isn't the right place to use object orientation.

Don't chase him off too quickly, I want to see where these _AutoItObject_* functions came from first!

:idea:

Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law
Link to comment
Share on other sites

I know I have another thread in the dev chat but that is a general oop thread. THis one is more specific to autoit so thats why I started one here.

Was that a bad idea?

Edited by jaberwocky6669
Link to comment
Share on other sites

Allright, I included the au3 file :)

Ah, I see the original topic now: AutoItObject UDF -- Creating and using objects in AutoIt

:idea:

Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law
Link to comment
Share on other sites

I have two classes which share a few functions. How do I make one class inherit from another? Please no jokes about wills, ahem, jrowe =P !

Here is how to inherit.

#region Inheritance example
#include "AutoItObject.au3"
_AutoItObject_Startup()

$oObject = _oObject()
$oObject.InheritedMehtod
$oObject.OwnMehtod

Func _oInheritMe()
    Local $self = _AutoItObject_Create()
    ; _oInheritMe Proprty
    _AutoItObject_AddProperty($self, "Inherited", $ELSCOPE_PRIVATE, "I'm from _oInheritMe")
    ; _oInheritMe Mehtod
    _AutoItObject_AddMethod($self, "InheritedMehtod", "_oInheritMe_InheritedMehtod")
    return $self
EndFunc
; _oInheritMe Mehtods
Func _oInheritMe_InheritedMehtod($self)
    MsgBox(0, "_oInheritMe", $self.Inherited)
EndFunc

Func _oObject()
    Local $self = _AutoItObject_Create(_oInheritMe()) ; This line does the inheriting by placing the inherited inside the _AutoItObject_Create's brackets
    ; _oObject Proprty
    _AutoItObject_AddProperty($self, "Own", $ELSCOPE_PRIVATE, "I'm from myself")
    ; _oObject Mehtod
    _AutoItObject_AddMethod($self, "OwnMehtod", "_oObject_OwnMehtod")
    return $self
EndFunc
; _oObject Mehtod
Func _oObject_OwnMehtod($self)
    MsgBox(0, "_oObject", $self.Own & " But i also know _oInheritMe Property 'Inherited' which is = " & $self.Inherited)
EndFunc
#endregion Inheritance example

Should I make a class that defines all electrical devices which have Name and State as properties and AnnounceName, AnnounceState, and Toggle as methods and then make two objects (light and switch) based on that class?

OR, should I do as I stated in the previous line but make a private controller class that monitors the state of the switch and changes the light bulb accordingly?

I guess that all depends on what you want it do , you have a problem to solve you do only whats necessary to solve the problem and if you want to cover a few things that might change keep that in mind while doing so.

The user programmer will only have access to the switch object. The lightbulb stays hidden. But I want the lightbulb to automagically turn on and off when someone toggles the switch.

This will solve the problem in the way you stated above, i modified your script.

#include "AutoItObject.au3"
_AutoItObject_Startup()

$LightSwitch = _LightSwitch()
$LightSwitch.ToggleState
$LightSwitch.ToggleState

#region _LightBulb
Func _LightBulb()
    Local $oSelf = _AutoItObject_Create()
    ; properties
    _AutoItObject_AddProperty($oSelf, "Name", $ELSCOPE_PRIVATE, "Light Bulb")
    _AutoItObject_AddProperty($oSelf, "State", $ELSCOPE_PRIVATE, False)
    ; methods
    _AutoItObject_AddMethod($oSelf, "AnnounceState", "_announceState", True)
    _AutoItObject_AddMethod($oSelf, "AnnounceName", "_announceName", True)
    _AutoItObject_AddMethod($oSelf, "SetState", "_SetState", False)
    Return $oSelf
EndFunc ;==>_LightBulb

;~ Func _displayName()   ; not required has no method above
;~     MsgBox(0, "Name:", $oSelf.Name)
;~ EndFunc ;==>_displayName

Func _SetState($oSelf, $fState)
    $oSelf.State = $fState
    $oSelf.AnnounceState
EndFunc ;==>_toggleState

Func _announceState($oSelf)
    MsgBox(0,"I'm the " & $oSelf.Name, "My state just changed to " & $oSelf.State)
EndFunc
#endregion LightBulb

#region _LightSwitch
Func _LightSwitch()
    Local $oSelf = _AutoItObject_Create()
    ; properties
    _AutoItObject_AddProperty($oSelf, "Name", $ELSCOPE_PRIVATE, "Light Switch")
    _AutoItObject_AddProperty($oSelf, "State", $ELSCOPE_PRIVATE, False)
    _AutoItObject_AddProperty($oSelf, "LightBulb", $ELSCOPE_PRIVATE, _LightBulb())
    ; methods
    _AutoItObject_AddMethod($oSelf, "Status", "_status")
    _AutoItObject_AddMethod($oSelf, "AnnounceName", "_announceName")
    _AutoItObject_AddMethod($oSelf, "ToggleState", "_ToggleSwitchState")
    Return $oSelf
EndFunc ;==>_LightSwitch

;~ Func _displayName() ; not required has no method above
;~     MsgBox(0, "Name:", $oSelf.Name)
;~ EndFunc ;==>_displayName

Func _status($oSelf)
    Return $oSelf.State
EndFunc ;==>_status

Func _ToggleSwitchState($oSelf)
    $oSelf.State = Not $oSelf.State
    $oSelf.LightBulb.SetState($oSelf.State)
EndFunc ;==>_toggleState
#endregion LightSwitch


Func _ErrFunc()
    ConsoleWrite("! COM Error ! Number: 0x" & Hex($oError.number, 8) & " ScriptLine: " & $oError.scriptline & " - " & $oError.windescription & @CRLF)
EndFunc ;==>_ErrFunc

I've edited the lightswitch/lightbulb as the actual state of the switch was not setting the light state it was just toggling bulbs state and the switch independantly.

Edited by Yoriz
GDIPlusDispose - A modified version of GDIPlus that auto disposes of its own objects before shutdown of the Dll using the same function Syntax as the original.EzMySql UDF - Use MySql Databases with autoit with syntax similar to SQLite UDF.
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...