Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/268.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/356.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# “有什么用?”;这";在Java和/或C语言中?_C#_Java - Fatal编程技术网

C# “有什么用?”;这";在Java和/或C语言中?

C# “有什么用?”;这";在Java和/或C语言中?,c#,java,C#,Java,假设我有一个简单的示例控制台程序,如下所示。我的问题是关于这个。此的唯一用途是否只是为了使用输入变量名来分配给实例变量?我想知道除了在这个程序的上下文中使用之外,这个还有什么用途 public class SimpleClass { int numberOfStudents; public SimpleClass(){ numberOfStudents = 0; } public void setStudent(int numberOfStu

假设我有一个简单的示例控制台程序,如下所示。我的问题是关于这个。
的唯一用途是否只是为了使用输入变量名来分配给实例变量?我想知道除了在这个程序的上下文中使用之外,这个还有什么用途

public class SimpleClass {

    int numberOfStudents;

    public SimpleClass(){
        numberOfStudents = 0;
    }

    public void setStudent(int numberOfStudents){
        this.numberOfStudents = numberOfStudents;
    }

    public void printStudents(){
        System.out.println(numberOfStudents);
    }

    public static void main(String[] args) {

        SimpleClass newRandom = new SimpleClass();
        newRandom.setStudent(5);
        newRandom.printStudents();

    }
}
以前,当我需要为与输入值具有相似性的实例变量名赋值时,我必须创造性地使用命名方案(或缺少)。例如,
setStudent()
方法如下所示:

public void setStudent(int numberOfStudentsI){
    numberOfStudents = numberOfStudentsI;
}

从上面的例子来看,似乎使用
这个
代替了必须这样做。这是它的预期用途,还是我遗漏了什么?

如果您想引用对象本身,也可以使用此

someMethod(this);
除了这种语法(双关语)没有其他选择


它还用于调用协构造函数和C#扩展方法。

如果您想引用对象本身,也可以使用此

someMethod(this);
除了这种语法(双关语)没有其他选择

它还用于调用协构造函数,并用于C#扩展方法。

在C#中,您可以使用它来引用类对象的当前实例,如msdn中的类

    class Employee
    {
        private string name;
        private string alias;
        private decimal salary = 3000.00m;

        // Constructor: 
        public Employee(string name, string alias)
        {
            // Use this to qualify the fields, name and alias: 
            this.name = name;
            this.alias = alias;
        }
        // Printing method: 
        public void printEmployee()
        {
            Console.WriteLine("Name: {0}\nAlias: {1}", name, alias);
            // Passing the object to the CalcTax method by using this:
            Console.WriteLine("Taxes: {0:C}", Tax.CalcTax(this));
        }

        public decimal Salary
        {
            get { return salary; }
        }
    }

    class Tax
    {
        public static decimal CalcTax(Employee E)
        {
            return 0.08m * E.Salary;
        }
    }

    class MainClass
    {
        static void Main()
        {
            // Create objects:
            Employee E1 = new Employee("Mingda Pan", "mpan");

            // Display results:
            E1.printEmployee();
        }
    }
    /*
    Output:
        Name: Mingda Pan
        Alias: mpan
        Taxes: $240.00
     */
在c#中,您使用它来引用类对象imEngine的当前实例,您从msdn获得了类似这样的类

    class Employee
    {
        private string name;
        private string alias;
        private decimal salary = 3000.00m;

        // Constructor: 
        public Employee(string name, string alias)
        {
            // Use this to qualify the fields, name and alias: 
            this.name = name;
            this.alias = alias;
        }
        // Printing method: 
        public void printEmployee()
        {
            Console.WriteLine("Name: {0}\nAlias: {1}", name, alias);
            // Passing the object to the CalcTax method by using this:
            Console.WriteLine("Taxes: {0:C}", Tax.CalcTax(this));
        }

        public decimal Salary
        {
            get { return salary; }
        }
    }

    class Tax
    {
        public static decimal CalcTax(Employee E)
        {
            return 0.08m * E.Salary;
        }
    }

    class MainClass
    {
        static void Main()
        {
            // Create objects:
            Employee E1 = new Employee("Mingda Pan", "mpan");

            // Display results:
            E1.printEmployee();
        }
    }
    /*
    Output:
        Name: Mingda Pan
        Alias: mpan
        Taxes: $240.00
     */

