[AHK]面向对象练习--温度转换

这篇博客展示了如何将C++中的温度类转换为AutoHotkey脚本。作者通过创建一个名为`temperature`的类,实现了摄氏度和华氏度之间的转换,并提供了设置和获取温度的方法。主要功能包括默认构造函数、设定温度值、打印温度值等,旨在帮助理解AutoHotkey中的对象导向编程。
摘要由CSDN通过智能技术生成
;~ 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() 

调试输出

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值