Em có một bài tập minh họa tính Diện tích hình chữ nhật trong C# như thế này, giả sử bây giờ em không viết chung trong 1 class nữa, mà em sẽ tạo ra 2 Class. 1 Class: Rectangle ( thực thi các phương thức hiển thị chiều dài chiều rộng, tính diện tích)
và 1 class ExecuteRetangle(chứa hàm main để thực thi tất cả các phương thức trong Class: Rectangle thì em phải làm sao ạ!??)
using System;
namespace BaiTapMinhHoa
{
class Rectangle
{
double length;
double width;
static void main(string[] args)
{
Console.WriteLine("Chuong trinh minh hoa huong doi tuong trong C#");
Console.WriteLine("----------------------------------------------");
//tao doi tuong Rectangle
Rectangle r1 = new Rectangle();
//goi cac huong thuc cua doi tuong nay
r1.AcceptDetails();
r1.GetArea();
r1.Display();
Console.ReadLine();
Console.ReadKey();
}
public void AcceptDetails()
{
length = 5.4;
width = 4.5;
}
public double GetArea()
{
return length * width;
}
public void Display()
{
Console.WriteLine("Chieu dai: {0}", length);
Console.WriteLine("Chieu rong: {0}", width);
Console.WriteLine("Dien tich: {0}", GetArea());
}
}
}