C # operator overloading

Source: Internet
Author: User
Tags arithmetic arithmetic operators

The 11.5.1 of the problem

In object-oriented program design, defining a class yourself is equivalent to creating a new type. Class can be passed as a parameter or as a return type, as is the case with a variable.

In the seventh chapter, we introduce many of the operators of the system definition. For example, for two integer variables, arithmetic operators can be used for simple arithmetic operations:

Class A
{public
 int x;
 public int y;
 public int plus{return
    x+y
  }
}
Again, for example, we want to add the content of two instances that belong to different classes:

class B
{public
  int x;
}
Class Test
{public
 int z;
 public static void main{
    a a=new a ();
    b b=new B ();
    z=a.x+b.x
 }
}

Use a.x+b.x This kind of writing is not concise, also not intuitive. A more serious problem is that this access is illegal if the members of the class are not using the PUBLIXC modifier when declared.

We know that in C #, all data is either a class or an instance of a class that fully embodies the object-oriented idea. Therefore, for the sake of ease of expression, it is desirable to give new meaning to the defined operator, and to make a new interpretation on an instance of a particular class. This needs to be solved by operator overloading.

11.5.2 using member methods to overload operators

In C #, operator overloading is always declared in a class and is implemented by invoking the member methods of a class.

The format of the operator overload declaration is:

Type operator Operator-name (formal-param-list)

In C #, the following operators can be overloaded:

+ - ! ~ + +--true false

*/% & | ^ << >> =!= > < >= <=

But there are also some operators that are not allowed to be overloaded, such as:

=,&&,| |,?:,new,typeof,sizeof,is

Unary operator overload

As the name implies, the unary operator overloads the operator only for one object, when the parameter table is empty, and the current object acts as a single operand of the operator.

Here's a list of examples that are often encountered in a role game. The role has five attributes of internal force, physical strength, experience value, residual strength, residual internal force, and when the experience value reaches a certain program, the role will be upgraded, physical strength, internal force rise, surplus physical strength and internal force fill. "Upgrade" We use the overloaded operator "+ +" to implement.

Program Listing 11-10:

Using System;
Class Player
{public
 int neili;
 public int tili;
 public int Jingyan;
 public int neili_r;
 public int tili_r;
 Public Player ()
 {
   neili=10;
   tili=50;
   jingyan=0;
   neli_r=50;
   tili_r=50;
 }
 public static player operator + + (Player p) {
   p.neili=p.neili+50;
   p.tili=p.tili+100;
   P.neili_r=p.neili;
   P.tili_r=p.tili;
   return p;
 }
 public void Show ()
 {
  Console.WriteLine ("Tili:{0}", tili);
  Console.WriteLine ("Neili:{0}", Neili);
  Console.WriteLine ("Tili_full:{0}", Tili_r);
  Console.WriteLine ("Neili_full:{0}", Neili_r);
 }
Class Test
{public
 static void Main () {
    player man=new player ();
    Man. Show ();
    man++;
    Console.WriteLine ("Now Upgrading ...:");
    Man. Show ();
 }

Binary operator overload

In most cases we use the two-dollar operator overload. The parameter table has a parameter, the current object as the left-hand operand of the operator, and the argument as the right-hand operand of the operator.

Here we give a simple example of the two-dollar operator overload, that is, the Cartesian coordinates are added.

Program Listing 11-11:

Using System;
Class DKR
{public
  int x,y,z;
  Public DKR (int vx,int vy,int vz) {
     x=vx;
     Y=vy;
     Z=vz;
  }
  public static DKR operator + (DKR D1,dkr D2)
  {
  DKR dkr=new DKR (0,0,0);
  dkr.x=d1.x+d2.x;
  DKR.Y=D1.Y+D2.Y;
  dkr.z=d1.z+d2.z;
  return DKr
  }
}
Class Test
{
  publixc statixc void Main () {
    DKR d1=new DKR (3,2,1);
    DKR d2=new DKR (0,6,5);
    DKR d3=d1+d2;
    Console.WriteLine ("The 3d location of D3 is:{0},{1},{2}", d3.x,d3.y,d3,z);
  }

Try compiling and running the program to see if the results are the same as expected.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.