Fundamentals Types of Variablesดนุเดช นิลคูหา 650710079
What are Types of Variables?
คือ การตั้งชื่อให้กับตำแหน่งของหน่วยความจำ ทั้งนี้มีกฎของการตั้งชื่ออยู่โดยไม่สามารถตั้งชื่อโดยเป็น keyword หรือนำหน้าด้วยตัวเลข ในภาษา C# จะต้องทำการประกาศตัวแปรทั้งหมดก่อนจึงจะ สามารถใช้งานได้ และค่าที่เก็บไว้ในตัวแปรดังกล่าวสามารถเปลี่ยนแปลงได้ระหว่างการทำงานของโปรแกรม
Topic in Types of Variables?
Declaring Variables
การเริ่มต้นการระบุประเภทข้อมูลของตัวแปรโดยมีรูปแบบ คือ ประเภทตัวแปร ตามด้วยชื่อตัวแปร
Syntax : type variableName = value;
Code
Copy using System ;
class Program {
static void Main () {
int integerVariable = 79 ; //ตัวแปรแบบจำนวนเต็ม integer
float floatVariable = 1.23f ; //ตัวแปรทศนิยมแบบ float
double doubleVariable = 77.77 ; //ตัวแปรทศนิยมแบบ double
char charVariable = 'O' ; //ตัวแปรประเภท char
string stringVariable = "Danudeth" ; //ตัวแปรประเภท String
bool boolVariable = true ; //ตัวแปรประเภท boolean
//แสดงค่าของตัวแปร
Console . WriteLine ( "Integer: " + integerVariable);
Console . WriteLine ( "Float: " + floatVariable);
Console . WriteLine ( "Double: " + doubleVariable);
Console . WriteLine ( "Char: " + charVariable);
Console . WriteLine ( "String: " + stringVariable);
Console . WriteLine ( "Boolean: " + boolVariable);
}
}
Copy Integer: 79
Float: 1.23
Double: 77.77
Char: O
String: Danudeth
Boolean: True
ทั้งนี้ในการประกาศตัวแปรในทุก ๆ ประเภทต้องคำนึงถึงกฎในการตั้งชื่อตัวแปรด้วย
ความสัมพันธ์ที่แสดงถึงของขอบเขต (scope) และช่วงอายุของตัวแปร (lifetime) Local Variables
ตัวแปรที่กำหนดไว้ใน Method เรียกว่า Local Variables มักใช้สำหรับจัดเก็บข้อมูลชั่วคราวภายในโปรแกรม โดยมีขอบเขตการจัดการเฉพาะใน Method นี้เท่านั้น และมีช่วงอายุของตัวแปร คือ เมื่อมีการเรียกใช้ Method ตัวแปรเหล่านี้จะถูกสร้าง และถูกทำลายหลังจาก Method นี้จบการทำงาน
Copy using System;
class Calculator {
//เมธอด (Method)
public void SumInteger(int a, int b) {
int result = a + b; //Local Variables เพื่อใช้ตัวแปร result ชั่วคราวภายในโปรแกรม
Console.WriteLine("result : " + result);
}
public static void Main(String[] args) {
Calculator ip = new Calculator(); //สร้าง object เพื่อเรียกใช้ Method Sum
ip.SumInteger(100, 79); //เรียกใช้ Method Sum
}
}
Instance Variables
เป็นตัวแปรที่ถูกประกาศในคลาสแต่อยู่นอก Method ตัวแปรเหล่านี้จะถูกสร้างเมื่อมีการสร้าง object ของคลาสสำหรับใช้ในการเข้าถึง ตัวแปรต่าง ๆ โดยค่าของตัวแปรเหล่านี้จะไม่แชร์กันระหว่างวัตถุแต่จะถูกใช้งานตามแต่ละ Object และถูกทำลายเมื่อ Object ถูกทำลาย
Copy using System;
class PersonalInfo {
String name; //ตัวแปรภายนอก Method, Instance Variables
int age;
public static void Main(String[] args) {
PersonalInfo profile1 = new PersonalInfo(); //สร้าง object profile1
profile1.name = "Danudeth";
profile1.age = 20;
PersonalInfo profile2 = new PersonalInfo(); //สร้าง object profile2
profile2.name = "Natchaya";
profile2.age = 21;
Console.WriteLine("object profile1:");
Console.WriteLine(profile1.name);
Console.WriteLine(profile1.age);
Console.WriteLine("object profile2:");
Console.WriteLine(profile2.name);
Console.WriteLine(profile2.age);
}
}
Copy object profile1:
Name: Danudeth
Age: 20
object profile2:
Name: Natchaya
Age: 21
Static Variables
เป็นตัวแปรที่ประกาศจะเป็นคาคงที่ภายในคลาสที่อยู่นอก Method ตัวแปรเหล่านี้จะถูกแชร์ ไม่ต้องใช้ Object ในการเข้าถึงเปลี่ยนแปลงค่าของตัวแปรได้ และรักษาค่าไว้ตลอดทั้งโปรแกรม จะถูกทำลายเมื่อสิ้นสุดโปรแกรม
Copy using System;
class EvenCounter {
static int counter = 0; //ตัวแปรภายนอก Method, Static Variables
static void Main(String[] args) {
Console.WriteLine("counter: " + counter);
counter+=2;
Console.WriteLine("counter: " + counter);
counter+=2;
Console.WriteLine("counter: " + counter);
}
}
Copy counter: 0
counter: 2
counter: 4
Constant Variables
เป็นตัวแปรที่ใช้ const จะสื่อถึงตัวแปรคงที่ โดยจะไม่สามารถแก้ไขได้หลังจากการประกาศตัวแปรได้ตลอดทั้งโปรแกรม โดยไม่ต้องใช้ Object ในการเข้าถึง ช่วยให้อ่านโค้ดได้ง่าย และป้องกันการแก้ไขค่าของตัวแปรโดยไม่ตั้งใจ
Copy using System;
class NotModify {
const int a = 1; //Constant Variables
public static void Main(){
//Program.a++; error ไม่สามารถเปลี่ยนแปลงได้
Console.WriteLine("a: " + NotModify.a);
}
}
Readonly Variables
เป็นตัวแปรที่ไม่สามารถแก้ไขค่าได้ โดยจะถูกใช้งานหลังจากที่มีการสร้าง Object ของคลาสเท่านั้น โดยสามารถ สร้างค่าเริ่มต้นสำหรับตัวแปรที่ไม่ได้ถูกกำหนดไว้ตอนแรกผ่าน Constructor
Copy using System;
class NotModifyUseObject {
readonly int a = 1; //Readonly Variables
readonly int b;
public NotModifyUseObject() { //Constructor ถูกใช้งานเมื่อมีการสร้าง ObjectของClass
this.b = 11;
}
public static void Main() {
NotModifyUseObject ro = new NotModifyUseObject(); //สร้าง Object
Console.WriteLine("a: " + ro.a);
Console.WriteLine("b: " + ro.b);
}
}
Compare with another language
Language
Local Variables
Instance Variables
Static Variables
Constant Variables
Readonly Variables
ในส่วนที่ไม่มีเครื่องหมาย ✅ ไม่ใช่ว่าไม่มีในภาษานั้น แต่หมายถึง มีการใช้งานผ่านชื่อของการประกาศที่แตกต่างกัน หรือสามารถจำลองการใช้งานได้ และมีการใช้รูปแบบที่เป็นมาตรฐานเพื่อสื่อความหมายที่เป็นสากล โดยมีโค้ดตัวอย่าง ดังต่อไปนี้
ชื่อประเภทในการประกาศตัวแปรที่ต่างกันในแต่ละภาษา
Copy class JavaReadOnly {
public final int readonlyVariable = 50; //Readonly Variable (ผ่าน final)
}
Copy #include <stdio.h>
struct StructInstance {
int instanceVariable; //Instance Variable ผ่าน struct จึงกำหนดค่าไม่ได้
};
Copy #include <stdio.h>
int main() {
const int readonlyVariable = 50; // Readonly Variable (ผ่าน const)
return 0;
}
จำลองการใช้งาน และไม่มีให้ใช้งานโดยตรง
Copy class PythonStatic:
static_variable = 10 #Static Variable จำลองการใช้งานได้ แต่ไม่ได้ระบุโดยตรง
Copy class PythonReadOnly:
def __init__(self):
self._readonly_variable = 1 #Readonly Variable
@property
def readonly_variable(self):
return self._readonly_variable
#จำลองด้วยการใช้ property เป็นการสร้างตัวแปรที่ไม่สามารถเปลี่ยนแปลงค่าได้ภายนอกคลาส
Copy CONSTANT_VARIABLE = 1 #Constant Variable
#โดยภาษา Python ไม่มีการกำหนดโดยตรง แต่จะใช้การตั้งชื่อด้วยตัวพิมพ์ใหญ่เป็นมาตรฐาน
Video Presentation
Slide Presentation
Reference
ชนิดตัวแปรในภาษา C# ประเภทสำหรับแบ่งตัวแปรในขอบเขต และอายุของตัวแปรต่าง ๆ การประกาศ และกฎการสร้างตัวแปร / รูป : ความสัมพันธ์ที่แสดงถึงของขอบเขต (scope) และช่วงอายุของตัวแปร (lifetime) มาตรฐานในการตั้งชื่อเมธอด (Method) ที่นิยมใช้ในภาษา C# Type of Variables in Java Property in Python for Readonly Variables Constant in Python
Last updated 7 months ago