当前位置:网站首页>C simple understanding - overloaded operator
C simple understanding - overloaded operator
2022-06-13 03:06:00 【cathy18c】
What are overloaded operators : utilize The existing Some kind of operator , in the light of Custom class or structure , Define an operation .
1, Instead of creating new operators ,
2, Custom classes or custom structures
Grammatical details :
public static Dogoperator +(Dog male,Dog female){ // The return value is Dog operator + Is an overloaded addition operator
……
return new Dog();
}
Which operators can be overloaded :
1, Unary and binary operators can be overloaded
2, Be careful : If it is a unary operator , The operand must be a class or structure If it is a binary operator , At least one of the two operands is a class or structure
namespace ConsoleApp1
{
class Program
{
static void Main(string[] args)
{
Pet[] pet = new Pet[] { new Cat("Cathy"), new Dog("Jack"), new Dog("Merry") };
for (int i = 0; i < pet.Length; i++)
{
pet[i]++; // The program runs here , The following... Will be called automatically ++ Overloading methods
pet[i].ShowAge();
}
}
}
class Pet {
private string name;
private int age;
public string Name { get => name; set => name = value; }
public int Age { get => age; set => age = value; }
public Pet(string name)
{
this.Name = name;
}
public static Pet operator ++(Pet pet) {//public static Is a must , return Pet type , Adopt self increasing heavy load
++pet.age;
return pet;
}
public void ShowAge()
{
Console.WriteLine(name+" In the age of :"+age);
}
}
class Dog : Pet {
public Dog(string name):base(name) { }
}
class Cat : Pet {
public Cat(string name) : base(name) { }
}
}
边栏推荐
- JVM class loading (I)
- Modify the color of El input, textarea and El checkbox when they are disabled
- HEAP[xxx.exe]: Invalid address specified to RtlValidateHeap( 0xxxxxx, 0x000xx)
- 【pytorch 記錄】pytorch的變量parameter、buffer。self.register_buffer()、self.register_parameter()
- Control scanner in Delphi
- JS multiple judgment writing
- Stack: daily temperature
- mysql索引
- Vscode liveserver use_ Liveserver startup debugging
- Pycharm installation pyqt5 and its tools (QT designer, pyuic, pyrcc) detailed tutorial
猜你喜欢

JS merge multiple string arrays to maintain the original order and remove duplicates

Linked list: the first coincident node of two linked lists

Radio design and implementation in IVI system

The weight of the input and textarea components of the applet is higher than that of the fixed Z-index

SQL execution process in MySQL (3)

Operating principle of JS core EventLoop

JMeter quick start

Mvcc and bufferpool (VI)
![HEAP[xxx.exe]: Invalid address specified to RtlValidateHeap( 0xxxxxx, 0x000xx)](/img/c9/884aa008a185a471dfe252c0756fc1.png)
HEAP[xxx.exe]: Invalid address specified to RtlValidateHeap( 0xxxxxx, 0x000xx)

专业的数据库管理软件:Valentina Studio Pro for Mac
随机推荐
Vant realizes the adaptation of mobile terminal
Image classification system based on support vector machine (Matlab GUI interface version)
Hash table: least recently used cache
Ijkplayer source code - setting options
JVM class loader (2)
Introduction and download of common data sets for in-depth learning (with network disk link)
Installing the IK word breaker
PK of dotnet architecture
How to become a technological bull -- from the bull man
. New features in net 6.0 _ What's new in net 6.0
Svg filter effect use
Linked lists: rearranging linked lists
wx. Createselectorquery() gets the usage of DOM nodes in components
SQL execution process in MySQL (3)
Few-shot Unsupervised Domain Adaptation with Image-to-Class Sparse Similarity Encoding
JS deconstruction assignment
Prometheus安装并注册服务
如何挑选基金产品?什么样的基金是好基金?
How to select fund products? What kind of fund is a good fund?
【pytorch 記錄】pytorch的變量parameter、buffer。self.register_buffer()、self.register_parameter()