“这个”的目的是什么? [英] What is the purpose of "this"

查看:101
本文介绍了“这个”的目的是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以有这个是c#,许多教程在线使用,但我不明白它实际上做了什么。



ps用外行术语说明,谢谢你。



i在练习时使用它但却不理解它的用途



我是什么尝试过:



 class Circle 
{
float pi = 3.142f;
int Rad;

public Circle(int Radius)
{
this.Rad = Radius;
}
public float CaluculateAreaMethod()
{
返回this.pi * this.Rad * this.Rad;
}
}
class Program
{
public static void Main()
{
Circle C1 = new Circle(5);
float Area = C1.CaluculateAreaMethod();

Console.WriteLine(Area =+ Area);
}
}

解决方案

关键字指的是当前类型的实例(例如 Circle )。



但是,在这种情况下,它的使用是多余的。没有必要区分它符合条件的字段( pi Rad )。



考虑您是否已将类级别字段 Rad 重新命名为 Radius 。在这种情况下,在圆的构造函数中,会有两个名为 Radius 的东西:构造函数的参数和类级字段。因此,在这种情况下,需要使用以下来区分它们:



 public Circle(int Radius)
{
this.Radius = Radius;
}


Eric绝对正确,但它可以帮助你想到汽车而不是课程。

你习惯了我的车和你的车的概念; 这辆车和那辆车 - 并且意识到它们中的每一个都可以是不同的,或者根据情况而改变。你沿着一排车辆往下看,然后这辆车随着你的移动而改变。汽车不会改变,但是你对它们的引用确实如此。



汽车排是一组汽车的实例,当你走路时,你在心理上依次将这辆车重新分配给每个新实例,这样你就可以用相同的词来讨论每个实例:这辆车是什么颜色的?; 这辆车有什么用?

答案会根据你当前站在哪辆车而改变,因为每次你问,这辆车都会检查你指的是哪辆车?它的具体数据。



C#中的这个服务于同一目的:它指的是当前的一个实例在代码运行时可用的类 - 并且会因为类外的代码使用不同的实例而改变。



它始终可用 - 静态方法除外不处理实例,但处理影响所有实例的事项:有多少车轮有车?例如,这是一个静态问题,因为你不必在车轮周围走动,计算轮胎的结果是四 - 如果它只有两个,那就是摩托车!



大多数时候你不需要使用它 - 你使用非静态属性或方法暗示 - 你唯一能做的就是当你有一个本地(或参数)变量时与类级别变量同名,你需要明确指定类级别版本。



有意义吗?


通常,文档有助于: this(C#Reference)| Microsoft Docs [ ^ ]。

so there is "this" is c# which many tutorial online use but i don't understand what it actually does.

p.s explain in layman terms, thank you.

i use it while practicing but don't understand its purpose

What I have tried:

class Circle
{
    float pi = 3.142f;
    int Rad;
    
    public Circle (int Radius)
    {
        this.Rad = Radius;
    }
    public float CaluculateAreaMethod()
    {
        return this.pi * this.Rad * this.Rad;
    }
}
class Program
{
    public static void Main()
    {
        Circle C1 = new Circle(5);
        float Area = C1.CaluculateAreaMethod();

        Console.WriteLine("Area = " + Area);
    }
}

解决方案

The this keyword refers to the current instance of the type (e.g. Circle).

However, in this case, its use is superfluous. It is NOT necessary to differentiate the fields (pi and Rad) that it qualifies.

Consider if you had re-named the class-level field Rad as Radius. In that case, within the constructor for the circle, there would be two things named Radius: the parameter to the constructor and the class-level field. So, in that case, the following use of this would be necessary to differentiate between them:

public Circle (int Radius)
{
    this.Radius = Radius;
}


Eric is absolutely right, but it may help you to think of cars instead of classes.
You are used to the concept of "my car", and "your car"; "this car" and "that car" - and aware that each of them can be different, or change as circumstances do. It you walk down a line of vehicles looking at each in turn, then "this car" changes as you move. The cars don't change, but your reference to them does.

The row of cars is a set a instances of the class "car" , and as you walk, you mentally reassign "this car" to each new instance in turn, so you can discuss each instance using the same words: "what colour is this car?"; "what make is this car?"
The answer changes according to which vehicle you are currently standing in front of because each time you ask, "this car" checks which instance of a car you are referring to and it's specific data.

this in C# serves the same purpose: it refers to the current instance of a class that is "available" when your code is running - and will change as your code outside the class uses different instances.

It's always available - except for static methods which don't work with instances but which deal with matters that affect all instances: "how many wheels has a car?" for example is a static question because you don;t have to walk round the vehicle counting the tires to know that the result is "four" - if it only had two it would be a motorcycle!

Most of the time you don;t need to use it - it's implied by your use of a non-static property or method - the only times you do is when you have a local (or parameter) variable that has the same name as a class level variable and you need to specify the class level version explicitly.

Make sense?


Usually, the documentation helps: this (C# Reference) | Microsoft Docs[^].


这篇关于“这个”的目的是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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