为什么在不使用它的情况下可以使用此关键字? [英] Why use this keyword when we can do same without using it?

查看:88
本文介绍了为什么在不使用它的情况下可以使用此关键字?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我很困惑,为什么我们使用这个关键字,我们可以通过使用属性名称直接引用来做到这一点。以下是我使用名称 this.name 的示例。使用关键字有什么意义,如果可以通过其他方式实现相同的目标。

I am confused about why do we use this keyword when we can do same by directly referring using the attribute name. Below is example where I used name and this.name. What is the point in using this keyword if same can be achieved other way.

class Myclass
{
    String name;
    public Myclass(String n1)// n1 represents name
    {
        name=n1;
    }

    public Myclass(String firstname,String lastname)
    {
        this.name = firstname + " " + lastname;
    }
}

推荐答案

这不是使用this关键字的好例子。通常你不会做你写的,至少不是干净的代码。



this用于将类的实例传递给其他函数。您可以使用this关键字来引用类的当前实例或传递给其他子例程,但是您通常不会使用this关键字来访问您正在使用的类中的数据。它不违反关键字,但只是不添加任何价值。







我是不确定这在Java中是否有效,但在c#中我们也可以通过以下方式使用它:



Its not a good example of the use of the "this" keyword. Typically you wouldn't do what you wrote, at least not in clean code.

"this" is used to pass the instance of the class to other functions. You can use the "this" keyword to reference the current instance of the class or pass to other subroutines, but you typically wouldn't use the "this" keyword to access data in the class you are working in. Its not a violation of the keyword, but just doesn't add any value.



I'm not sure if this is valid in Java, but in c# we can also use it in the following way:

class Myclass
{
    String firstname;
    public Myclass(String n1)// n1 represents name
    {
        firstname=n1;
    }
 
    public Myclass(String firstname,String lastname)
    {
        this.firstname = firstname + " " + lastname;
    }
}





区分参数和班级成员。



[/ Edit]



To differentiate between the parameter and the class member.

[/Edit]


首先,到目前为止,唯一正确的解决方案是解决方案1;你应该正式接受解决方案1,并且可能应该拒​​绝解决方案2:可读性很重要,但是解析名称非常重要,并且在解决方案2中忽略了this缺少情况编译错误的情况。



现在,我想你也应该明白这个究竟是什么。它看起来并不是那么微不足道。请看我过去的答案:

C#windows基于这个关键词及其在应用程序中的用途 [ ^ ]。



这与实例方法,所以请参阅:

什么使得静态方法可访问? [ ^ ],

Catch 22 - 当使用它们的函数变为静态时,指向接口对象的指针会死亡。 [ ^ ]。



(请不要对某些C#和C ++代码示例感到困惑。他们解释了一些原则,这些原则同样适用于Java和其他OPP语言。)



祝你好运,

-SA
First of all, so far, the only correct solution is the Solution 1; you should accept Solution 1 formally, and probably should reject Solution 2: readability is important, but resolution of the names is critically important and the case where the lack of "this" would case compilation error is overlooked in Solution 2.

Now, I think you also should understand what "this" actually is. It's is not so trivial as it may seem. Please see my past answer:
C# windows base this key word related and its uses in the application[^].

This is related to instance methods, so please also see:
What makes static methods accessible?[^],
Catch 22 - Pointers to interface objects die when function using them is made static.[^].

(Please don't get confused with some C# and C++ code samples. They explain some first principles which are equally applied to Java and other OPP languages.)

Good luck,
—SA


如你所说,不需要这个并且使用不带的成员名称给出相同的结果。所以从技术角度来看,没有必要使用这个



但它可以用来制作它在代码中清楚表明您引用的是类实例的成员变量而不是(静态)类变量。如果您在整个项目中遵循此约定,这将提高代码的可读性。
As you stated, this is not needed and using the member name without this give the same result. So from the technical standpoint it is not necessary to use this.

However it can be used to make it clear in the code that you are referring to a member variable of an instance of the class and not a (static) class variable. This improves the readability of your code if you follow this convention across your project.


这篇关于为什么在不使用它的情况下可以使用此关键字?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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