当前位置: 首页 > news >正文

网站建设设计时代创信好海南城乡和住房建设厅网站

网站建设设计时代创信好,海南城乡和住房建设厅网站,网红营销活动,小吃店网站建设1 . 当成员变量和局部变量重名时#xff0c;在方法中使用this时#xff0c;表示的是该方法所在类中的成员变量。(this是当前对象自己)如#xff1a;public class Hello {String s Hello;public Hello(String s) {System.out.println(s s);Syste…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!);System.out.println(s x.s);//验证成员变量值的改变}}结果为s HelloWorld!1 - this.s Hello2 - this.s HelloWorld!sHelloWorld!在这个例子中构造函数Hello中参数s与类Hello的成员变量s同名这时如果直接对s进行操作则是对参数s进行操作。若要对类Hello的成员变量s进行操作就应该用this进行引用。运行结果的第一行就是直接对构造函数中传递过来的参数s进行打印结果 第二行是对成员变量s的打印第三行是先对成员变量s赋传过来的参数s值后再打印所以结果是HelloWorld!而第四行是主函数中直接打印类中的成员变量的值也可以验证成员变量值的改变。2把自己当作参数传递时也可以用this.(this作当前参数进行传递)class A {public A() {new B(this).print();// 调用B的方法}public void print() {System.out.println(HelloAA from A!);}}class B {A a;public B(A a) {this.a a;}public void print() {a.print();//调用A的方法System.out.println(HelloAB from B!);}}public class HelloA {public static void main(String[] args) {A aaa new A();aaa.print();B bbb new B(aaa);bbb.print();}}结果为HelloAA from A!HelloAB from B!HelloAA from A!HelloAA from A!HelloAB from B!在这个例子中对象A的构造函数中用new B(this)把对象A自己作为参数传递给了对象B的构造函数。3有时候我们会用到一些内部类和匿名类如事件处理。当在匿名类中用this时这个this则指的是匿名类或内部类本身。这时如果我们要使用外部类的方法和变量的话则应该加上外部类的类名。如public class HelloB {int i 1;public HelloB() {Thread thread new Thread() {public void run() {for (int j0;j20;j) {HelloB.this.run();//调用外部类的方法try {sleep(1000);} catch (InterruptedException ie) {}}}}; // 注意这里有分号thread.start();}public void run() {System.out.println(i i);i;}public static void main(String[] args) throws Exception {new HelloB();}}在上面这个例子中, thread 是一个匿名类对象在它的定义中它的 run 函数里用到了外部类的 run 函数。这时由于函数同名直接调用就不行了。这时有两种办法一种就是把外部的 run 函数换一个名字但这种办法对于一个开发到中途的应用来说是不可取的。那么就可以用这个例子中的办法用外部类的类名加上 this 引用来说明要调用的是外部类的方法 run。4 在构造函数中通过this可以调用同一类中别的构造函数。如public class ThisTest {ThisTest(String str) {System.out.println(str);}ThisTest() {this(this测试成功);}public static void main(String[] args) {ThisTest thistest new ThisTest();}}为了更确切的说明this用法另外一个例子为public class ThisTest {private int age;private String str;ThisTest(String str) {this.strstr;System.out.println(str);}ThisTest(String str,int age) {this(str);this.ageage;System.out.println(age);}public static void main(String[] args) {ThisTest thistest new ThisTest(this测试成功,25);}}结果为this测试成功25值得注意的是1在构造调用另一个构造函数调用动作必须置于最起始的位置。2不能在构造函数以外的任何函数内调用构造函数。3在一个构造函数内只能调用一个构造函数。5this同时传递多个参数。public class TestClass {int x;int y;static void showtest(TestClass tc) {//实例化对象System.out.println(tc.x tc.y);}void seeit() {showtest(this);}public static void main(String[] args) {TestClass p new TestClass();p.x 9;p.y 10;p.seeit();}}结果为9 10代码中的showtest(this),这里的this就是把当前实例化的p传给了showtest()方法从而就运行了。
http://www.zqtcl.cn/news/489570/

相关文章:

  • 扁平化网站设计方案大学生做的美食网站
  • wordpress前台打开速度20秒湖南正规竞价优化公司
  • 深度网营销型网站建设wordpress keywords
  • 企业官网快速建站框架物流网站源代码
  • 网站图片设置隐私保护怎么下载搭建购物网站
  • 网站运营和推广可以做mv 的视频网站
  • 成都网站建设冠辰成都关键词优化技术
  • 用什么框架做网站快哪个网站可以自己做名片
  • 免费网站建设ppt模板下载网站设计与程序专业
  • o2o网站设计方案高端定制网站开发设计建站流程
  • 杭州建设公司网站石家庄做网站比较好的公司
  • 英文网站支付怎么做产品做推广都有那些网站
  • 自己做的网站怎么加入微信支付综合性门户网站列举
  • 哪个网站 可以做快递单录入网站怎么做抽奖
  • 网站设计培训班网站域名费用怎么做分录
  • 济南做网站哪里好惠州附近公司做网站建设多少钱
  • 使用oss做静态网站网站广告牌制作教程
  • 外贸看的英文网站公众号模板编辑器
  • 做网站的数据库的步骤阅读网站模板下载
  • 建设网站要钱吗个人养老金制度是什么意思
  • 做h5的网站页面设计软文素材网站
  • 黄冈网站推广软件费用是多少手机网站弹出层插件有哪些
  • wordpress文章链接怎么改怎么优化关键词排名优化
  • 专业做包包的网站好产品网站做营销推广
  • 网站刚建好怎么做能让百度收录湖北黄石网站建设
  • 网站建设拾金手指下拉二一wordpress 插件破解
  • 天津做网站外包公司有哪些美橙互联网站
  • 石家庄网站建设蓝点办公室装修工程
  • 申请网站空间就是申请域名建设机械网站咨询
  • 做美食网站有哪些网站怎么做自响应