หากอยากใช้ for loop ในการสร้างวัตถุแบบอัตโนมัติสามารถแก้ไขใน method Main( ) และเราจะมีการกำหนดค่าแบบอาร์เรย์ไปพร้อมกันกับการสร้างวัตถุ ซึ่งสามารถทำได้ดังนี้
4.แบบ Type Inference
เป็นการสร้างวัตถุขึ้นมาโดยใช้คำสั่ง var ร่วมกับคำสั่ง new ส่งผลให้ทำให้ตัวแปรของวัตถุนั้นมีชนิดข้อมูลตรงกับวัตถุที่ถูกสร้างขึ้นโดยไม่ต้องระบุชนิดข้อมูลชัดเจน
บรรทัดที่ 23 Person person1; คือการสร้างตัวแปร person1 ซึ่งเป็น instance ของ class Person โดย person1 เป็นวัตถุที่มีสมาชิกภายในคือ name และ age ซึ่งสามารถเข้าถึงได้ผ่านวัตถุนี้ รวมถึงการใช้ฟังก์ชันภายในคลาส Person อีกด้วย ทั้งนี้เนื่องจากไม่มีคำสั่ง new จึงจัดสรรพื้นที่ใน stack ซึ่งวัตถุจะถูก deallocate โดยอัตโนมัติเมื่อออกจากขอบเขต หรือในที่นี้คือเมื่อฟังก์ชัน main() จบการทำงาน และถ้าเราอยากใช้คำสั่ง new สามารถทำได้ดังนี้
ในบรรทัดที่ 4 Person* person1 = new Person; ด้วยคำสั่ง new นี้ จะทำให้จองพื้นที่ใน heap โดยมี pointer person1 ชี้ไปที่วัตถุ Person เพื่ออ้างอิงและต้องมีการใช้ delete กับ person1 เพื่อคืนหน่วยความจำเมื่อไม่ต้องการใช้อีกต่อไปซึ่งต่างจาก C# ที่มี garbage collector คอยจัดการ deallocate พื้นที่ของวัตถุให้อัตโนมัติ
บรรทัดที่ 8 CreateObjectExample1 obj = new CreateObjectExample1();เป็นการสร้างวัตถุใหม่นั่นคือ obj จากคลาส CreateObjectExample1 ด้วยคำสั่ง new
manasikirloskar. (2021, August 18). Different ways to create an Object in C#. GeeksforGeeks. https://www.geeksforgeeks.org/different-ways-to-create-an-object-in-c-sharp/
BillWagner. (2023, November 14). new operator - Create and initialize a new instance of a type - C# reference. Microsoft Learn. https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/operators/new-operator
BillWagner. (2021, September 17). Objects - create instances of types - C#. Microsoft Learn. https://learn.microsoft.com/en-us/dotnet/csharp/fundamentals/object-oriented/objects
Zeeshan, M. (2024, February 2). How to declare an array of objects in C#. Delft Stack. https://www.delftstack.com/howto/csharp/csharp-array-of-objects/
Kexugit. (2019, October 24). C++ -> C#: What You Need to Know to Move from C++ to C#. Microsoft Learn. https://learn.microsoft.com/en-us/archive/msdn-magazine/2001/july/c-csharp-what-you-need-to-know-to-move-from-c-to-csharp
Dang, T. (2024, September 24). C# vs C++: A Side-by-Side Comparison for Beginners. Orient Software. https://www.orientsoftware.com/blog/csharp-vs-cplusplus/
BillWagner. (2024, April 9). Tips for Java Developers - A tour of C#. Microsoft Learn. https://learn.microsoft.com/en-us/dotnet/csharp/tour-of-csharp/tips-for-java-developers
Timilehin, O. (2023, January 19). Want to explore the OOP World of Java and Python? Read This. Linkedin. https://www.linkedin.com/pulse/want-explore-oop-world-java-python-read-olowookere-timilehin
using System;
public class Person
{
public string Name { get; set; } //คลาสมี properties 2 ตัวคือ Name,Age
public int Age { get; set; }
public Person(string name, int age) //constructor กำหนด ค่า Name และ Age
{
Name = name;
Age = age;
}
}
class Program
{
static void Main()
{
// สร้าง object จากคลาส Person ชื่อ person1 โดยกำหนดค่า Name = "Leopold",Age = 6.
Person person1 = new Person("Leopold", 6);
Console.WriteLine("person1 Name = {0} Age = {1}", person1.Name, person1.Age);
Person person2 = person1; //สร้างการ reference
// เมื่อเปลี่ยนค่า Name,Age ด้วย person2,ค่า Name,Age ใน person1 ก็โดนเปลี่ยนไปด้วย
person2.Name = "Molly";
person2.Age = 16;
Console.WriteLine("person2 Name = {0} Age = {1}", person2.Name, person2.Age);
Console.WriteLine("person1 Name = {0} Age = {1}", person1.Name, person1.Age);
}
}
person1 Name = Leopold Age = 6
person2 Name = Molly Age = 16 //เมื่อ person2 เปลี่ยนค่า Name และ Age
person1 Name = Molly Age = 16 // ส่งผลต่อ person1 ด้วย
className[] arrayName = new className[size];
using System;
class employee {
//มีฟิลด์แบบ private ทั้งหมด 3 ฟิลด์ คือ empName empId และ empDesig
private string empName;
private int empId;
private string empDesig;
// method EmpInfo รับข้อมูลพนักงานและกำหนดค่าให้กับฟิลด์ต่างๆ
public void EmpInfo(string empName, int empId, string empDesig) {
this.empId = empId;
this.empDesig = empDesig;
this.empName = empName;
}
//method showEmployeeDetails ใช้สำหรับแสดงข้อมูลพนักงานที่เก็บไว้
public void showEmployeeDetails() {
Console.WriteLine("\nEmployee Record: ");
Console.WriteLine("\t Emp Name: " + empName);
Console.WriteLine("\t Id : " + empId);
Console.WriteLine("\tDesignation : " + empDesig);
}
}
class EmployeeTest {
public static void Main() {
//ประกาศอาเรย์ของวัตถุ
employee[] e = new employee[3];
//สร้างวัตถุให้แต่ละตำแหน่งของอาเรย์
e[0] = new employee();
e[1] = new employee();
e[2] = new employee();
//ใช้method EmpInfo กำหนดข้อมูลพนักงานแต่ละคนให้กับวัตถุในอาเรย์
e[0].EmpInfo("Shan", 132, "Manager"); //กำหนดค่าให้วัตถุตำแหน่งที่ 0
e[0].showEmployeeDetails();//แสดงข้อมูลพนักงานของวัตถุตำแหน่งที่ 0
e[1].EmpInfo("Casper", 131, "CEO");//กำหนดค่าให้วัตถุตำแหน่งที่ 1
e[1].showEmployeeDetails();//แสดงข้อมูลพนักงานของวัตถุตำแหน่งที่ 1
e[2].EmpInfo("Olga", 139, "Team Leader");//กำหนดค่าให้วัตถุตำแหน่งที่ 2
e[2].showEmployeeDetails();//แสดงข้อมูลพนักงานของวัตถุตำแหน่งที่ 2
}
}
Employee Record:
Emp Name: Shan
Id : 132
Designation : Manager
Employee Record:
Emp Name: Casper
Id : 131
Designation : CEO
Employee Record:
Emp Name: Olga
Id : 139
Designation : Team Leader
public static void Main()
{
// ประกาศอาเรย์ของ Employee ขนาด 3 ช่องเหมือนเดิม
Employee[] employees = new Employee[3];
// เตรียมข้อมูลพนักงานในรูปแบบอาเรย์(กรณีอยากกำหนดค่าให้วัตถุแบบอาเรย์ด้วย)
string[] names = { "Shan", "Casper", "Olga" };
int[] ids = { 132, 131, 139 };
string[] designations = { "Manager", "CEO", "Team Leader" };
// ใช้ for loop ในการสร้างและกำหนดค่าให้กับวัตถุในอาเรย์แบบอัตโนมัติ
for (int i = 0; i < employees.Length; i++){
employees[i] = new Employee(); // สร้างวัตถุ Employee ใหม่
employees[i].EmpInfo(names[i], ids[i], designations[i]); // กำหนดค่าให้วัตถุ
}
// ใช้ for loop ในการแสดงรายละเอียดของพนักงานแต่ละคน ตามตำแหน่งอาเรย์ของวัตถุ
for (int i = 0; i < employees.Length; i++){
employees[i].showEmployeeDetails(); // แสดงข้อมูลพนักงาน
}
}
var ObjectName = new className();
using System;
class Customer {
//คลาส Customer มีสาม properties คือ CustomerID, Name, และ Email
public int CustomerID { get; set; }
public string Name { get; set; }
public string Email { get; set; }
//constructor ของคลาส Customer
public Customer(int id, string name, string email) {
CustomerID = id;
Name = name;
Email = email;
}
//method แสดงข้อมูลลูกค้า
public void DisplayCustomerInfo() {
Console.WriteLine($"Customer ID: {CustomerID}");
Console.WriteLine($"Name: {Name}");
Console.WriteLine($"Email: {Email}");
}
}
class Program {
static void Main(string[] args) {
// การสร้างวัตถุ customer ด้วย var
var customer = new Customer(101, "Alice", "alice@example.com");
// เรียกใช้เมธอดเพื่อแสดงข้อมูลของลูกค้า
customer.DisplayCustomerInfo();
}
}
Customer ID: 101
Name: Alice
Email: alice@example.com
#include <iostream>
#include <string>
using namespace std;
//คลาส Person
class Person {
public:
// มีสมาชิกสองตัว คือ name และ age
string name;
int age;
// ฟังก์ชัน introduce() ใช้ในการแสดงข้อความ โดยใช้ข้อมูลจาก name และ age
void introduce()
{
cout << "Hi, my name is " << name << " and I am "
<< age << " years old." << endl;
}
};
int main()
{
// สร้างวัตถุ person1 จากคลาส Person
Person person1;
// กำหนดค่าให้ name และ age ผ่านวัตถุ person1
person1.name = "Alice";
person1.age = 30;
// เรียกใช้ฟังก์ชัน introduce() ผ่านวัตถุ person1 เพื่อแสดงข้อมูล
person1.introduce();
return 0;
}
public class CreateObjectExample1 {
// method show() ซึ่งแสดงข้อความ
void show() {
System.out.println("Welcome to javaTpoint");
}
public static void main(String[] args) {
// การสร้างวัตถุใหม่จากคลาส CreateObjectExample1
CreateObjectExample1 obj = new CreateObjectExample1();
// การเรียกใช้ show() ผ่านวัตถุ obj
obj.show();
}
}