Open Aida-Hagh opened 5 months ago
public class Car { public int speed; public int shetab;
public static Car operator +(Car car1, Car car2)
{
Car s = new Car()
{
speed = car1.speed + car2.speed,
shetab = car1.shetab + car2.shetab
};
return s;
}
public static Car operator + (Car car1)
{
Car s = new Car()
{
speed = car1.speed + 1,
shetab = car1.shetab + 1
};
return s;
}
public static Car operator - (Car car1, int x)
{
Car s = new Car()
{
speed = car1.speed - x,
shetab = car1.shetab - x
};
return s;
}
}
class Program { static void Main() { Car pride = new Car(){ speed=70, shetab=20 };
Car benz = new Car(){ speed=370, shetab=120 };
**Car cars = pride + benz; System.Console.WriteLine(cars.speed); //ج:440 System.Console.WriteLine(cars.shetab); //ج:140
Car cars2 = +benz; System.Console.WriteLine(cars2.speed); //ج:371 System.Console.WriteLine(cars2.shetab); //ج:121
Car cars3 = benz - 20; System.Console.WriteLine(cars3.speed); //ج:350 System.Console.WriteLine(cars3.shetab); //ج:100** } }
( بیشبارگذاری اپراتورها)OverLoading Operators:
با استفاده از کلمه کلیدی operator و تعریف یک تابع استاتیک انجام میشود.