导航:首页 > 编程语言 > thisjava

thisjava

发布时间:2022-02-26 04:08:14

A. java中“this”的用法是什么

一、指自己所在的对象。
比如在一个方法中,调用其他对象的变量或方法时,可以使用那个对象的对象名,比如aa.abc();
而调用自己所在对象的方法或变量时,不知道别人给起了什么名,所以直接用this.abc()就可以了。
二、看一个小例子中“this”的用法!

/**

* @author feng-neusoft

*

* 本示例为了说明this的三种用法!

*/

package test;

public class ThisTest {

private int i=0;

//第一个构造器:有一个int型形参

ThisTest(int i){

this.i=i+1;//此时this表示引用成员变量i,而非函数参数i

System.out.println("Int constructor i——this.i: "+i+"——"+this.i);

System.out.println("i-1:"+(i-1)+"this.i+1:"+(this.i+1));

//从两个输出结果充分证明了i和this.i是不一样的!

}

// 第二个构造器:有一个String型形参

ThisTest(String s){

System.out.println("String constructor: "+s);

}

// 第三个构造器:有一个int型形参和一个String型形参

ThisTest(int i,String s){

this(s);//this调用第二个构造器

//this(i);

/*此处不能用,因为其他任何方法都不能调用构造器,只有构造方法能调用他。

但是必须注意:就算是构造方法调用构造器,也必须为于其第一行,构造方法也只能调

用一个且仅一次构造器!*/

this.i=i++;//this以引用该类的成员变量

System.out.println("Int constructor: "+i+"/n"+"String constructor: "+s);

}

public ThisTest increment(){

this.i++;

return this;//返回的是当前的对象,该对象属于(ThisTest)

}

public static void main(String[] args){

ThisTest tt0=new ThisTest(10);

ThisTest tt1=new ThisTest("ok");

ThisTest tt2=new ThisTest(20,"ok again!");

System.out.println(tt0.increment().increment().increment().i);

//tt0.increment()返回一个在tt0基础上i++的ThisTest对象,

//接着又返回在上面返回的对象基础上i++的ThisTest对象!

}

}

运行结果:

Int constructor i——this.i: 10——11

String constructor: ok

String constructor: ok again!

Int constructor: 21

String constructor: ok again!

14

细节问题注释已经写的比较清楚了,总结一下,其实this主要要三种用法:

1、表示对当前对象的引用!

2、表示用类的成员变量,而非函数参数,注意在函数参数和成员变量同名是进行区分!其实这是第一种用法的特例,比较常用,所以那出来强调一下。

3、用于在构造方法中引用满足指定参数类型的构造器(其实也就是构造方法)。但是这里必须非常注意:只能引用一个构造方法且必须位于开始!

还有就是注意:this不能用在static方法中!所以甚至有人给static方法的定义就是:没有this的方法!虽然夸张,但是却充分说明this不能在static方法中使用!

B. this()在java中什么意思

this表示类实例本身。

this的用法:

1、表示对当前对象的引用!

publicclassA{

publicAgetA(){

returnthis;//表示获取当前实例本身

}

}

2、表示类的成员变量,而非函数参数,注意在函数参数和成员变量同名是进行区分!

publicclassA{

privateinta=0;//位置1

publicAgetA(inta){

this.a=a;//前面this.a表示位置1的a,赋值=号右侧的表示参数a

}

}

3、用于在构造方法中引用满足指定参数类型的构造器。

publicclassA{

publicA(inta){

}

publicA(){

this(1);//这里调用自身的构造函数publicA(inta){

}

}

C. java里的“this”到底是什么意思

this为一系统资源,只允许用户读而不允许写,它存放当前对象的地址(引用)。

this变量有以下作用:

1. 构造方法重用:

public class Rectangle{
public Rectangle(Location at, Shape size) {…}
public Rectangle(Shape size,Location at){
this(at, size); }
public Rectangle(Location at) {
this(at, new Shape(100,100));
}
public Rectangle(Shape size) {
this(size, new Location(1,1));
}
public Rectangle() {
this(new Location(1,1), new Shape(100,100));
}
}

2、消除歧义:

Location{
private int x;
private int y;
public Location(int x,int y) {
this.x=x;
this.y=y;
}
……
}

3、返回对象-链式方法调用:

public class Count {
private int i = 0;
Count increment() {
i++;
return this; //返回对象的地址,所以我们可以链式访问它
}
void print() {
System.out.println("i = " + i);
}
}
public class CountTest{
public static void main(String[] args) {
Count x = new Count();
x.increment().increment().print();
}
}

4、作为参数传递"this”变量-进行回调:

假设有一个容器类和一个部件类,在容器类的某个方法中要创建部件类的实例对象,而部件类的构造方法要接受一个代表其所在容器的参数。例如:
class Container
{
Component comp;
public void addComponent()
{
comp = new Component(this); //代表你所创建的对象,因为它要用到.
}
}
class Component
{
Container myContainer;
public Component(Container c)
{
myContainer = c;
}
}

其中我们开发中最常用到的地方是第二点,消除歧义。

比方说有类
public class A
里面有几个变量
private String aa,
private String bb;

this 在这里就代表A ,其实它是对对象A的引用。
我们在用到aa或者bb的时候,this.aa 和 直接用aa 是没有区别的。
但是假如你在某个方法里也有个变量aa,比如:
public void dosomething(String aa){
this.aa = aa;
//这个时候就this.aa 代表对象A中的变量,而不加this的话,代表方法中的变量。
}

D. java中this();什么意思

这句话是调用无参构造函数,相当于没有参数构造方法

E. java中this的用法

