① java中new和this的用法和作用
new是生成一个对象实例,类名称 对象名称=new 类名称(这里面可以传参数,也可以没有,看你构造函数);
this 就是指当前的对象,
② Java中 A a=new B(this) 是什么意思
这个语句拆成3部分来看
首先new b()意味着实例化b类,
而this代表着,将用到这个语句的这个类,作为参数传递到b的构造函数中去。
最后向上转型变成其父类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方法中使用!
④ 在JAVA中new 和this的作用,用法,在什么情况下使用
new只出现在构造方法之前用来生成对象
即:Student
a
=
new
Student();
this
1、当全局变量跟局部变量重名时,表示使用全局变量(此时this指代本类对象)
例有一类class
A{
String
name;
void
setName(String
name){
this.name
=
name;
}
}
2、构造方法相互调用,此时this指代本类类名
注意this只能放在构造方法第一句
如class
B{
String
name;
B(){
this("name");//会自动调用带String参数的构造方法
}
B(String
name){
this.name
=
name;
}
}
⑤ java this什么意思
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作为参数传递
当你要把自己作为参数传递给别的对象时,也可以用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的构造函数。
⑥ java 中this什么作用 小白提问
this指代的是当前类本身,可以在成员变量和参数同名的时候用来区分参数和成员变量。
⑦ 在java中 Date d = new Date(this); this指的是啥
new Date(this)这个没想起来是什么,建议把整个类贴出来。只要记住this对应调用这个方法的实例对象就行了。所以在内部类中对应内部类的实例,如果要使用外部类的this,需要加上类名,如用Person.this
⑧ 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的构造函数。
⑨ JAVA中return,this,new,的用法
return:返回
其实它的作用不是结束循环的,而是结束方法的。
this:是当前类的对象引用。简单的记,它就代表当前类的一个对象。
注意:谁调用这个方法,在该方法内部的this就代表谁。
this的场景:
解决局部变量隐藏成员变量
new:
在一个java文件中写两个类:一个基本的类,一个测试类。
注意:文件名称和测试类名称一致。
如何使用呢?
创建对象使用。
如何创建对象呢?
格式:类名对象名=new类名();
如何使用成员变量呢?
对象名.变量名
如何使用成员方法呢?
对象名.方法名(...)