什么是超载真假运营商呢? [英] What does overloading true and false operators do?

查看:256
本文介绍了什么是超载真假运营商呢?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只是一个初学者到C#,但我努力学习一切。不过,我被困在超载真假;这是什么意思?请(试图)给出的答复基本越好(因此,即使是13岁老懂逻辑)。
(请以确保使之作为理解越好,谢谢)。
。如果可以的话,请解释的结果会是什么,如果坐标已经变成别的东西(如:(3,5))。



我已经预先作出这里代码:
(你可以租上改变坐标解释不同的输出)
信息:
- 程序是一个控制台应用程序
- C#
口已经取得了一个名为坐标(代表坐标)类。
类里面:

 类坐标
{
私人诠释_x,_y;

公共坐标(INT X,int y)对
{
_x = X;
_y = Y;
}

公众诠释X
{
得到
{
返回_x;
}

{
_x =价值;
}
}

公众诠释Ÿ
{
得到
{
返回_y;
}

{
_y =价值;
}
}

公共静态布尔运算符真(坐标坐标1)
{
如果(coord1.x!= 0 || coord1.y! = 0)
{
返回真;
}
,否则
{
返回FALSE;
}
}

公共静态布尔运算符假(坐标坐标1)
{
如果(coord1.x == 0安培;&安培;坐标1。 Ÿ== 0)
{
返回真;
}
,否则
{
返回FALSE;
}
}
}



在main类节目:

 类节目
{
静态无效的主要(字串[] args)
{
座标座标=新坐标(0,0);

如果(坐标)
{
Console.WriteLine(真);
}
,否则
{
Console.WriteLine(假);
}

到Console.ReadLine();
}
}


解决方案

我只是一个初学者到C#,但我努力学习一切。




请确保分配了几十年最低。我了解这种语言屡有新的东西。




我停留在超载真假; ?这是什么意思




他们很少使用;它们主要需要让用户自定义的&放大器;&安培; || 运营商是短路



阅读我的一系列关于这个问题的文章;你将在第三部分特别感兴趣,但你应该阅读整个事情。



https://ericlippert.com/2012/03/26/null-is-not-false-part-one/




如果可以的话,请解释的结果会是什么,如果坐标已经变成别的东西(如:(3,5))。




如果你想知道当你想改变什么程序做什么,的修改并运行它的,你很快就会知道!


I am only a beginner to C#, but am trying to learn everything. However, I am stuck on the overloading true and false; what does this mean? Please (try to) give replies as basic as possible (so that even a '13' yr old understands the logic). (Please make to sure make it as understandable as possible, thank you). If you can, please explain what the outcome would be if the coord had changed to something else (such as: (3,5)).

I have pre-made code right here: (Can you lease explain the different outputs on changing the coordinates) Info: - Program is a console application - C# I have made a class called Coord (standing for co-ordinates). Inside Class:

class Coord
{
    private int _x, _y;

    public Coord(int x, int y)
    {
        _x = x; 
        _y = y;
    }

    public int x
    {
        get 
        { 
            return _x; 
        }
        set 
        { 
            _x = value; 
        }
    }

    public int y
    {
        get 
        { 
            return _y; 
        }
        set 
        { 
            _y = value; 
        }
    }

    public static bool operator true(Coord coord1)
    {
        if (coord1.x != 0 || coord1.y != 0)
        { 
            return true; 
        }
        else
        { 
            return false; 
        }
    }

    public static bool operator false(Coord coord1)
    {
        if (coord1.x == 0 && coord1.y == 0)
        { 
            return true; 
        }
        else
        { 
            return false; 
        }
    }
}

Inside Main class Program:

class Program
{
    static void Main(string[] args)
    {
        Coord coord = new Coord(0, 0);

        if (coord)
        {
            Console.WriteLine("True");
        }
        else
        {
            Console.WriteLine("False");
        }

        Console.ReadLine();
    }
}

解决方案

I am only a beginner to C#, but am trying to learn everything.

Make sure to allocate a couple of decades minimum. I learn new things about this language quite frequently.

I am stuck on the overloading true and false; what does this mean?

They are seldom used; they are primarily needed to make user-defined && and || operators be short-circuiting.

Read my series of articles on this subject; you will be particularly interested in part three but you should read the whole thing.

https://ericlippert.com/2012/03/26/null-is-not-false-part-one/

If you can, please explain what the outcome would be if the coord had changed to something else (such as: (3,5)).

If you want to know what a program does when you change something, change it and run it and soon you will know!

这篇关于什么是超载真假运营商呢?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