1、this指向当前类的对象,也就是TwoListen类的对象。由于MouseMotionListener,MouseListener 都是接口,因此
f.addMouseMotionListener(this);//(************************)
f.addMouseListener(this); //(************************)
中分别需要一个实现了MouseMotionListener和MouseListener接口的类的实例。

在本例子中 TwoListen类都实现了这两个接口,因此可以用本类的实例来做参数。或者新建一个实现了这两个接口的类,再用这个类的实例做参数。

2、在该例子中,this指向生成的two对象。为什么用two替换后就不能运行了呢?
因为two是在main方法中定义的,是局部变量;而在go方法中并不能访问其他方法中定义的局部变量。
如果把two定义为全局变量,如:
static TwoListen two;
再在main方法中定义:
two=new TwoListen();

这时就可以用two代替this了

F. java里面this指的是什么

this为一系统资源,只允许用户读而不允许写,它存放当前对象的地址(引用)。

this变量有以下作用:

1. 构造方法重用:

public class Rectangle{
public Rectangle(Location at, Shape size) {…}
public Rectangle(Shape size,Location at){
this(at, size); }
public Rectangle(Location at) {
this(at, new Shape(100,100));
}
public Rectangle(Shape size) {
this(size, new Location(1,1));
}
public Rectangle() {
this(new Location(1,1), new Shape(100,100));
}
}

2、消除歧义:

Location{
private int x;
private int y;
public Location(int x,int y) {
this.x=x;
this.y=y;
}
……
}

3、返回对象-链式方法调用:

public class Count {
private int i = 0;
Count increment() {
i++;
return this; //返回对象的地址,所以我们可以链式访问它
}
void print() {
System.out.println("i = " + i);
}
}
public class CountTest{
public static void main(String[] args) {
Count x = new Count();
x.increment().increment().print();
}
}

4、作为参数传递"this”变量-进行回调:

假设有一个容器类和一个部件类,在容器类的某个方法中要创建部件类的实例对象,而部件类的构造方法要接受一个代表其所在容器的参数。例如:
class Container
{
Component comp;
public void addComponent()
{
comp = new Component(this); //代表你所创建的对象,因为它要用到.
}
}
class Component
{
Container myContainer;
public Component(Container c)
{
myContainer = c;
}
}

G. java中this的用法

1. this指当前对象。
当在一个类中要明确指出使用对象变量或函数时加上this引用。如下面例子中:

public class Hello {

String s = "Hello";

public Hello(String s){
System.out.println("s = " + s);
System.out.println("1 -> this.s = " + this.s);
this.s = s;
System.out.println("2 -> this.s = " + this.s);
}

public static void main(String[] args) {
Hello x=new Hello("HelloWorld!");
}
}

运行结果:

s = HelloWorld!
1 -> this.s = Hello
2 -> this.s = HelloWorld!

在这个例子中,构造函数Hello中,参数s与类Hello的变量s同名,这时直接对s进行操作则是对参数s进行操作。对类Hello的成员变量s进行操作就应该用this进行引用。运行结果的第一行就是直接对构造函数中传递过来的参数s进行打印结果;
第二行是对成员变量s的打印;第三行是先对成员变量s赋传过来的参数s值后再打印,所以结果是HelloWorld!

2. this作为参数传递
当你要把自己作为参数传递给别的对象时如:

public class A {
public A() {
new B(this).print();
}

public void print() {
System.out.println("Hello from A!");
}
}

public class B {
A a;
public B(A a) {
this.a = a;
}

public void print() {
a.print();
System.out.println("Hello from B!");
}
}

运行结果:
Hello from A!
Hello from B!

在这个例子中,对象A的构造函数中,new
B(this)把对象A作为参数传递给了对象B的构造函数。

H. JAVA中this()和this.的区别

public ClassName(){
System.out.println("sssssssssssss");
}
public ClassName(String a){
this();
System.out.println(a);
}
public static void main(String[] args) {
new ClassName("asdf");
}

运行结果就是:
sssssssssssss
asdf

this() 就是调用自己的无参构造方法,和super()一个道理。
this就是指本对象自身。

I. java this 的用法

this关键字用来指代当前对象。
你说的这个问题我也曾考虑过,但是最终还是要用一样的变量名,其原因是:

你的方法是用来给其他程序在其他位置调用的。如果你在这里设置了不同的变量名,那其他地方在调用的时候也会看到你的另外一个参数名,因为名字不一样,就不能一下知道你的参数到底是需要什么东西,对于除开你自己外的所有开发人员都不具有可读性。而且对后期的维护会造成很大的麻烦,为了避免这些麻烦,这里都是要用相同的变量名的,只需要加一个this关键字就可以。相对来说是很方便的。

J. JAVA中的this是什么意思

java里面this是指本身的意思,比如说在一个类里面this就代表自己本身这个类

阅读全文

与thisjava相关的资料

热点内容
优信二手车解压后过户 浏览:63
Windows常用c编译器 浏览:780
关于改善国家网络安全的行政命令 浏览:835
安卓如何下载网易荒野pc服 浏览:656
javainetaddress 浏览:106
苹果4s固件下载完了怎么解压 浏览:1006
命令zpa 浏览:288
python编译器小程序 浏览:946
在app上看视频怎么光线调暗 浏览:542
可以中文解压的解压软件 浏览:595
安卓卸载组件应用怎么安装 浏览:915
使用面向对象编程的方式 浏览:342
程序员项目经理的年终总结范文 浏览:932
内衣的加密设计用来干嘛的 浏览:435
淮安数据加密 浏览:295
魔高一丈指标源码 浏览:984
松下php研究所 浏览:171
c回调java 浏览:403
梦幻端游长安地图互通源码 浏览:747
电脑本地文件如何上传服务器 浏览:315