Home>

java this keyword

The this keyword is used to indicate the current object itself,Or an instance of the current class,This can call all the methods and properties of this object.E.g:

public class demo {
  public int x=10;
  public int y=15;
  public void sum () {
    //fetch member variables through this
    int z=this.x + this.y;
    system.out.println ("x + y =" + z);
  }
  public static void main (string [] args) {
    demo obj=new demo ();
    obj.sum ();
  }
}

operation result:

x + y=25

In the above program,obj is an instance of the demo class,this is equivalent to obj, and executing int z=this.x + this.y;is equivalent to executing int z=obj.x + obj.y ;.

Note:this only makes sense after the class is instantiated.

Use this to distinguish variables of the same name

When a member variable has the same name as a variable inside a method,Want to call a member variable inside a method,How to do it?Only use this at this time, for example:

public class demo {
  public string name;
  public int age;
  public demo (string name, int age) {
    this.name=name;
    this.age=age;
  }
  public void say () {
    system.out.println ("The name of the site is" + name + ", it has been established" + age + "year");
  }
  public static void main (string [] args) {
    demo obj=new demo ("Wei Xue Yuan", 3);
    obj.say ();
  }
}

operation result:

The name of the website is Wei Xue Yuan.
Has been established for 3 years

The scope of the parameter is the entire method body,Is a local variable.In demo (), the formal parameters and member variables have the same name,If you do not use this, you will access the local variables name and age, not the member variables.In say () we don't use this because the scope of the member variable is the entire instanceYou can also add this:

public void say () {
  system.out.println ("The name of the site is" + this.name + ", it has been established" + this.age + "year");
}

java associates all member variables and member methods with this by default,So using this is redundant in some cases.

Initialize the object as a method name

That is equivalent to calling other constructors of this class,It must be used as the first sentence of the constructor.Examples are:

public class demo {
  public string name;
  public int age;
  public demo () {
    this ("Wei Xue Yuan", 3);
  }
  public demo (string name, int age) {
    this.name=name;
    this.age=age;
  }
  public void say () {
    system.out.println ("The name of the site is" + name + ", it has been established" + age + "year");
  }
  public static void main (string [] args) {
    demo obj=new demo ();
    obj.say ();
  }
}

operation result:

The name of the website is Wei Xue Yuan.Has been established for 3 years

It is worth noting:

Call another constructor in the constructor,The call action must be placed at the very beginning.

A constructor cannot be called inside any method other than a constructor.

Only one constructor can be called within a constructor.

The above code involves method overloading,That is, java allows multiple methods with the same name,As long as the parameters are different.This will be explained in subsequent chapters.

Passed as a parameter

Need to call a method in some completely separate class,When passing a reference to the current object as a parameter.E.g:

public class demo {
  public static void main (string [] args) {
    b b=new b (new a ());
  }
}
class a {
  public a () {
    new b (this) .print ();//anonymous object
  }
  public void print () {
    system.out.println ("hello from a!");
  }
}
class b {
  a a;
  public b (a a) {
    this.a=a;
  }
  public void print () {
    a.print ();
    system.out.println ("hello from b!");
  }
}

operation result:

hello from a!
hello from b!

Anonymous objects are objects that have no name.If the object is used only once,As an anonymous object,In the code new b (this) .print ();is equivalent to (new b (this)) .print () ;, first create an object without a name through new b (this),Then call its methods.

java method overloading

In java, multiple methods in the same class can have the same name,As long as their parameter lists are different,This is called method overloading.

The parameter list is also called parameter signature,Including the type of the parameter, the number of parameters, and the order of the parameters,As long as there is a difference, it is called a different parameter list.

Overloading is a fundamental feature of object orientation.

See a detailed example below.

public class demo {
  //a normal method,Without parameters
  void test () {
    system.out.println ("no parameters");
  }
  //overload the above method,With an integer parameter
  void test (int a) {
    system.out.println ("a:" + a);
  }
  //overload the above method,And took two parameters
  void test (int a, int b) {
    system.out.println ("a and b:" + a + "" + b);
  }
  //overload the above method,With a double parameter
  double test (double a) {
    system.out.println ("double a:" + a);
    return a * a;
  }
  public static void main (string args []) {
    demo obj=new demo ();
    obj.test ();
    obj.test (2);
    obj.test (2,3);
    obj.test (2.0);
  }
}

operation result:

no parameters
a:2
a and b:2 3
double a:2.0

Through the above example,The reader can see thatOverloading is in a class,Have the same function name,But the parameters are different functions.Overloaded results,You can minimize the number of codes and methods in a program segment.

Explanation:

Different parameter lists include:different numbers, different types, and different orders. It is not possible to have only different parameter variable names. Like member methods,Constructors can also be overloaded. Methods declared final cannot be overloaded. Methods declared static cannot be overloaded,But it can be stated again.

Method overloading rules:

The method names must be the same. The parameter lists must be different (different numbers, or types, parameter order, etc.). The return type of a method can be the same or different. Merely different return types are not enough to be overloaded.

Method overloading implementation:

When the method names are the same,The compiler will match one by one according to the number of parameters and parameter types of the calling method.To choose the corresponding method,If the match fails,The compiler reports an error,This is called overload resolution.

  • Previous C # implementation of multiple SQL statements with GO instances
  • Next When debugging ASPNET2005/2008, there are three sets of solutions to incorrect ports