;~ http://www.autohotkey.com/board/topic/74608-oop-example/
;~ I've been working on my AHK OOP skills. To help me better understand Objects in AHK I decided to translate a simple c++ object oriented program to AHK.
;~ For the AHK OOP experts... feel free to improve/correct it. :)
;~ This is the c++ code from a college course:
/*
//========================================================
// Temperature class interface definition
//========================================================
class Temperature {
private:
int f;
int c; // celcius
public:
Temperature(); // constructor method interface (automatically called when an object is instanciated and cant have a return value)
Temperature(int); // constructor method interface (if you use names they are ignored eg. int x, int y)
void print(); // Print temperature value as Celsius and Fahrenheit.
void setTemperatureCelsius(int); // Set the temperature using Celsius.
void setTemperatureFahrenheit(int); // Set the temperature using Fahrenheit.
int getTemperatureCelsius(); // Get the temperature value in Celsius
int getTemperatureFahrenheit(); // Get the temperature value in Fahrenheit
};
//========================================================
// Temperature class implementation
//========================================================
#include <iostream>
//------------------------------------
// constructor: The default value will be 0 Celsius.
//------------------------------------
Temperature::Temperature() {
c = 0;
setTemperatureCelsius(c);
}
//------------------------------------
// constructor: The value will be n Celsius.
//------------------------------------
Temperature::Temperature(int n) {
c = n;
setTemperatureCelsius(c);
}
//------------------------------------
// method: Print temperature value as Celsius and Fahrenheit.
//------------------------------------
void Temperature::print() {
std::cout << "Celcius = " << c << ", Fahrenheit = " << f << "\n";
}
//------------------------------------
// method: set temperature using celsius
//------------------------------------
void Temperature::setTemperatureCelsius(int n) {
c = n;
f = int( (c * (9.0f/5.0f)) + 32.0f ) ;
}
//------------------------------------
// method: set temperature using fahrenheit
//------------------------------------
void Temperature::setTemperatureFahrenheit(int n) {
f = n;
c = int((5.0f/9.0f) * (f-32)); // convert fahrenheit to celsius
}
//------------------------------------
// method: get current temperature value in celcius
//------------------------------------
int Temperature::getTemperatureCelsius() {
return c;
}
//------------------------------------
// method: get current temperature value in fahrenheit
//------------------------------------
int Temperature::getTemperatureFahrenheit() {
return f;
}
//========================================================
// Main
//========================================================
// lab1 assignment
//
// The Global Dynamic Corporation needs to keep track of various temperature values.
// They would like you to create a new class that can store temperatures as either Fahrenheit or Celsius values.
// The data type for temperatures will be an integer.
// Please write this class and a main program to test all of the methods of your temperature class.
// Your main program should create at least 3 temperature objects and at a minimum test for:
//
// 1. Creating a temperature object with the default constructor call and then printing its value as Fahrenheit and Celsius.
// 2. Creating a temperature object with the initial value of 100 and then printing its value as Fahrenheit and Celsius.
// 3. Creating a temperature object with the default constructor call and then setting its value to 212 as Fahrenheit and then
// printing its value as Fahrenheit and Celsius.
// 4. Your program will also need to test all of the methods that you have written.
//
#include <iostream>
int main() {
// Create a temperature object with the default constructor call and then print its value as Fahrenheit and Celsius.
Temperature temp1;
temp1.print();
// Create a temperature object with the initial value of 100 and then print its value as Fahrenheit and Celsius.
Temperature temp2(100);
temp2.print();
// Create a temperature object with the default constructor call and then set its value to 212 as Fahrenheit and then print its value as Fahrenheit and Celsius.
Temperature temp3;
temp3.setTemperatureFahrenheit(212);
temp3.print();
// Set its value to 22 as Celsius and then print its value as Fahrenheit and Celsius.
temp3.setTemperatureCelsius(22);
// test remaining unused Temperature class methods
std::cout << "temp3 in C = " << temp3.getTemperatureCelsius() << "\n";
std::cout << "temp3 in F = " << temp3.getTemperatureFahrenheit() << "\n";
return 0;
}
Here is the same logic in ahk (much simpler I think):
*/
AHK代码
;-----------------------------------------
; Class
;-----------------------------------------
class temperature
{
print(){
outputdebug % "c = " this.c " , f = " this.f
}
setTemperatureFahrenheit(n){
this.f := n
this.c := (5/9 * (n-32))
}
setTemperatureCelsius(n){
this.c := n
this.f := (this.c * (9/5)) + 32
}
getTemperatureCelsius(){
return this.c
}
getTemperatureFahrenheit(){
return this.f
}
__New(n=""){
if (n="") {
this.c := 0
} else {
this.c := n
}
}
}
;-----------------------------------------
; Main
;-----------------------------------------
outputdebug DBGVIEWCLEAR
temp1 := new temperature
temp1.print()
temp2 := new temperature(200)
temp2.print()
; Create a temperature object with the default constructor call and then set its value to 212 as Fahrenheit and then print its value as Fahrenheit and Celsius.
temp3 := new temperature
temp3.setTemperatureFahrenheit(212)
temp3.print()
; Set its value to 22 as Celsius and then print its value as Fahrenheit and Celsius.
temp3.setTemperatureCelsius(3)
temp3.print()
outputdebug % "Celsius = " temp3.getTemperatureCelsius()
outputdebug % "Fahrenheit = " temp3.getTemperatureFahrenheit()
调试输出