在Java/C中,变量的作用域不同。以下面这个例子为例。虽然
this.numberOfStudents
numberOfStudents
具有相同的名称,但它们并不完全相同

public void setStudent(int numberOfStudents){
    this.numberOfStudents = numberOfStudents;
}
this.numberOfStudents
是一个名为
numberOfStudents
的变量,它位于该类的实例中<代码>此始终指向当前实例


public void setStudent(int numberOfStudents)
numberOfStudents
是一个新变量,仅在该方法中可用

在Java/C#中,变量的作用域不同。以下面这个例子为例。虽然
this.numberOfStudents
numberOfStudents
具有相同的名称,但它们并不完全相同

public void setStudent(int numberOfStudents){
    this.numberOfStudents = numberOfStudents;
}
this.numberOfStudents
是一个名为
numberOfStudents
的变量,它位于该类的实例中<代码>此始终指向当前实例

public void setStudent(int numberOfStudents)
numberOfStudents
是一个新变量,仅在该方法中可用

关键字“this”是指当前类(SimpleClass)的动态对象

public class SimpleClass(){

    private int a;
    private int b;
    private int c;

    public SimpleClass(int a, int b){
       this.a = a;
       this.b = b;
    }

    public SimpleClass(int a, int b, int c){
       // this constrcutor
       this(a,b);
       this.c = c;
    }
}
关键字“this”是指当前类(SimpleClass)的动态对象

public class SimpleClass(){

    private int a;
    private int b;
    private int c;

    public SimpleClass(int a, int b){
       this.a = a;
       this.b = b;
    }

    public SimpleClass(int a, int b, int c){
       // this constrcutor
       this(a,b);
       this.c = c;
    }
}

“this”只是指对象本身

当编译器查找'numberOfStudents'的值时,它将使用此名称匹配'closest'变量。在本例中,函数的参数为。 但是,如果要将其分配给类变量,则需要使用“this.”符号

在方法中

public void setStudent(int numberOfStudents){
    this.numberOfStudents = numberOfStudents;
}
比如说

'this.numberOfStudents'引用名为'numberOfStudents'的类变量 “numberOfStudents”引用了该方法的参数


因此,此方法简单地将参数的值分配给类变量(具有相同的名称)。

“this”仅指对象本身

当编译器查找'numberOfStudents'的值时,它将使用此名称匹配'closest'变量。在本例中,函数的参数为。 但是,如果要将其分配给类变量,则需要使用“this.”符号

在方法中

public void setStudent(int numberOfStudents){
    this.numberOfStudents = numberOfStudents;
}
比如说

'this.numberOfStudents'引用名为'numberOfStudents'的类变量 “numberOfStudents”引用了该方法的参数


因此,这个方法简单地将参数的值分配给类变量(使用相同的名称)。

事情与您目前对它们的看法完全相反:
这个
在Java/C中是如此重要和经常使用的一个项,因此有许多特殊的语法规则可以假定它的位置。这些规则导致你实际上很少看到写出来的
这个

但是,除了您的示例之外,还有许多其他情况下无法避免显式
this
(Java):

  • 从内部类引用封闭实例
  • 显式参数化对泛型方法的调用
  • 将此
作为参数传递给其他方法
  • 从方法返回
    (生成器模式中经常出现)
  • 将该
  • 赋值给变量
    。。。还有更多。

    事情与您目前对它们的看法完全相反:
    这是Java/C中一个非常重要且经常使用的项目,因此有许多特殊的语法规则可以假设它。这些规则导致你实际上很少看到写出来的
    这个

    但是,除了您的示例之外,还有许多其他情况下无法避免显式
    this
    (Java):

    • 从内部类引用封闭实例
    • 显式参数化对泛型方法的调用
    • 将此
    作为参数传递给其他方法
  • 从方法返回
    (生成器模式中经常出现)
  • 将该
  • 赋值给变量
    。。。还有更多。

    这是调用者。
    但很少在Java中看到它的用法
    你在读什么Java代码?@BobbyDigital:不,不是。即使你不“需要它”,它也表明了你的意图,并在你开始扩展对象时使代码更容易阅读。这不是调用者。调用方是调用当前函数的函数。