D six, java and the static keyword in this

introduction:

     D five, java method overloading we mentioned in the last two objects of the same type is how to call a method it? Then we take a look at this example it

A, this

class cat{
	void print(int i) {
		System.out.println("There is "+i+" cat.");
	}
}
public class ThisObject {
	public static void main(String []args) {
	cat c1 = new cat();
	cat c2 = new cat();
	c1.print(1);
	c2.print(2);
	}
}

     You may find that the two objects are invoked by parameters (that is, the reference to the operation of the object passed to the method ). If you want to get inside the method of reference to the current object, then we must know that this keyword. this keyword can only use internal methods, is a reference to "that object to invoke a method" of . If you call the same method inside a class inside the method, you do not need to use this keyword, this reference method will automatically be applied to the same class . Therefore, only need to clearly indicate when referring to the current object, only need to use this keyword. E.g:

public class ThisObject {
	int i = 0;
	ThisObject cat() {
		i++;
		return this;
	}
	void print() {
		System.out.println("i= "+i);
	}
	public static void main(String []args) {
		ThisObject t = new ThisObject();
		t.cat().cat().cat().print();
	}
}

Output: i = 3

 

     Here we need a clear return to the current object reference (this can be performed on the same object multiple operations inside a single statement ), so we used the return behind the this keyword. this keyword may also be used to pass the current object to the other methods . E.g:

class cat{
	public void eat(Apple apple) {
		Apple peeled = apple.getPeeled();
		System.out.println("jack");
	}
}
class Peeler{
	static Apple peel(Apple apple) {
		return apple;
	}
}
class Apple{
	Apple getPeeled() {return Peeler.peel(this);
	}
}
public class ThisO {
	public static void main(String []args) {
		new cat().eat(new Apple());
	}
}

Output: Jack

 

     We created inside the main method object of cat and calls inside the eat method, parameter is Apple's object, which in turn calls the Apple inside getPeeled () method, you need to return the current object reference, and then return to apple objects, and finally output jack. This example is simply Apple needs to call an external Peeler.peel method requires passing itself to external means, so we used the this keyword . Of course, we can also call this keyword in the constructor constructor (a constructor can only be called) . E.g:

public class ThisO{
	int Count = 0;
	String s = "cat";
	ThisO(int c){//int参数的构造器
		Count = c;
		System.out.println("Constructor int args Count = "+c);
	}
	ThisO(String ss){//String 参数的构造器
		System.out.println("s = "+ ss);
		s = ss;
	}
	ThisO(String s,int c){//两个参数的构造器
		this(c);
		//this(s);试图调用另一个构造器,错误!
		this.s = s;//明确对数据成语进行赋值,与参数s区别
		System.out.println("String & int args");
	}
	ThisO(){//无参构造器
		this("pig",47);
		System.out.println("default constructor (no args)");
	}
	void PrintCount() {
		//this(11);  我们不能在构造器以外的方法调用构造器,哈哈
		System.out.println("Count = "+Count+" String= "+s);
	}
	public static void main(String []args) {
		ThisO t = new ThisO();
		t.PrintCount();
	}
}

Output:

 

     In the above example, we can see that for a call to the constructor, only one call can be, but not two call , and we can see, in addition to the constructor, the compiler prohibit other constructor method call . Examples of members other than the name and data parameters consistent and syntax used this.s = s, and with data representing this.s members , in fact, we often used, for example, void cat (int i) {this.i = i;} on It can be initialized so.

 

 

二、static

     And this, of course, is relatively static, the method is not this static method, the internal static methods can not call non-static methods, in turn, does fit . We mentioned previously that a use is if you do not want to create an object invokes the method , then, is to use the static. But we can not appear a lot of static, although there is no global variables java, but static can play this role. (In C language which we were told not to use a large number of global variables, so the java inside, too).

     Here to do a summary of the object initialization and clean it for garbage collection mechanism we can go see Resources. Initialization, there are many things worthy of our attention, such as initialization of the array (java to initialize the array to avoid cross-border), the static type of initialization. There we often encounter in c ++, the reason we want to know the order of initialization (this type of problem often mentioned polymorphism inside, we also worth noting).

 

to sum up:

     You will find that I mentioned in the static keyword, where the local and global variables, then they certainly have a certain range, then we take a look at the competence of those visits. If wrong, please point out, thank you.

 

 

Published 79 original articles · won praise 182 · views 10000 +

Guess you like

Origin blog.csdn.net/qq_43919400/article/details/104267357