In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-26 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article introduces the knowledge of "how to overload more operators in C #". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!
Example
Using System
Namespace OperatorOvlApplication
{
Class Box
{
Private double length; / / length
Private double breadth; / / width
Private double height; / / height
Public double getVolume ()
{
Return length * breadth * height
}
Public void setLength (double len)
{
Length = len
}
Public void setBreadth (double bre)
{
Breadth = bre
}
Public void setHeight (double hei)
{
Height = hei
}
/ / overload + operator to add two Box objects
Public static Box operator+ (Box b, Box c)
{
Box box = new Box ()
Box.length = b.length + c.length
Box.breadth = b.breadth + c.breadth
Box.height = b.height + c.height
Return box
}
Public static bool operator = (Box lhs, Box rhs)
{
Bool status = false
If (lhs.length = = rhs.length & & lhs.height = = rhs.height
& & lhs.breadth = = rhs.breadth)
{
Status = true
}
Return status
}
Public static bool operator! = (Box lhs, Box rhs)
{
Bool status = false
If (lhs.length! = rhs.length | | lhs.height! = rhs.height
| lhs.breadth! = rhs.breadth) |
{
Status = true
}
Return status
}
Public static bool operator rhs.breadth)
{
Status = true
}
Return status
}
Public static bool operator = rhs.breadth)
{
Status = true
}
Return status
}
Public override string ToString ()
{
Return String.Format ("({0}, {1}, {2})", length, breadth, height)
}
}
Class Tester
{
Static void Main (string [] args)
{
Box Box1 = new Box (); / / declare Box1 with type Box
Box Box2 = new Box (); / / declare Box2 with type Box
Box Box3 = new Box (); / / declare Box3 with type Box
Box Box4 = new Box ()
Double volume = 0; / / Volume
/ / Box1 details
Box1.setLength (6.0)
Box1.setBreadth (7. 0)
Box1.setHeight (5.0)
/ / Box2 details
Box2.setLength (12.0)
Box2.setBreadth (13.0)
Box2.setHeight (10.0)
/ / use overloaded ToString () to display two boxes
Console.WriteLine ("Box1: {0}", Box1.ToString ())
Console.WriteLine ("Box2: {0}", Box2.ToString ())
/ / Volume of Box1
Volume = Box1.getVolume ()
Console.WriteLine ("volume of Box1: {0}", volume)
/ / Volume of Box2
Volume = Box2.getVolume ()
Console.WriteLine ("volume of Box2: {0}", volume)
/ / add two objects together
Box3 = Box1 + Box2
Console.WriteLine ("Box3: {0}", Box3.ToString ())
/ / Volume of Box3
Volume = Box3.getVolume ()
Console.WriteLine ("volume of Box3: {0}", volume)
/ / comparing the boxes
If (Box1 > Box2)
Console.WriteLine ("Box1 is greater than Box2")
Else
Console.WriteLine ("Box1 is not greater than Box2")
If (Box1 = Box2)
Console.WriteLine ("Box1 greater than or equal to Box2")
Else
Console.WriteLine ("Box1 is not greater than or equal to Box2")
If (Box1
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.