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

手机浏览器网页加速器重庆seo霸屏

手机浏览器网页加速器,重庆seo霸屏,谷歌app下载,企业商城网站建设价格目录 1、this引用 1.1、为什么要有this引用 1.2、什么是this引用 1.3、 this引用的特性 2、 对象的构造及初始化 2.1、 如何初始化对象 2.2、构造方法 2.2.1、概念 2.2.2、特性 2.3、默认初始化 2.4、就地初始化 上篇:【JavaSE】基础笔记 - 类和对象&#…

 

目录

1、this引用

1.1、为什么要有this引用

1.2、什么是this引用

1.3、 this引用的特性

2、 对象的构造及初始化

2.1、 如何初始化对象

2.2、构造方法

2.2.1、概念 

 2.2.2、特性

 2.3、默认初始化

 2.4、就地初始化


 

上篇:【JavaSE】基础笔记 - 类和对象(上)-CSDN博客icon-default.png?t=N7T8https://blog.csdn.net/zzzzzhxxx/article/details/134232584?spm=1001.2014.3001.5501

接上篇继续对类和对象的知识展开讲述。

1、this引用

1.1、为什么要有this引用

先看一个日期类的例子: 

    public class Date {public int year;public int month;public int day;public void setDay(int y, int m, int d){year = y;month = m;day = d;}public void printDate(){System.out.println(year + "/" + month + "/" + day);}public static void main(String[] args) {// 构造三个日期类型的对象 d1 d2 d3Date d1 = new Date();Date d2 = new Date();Date d3 = new Date();// 对d1,d2,d3的日期设置d1.setDay(2020,9,15);d2.setDay(2020,9,16);d3.setDay(2020,9,17);// 打印日期中的内容d1.printDate();d2.printDate();d3.printDate();}}

以上代码定义了一个日期类,然后main方法中创建了三个对象,并通过Date类中的成员方法对对象进行设置和打印,代码整体逻辑非常简单,没有任何问题。

但是细思之下有以下两个疑问

1、形参名不小心与成员变量名相同:

    public void setDay(int year, int month, int day){year = year;month = month;day = day;}

那函数体中到底是谁给谁赋值?成员变量给成员变量?参数给参数?参数给成员变量?成员变量参数?估计自己都搞不清楚了。

再说了,每次都需要起不同的变量名非常不方便,有没有一种办法可以允许我们直接使用相同的变量名,省去繁琐的起名环节?

2、 三个对象都在调用setDate和printDate函数,但是这两个函数中没有任何有关对象的说明,setDate和printDate函数如何知道打印的是哪个对象的数据呢?

一切让 this 引用来揭开这层神秘的面纱。 

1.2、什么是this引用

this引用指向当前对象(成员方法运行时调用该成员方法的对象),在成员方法中所有成员变量的操作,都是通过该引用去访问。只不过所有的操作对用户是透明的,即用户不需要来传递,编译器自动完成。

    public class Date {public int year;public int month;public int day;public void setDay(int year, int month, int day){this.year = year;this.month = month;this.day = day;}public void printDate(){System.out.println(this.year + "/" + this.month + "/" + this.day);}}

 注意:this引用的是调用成员方法的对象。例如当调用a.setDay()是,此时的this就代指a,如下图。同理b也一样。

1.3、 this引用的特性

  1. this的类型:对应类类型引用,即哪个对象调用就是哪个对象的引用类型
  2. this只能在"成员方法"中使用
  3. 在"成员方法"中,this只能引用当前对象,不能再引用其他对象
  4. this是“成员方法”第一个隐藏的参数,编译器会自动传递,在成员方法执行时,编译器会负责将调用成员方法对象的引用传递给该成员方法,this负责来接收。

 在代码层面来简单演示一下隐藏参数this,注意:下图右侧中的Date类也是可以通过编译的

2、 对象的构造及初始化

2.1、 如何初始化对象

通过前面知识点的学习知道,在Java方法内部定义一个局部变量时,必须要初始化,否则会编译失败。

public static void main(String[] args) {int a;System.out.println(a);}
// Error:(26, 28) java: 可能尚未初始化变量a

要让上述代码通过编译,非常简单,只需在正式使用a之前,给a设置一个初始值即可。如果是对象:

public static void main(String[] args) {Date d = new Date();d.printDate();d.setDate(2021,6,9);d.printDate();}
// 代码可以正常通过编译

需要调用之前写的SetDate方法才可以将具体的日期设置到对象中。通过上述例子发现两个问题:

  1. 每次对象创建好后调用SetDate方法设置具体日期,比较麻烦,那对象该如何初始化?
  2. 局部变量必须要初始化才能使用,为什么字段声明之后没有给值依然可以使用?

2.2、构造方法

2.2.1、概念 

构造方法(也称为构造器)是一个特殊的成员方法,名字必须与类名相同,在创建对象时,由编译器自动调用,并且在整个对象的生命周期内只调用一次。

public class Date {public int year;public int month;public int day;// 构造方法:
// 名字与类名相同,没有返回值类型,设置为void也不行
// 一般情况下使用public修饰
// 在创建对象时由编译器自动调用,并且在对象的生命周期内只调用一次public Date(int year, int month, int day){this.year = year;this.month = month;this.day = day;System.out.println("Date(int,int,int)方法被调用了");}public void printDate(){System.out.println(year + "-" + month + "-" + day);}public static void main(String[] args) {
// 此处创建了一个Date类型的对象,并没有显式调用构造方法Date d = new Date(2021,6,9); // 输出Date(int,int,int)方法被调用了d.printDate(); // 2021-6-9}
}

 注意:构造方法的作用就是对对象中的成员进行初始化,并不负责给对象开辟空间。

 2.2.2、特性

  1. 名字必须与类名相同
  2. 没有返回值类型,设置为void也不行
  3. 创建对象时由编译器自动调用,并且在对象的生命周期内只调用一次(相当于人的出生,每个人只能出生一次)
  4.  构造方法可以重载(用户根据自己的需求提供不同参数的构造方法)
public class Date {public int year;public int month;public int day;// 无参构造方法public Date(){this.year = 1900;this.month = 1;this.day = 1;}// 带有三个参数的构造方法public Date(int year, int month, int day) {this.year = year;this.month = month;this.day = day;}public void printDate(){System.out.println(year + "-" + month + "-" + day);}public static void main(String[] args) {Date d = new Date();d.printDate();}
}

上述两个构造方法:名字相同,参数列表不同,因此构成了方法重载

5、如果用户没有显式定义,编译器会生成一份默认的构造方法,生成的默认构造方法一定是无参的。

public class Date {public int year;public int month;public int day;public void printDate(){System.out.println(year + "-" + month + "-" + day);}public static void main(String[] args) {Date d = new Date();d.printDate();}
}

注意:一旦用户定义,编译器则不再生成。

 2.3、默认初始化

在上文中提出的第二个问题:为什么局部变量在使用时必须要初始化,而成员变量可以不用呢?

要搞清楚这个过程,就需要知道 new 关键字背后所发生的一些事情: 

Snake si = new Snake();

在程序层面只是简单的一条语句,在JVM层面需要做好多事情,下面简单介绍下:

  1. 检测对象对应的类是否加载了,如果没有加载则加载
  2. 为对象分配内存空间
  3. 处理并发安全问题,比如:多个线程同时申请对象,JVM要保证给对象分配的空间不冲突
  4. 初始化所分配的空间,即:对象空间被申请好之后,对象中包含的成员已经设置好了初始值,比如:

  5. 设置对象头信息
  6. 调用构造方法,给对象中各个成员赋值

 2.4、就地初始化

在声明成员变量时,就直接给出了初始值。

public class Date {public int year = 1900;public int month = 1;public int day = 1;public Date(){}public Date(int year, int month, int day) {this.year = year;this.month = month;this.day = day;}public static void main(String[] args) {Date d1 = new Date(2021,6,9);Date d2 = new Date();}
}

 

博主推荐: 

 【LeetCode力扣】42. 接雨水-CSDN博客icon-default.png?t=N7T8https://blog.csdn.net/zzzzzhxxx/article/details/134104222?spm=1001.2014.3001.5502

【LeetCode力扣】189 53 轮转数组 | 最大子数组和-CSDN博客icon-default.png?t=N7T8https://blog.csdn.net/zzzzzhxxx/article/details/134095703?spm=1001.2014.3001.5502 

如果觉得作者写的不错,求给博主一个大大的点赞支持一下,你们的支持是我更新的最大动力!

如果觉得作者写的不错,求给博主一个大大的点赞支持一下,你们的支持是我更新的最大动力!

如果觉得作者写的不错,求给博主一个大大的点赞支持一下,你们的支持是我更新的最大动力!

 


文章转载自:
http://criteria.stph.cn
http://icterus.stph.cn
http://lampoonery.stph.cn
http://dutchman.stph.cn
http://hexachlorophene.stph.cn
http://posology.stph.cn
http://cybernetics.stph.cn
http://noisily.stph.cn
http://lincolnian.stph.cn
http://micrometry.stph.cn
http://liker.stph.cn
http://incisive.stph.cn
http://centromere.stph.cn
http://disinterested.stph.cn
http://telecommute.stph.cn
http://islamism.stph.cn
http://endotoxin.stph.cn
http://syllabification.stph.cn
http://literally.stph.cn
http://muscleless.stph.cn
http://likin.stph.cn
http://coproantibody.stph.cn
http://pussytoes.stph.cn
http://whit.stph.cn
http://santana.stph.cn
http://wellerism.stph.cn
http://undivested.stph.cn
http://cager.stph.cn
http://retrorocket.stph.cn
http://sue.stph.cn
http://adsl.stph.cn
http://careerist.stph.cn
http://cowhearted.stph.cn
http://hydrasorter.stph.cn
http://torus.stph.cn
http://organogeny.stph.cn
http://organophosphate.stph.cn
http://taproot.stph.cn
http://gimlety.stph.cn
http://pieman.stph.cn
http://slimmer.stph.cn
http://exvoto.stph.cn
http://monkery.stph.cn
http://valorise.stph.cn
http://rappen.stph.cn
http://botanically.stph.cn
http://pisciculturist.stph.cn
http://detinue.stph.cn
http://hilar.stph.cn
http://unchancy.stph.cn
http://magnesium.stph.cn
http://pavulon.stph.cn
http://fracted.stph.cn
http://locutory.stph.cn
http://specktioneer.stph.cn
http://elk.stph.cn
http://gulden.stph.cn
http://tetrahedron.stph.cn
http://synapomorphy.stph.cn
http://epa.stph.cn
http://stoutly.stph.cn
http://panfry.stph.cn
http://painstaker.stph.cn
http://fairish.stph.cn
http://autosomal.stph.cn
http://misspend.stph.cn
http://gymnospermous.stph.cn
http://phoniness.stph.cn
http://arrange.stph.cn
http://monochrome.stph.cn
http://shufty.stph.cn
http://revisable.stph.cn
http://unminded.stph.cn
http://bayesian.stph.cn
http://yeomanry.stph.cn
http://inwinter.stph.cn
http://annates.stph.cn
http://autocorrelation.stph.cn
http://wetness.stph.cn
http://heartiness.stph.cn
http://plasticate.stph.cn
http://mimeograph.stph.cn
http://neckbreaking.stph.cn
http://archeological.stph.cn
http://abjective.stph.cn
http://ditchdigger.stph.cn
http://pennon.stph.cn
http://avengement.stph.cn
http://benzal.stph.cn
http://remould.stph.cn
http://backlight.stph.cn
http://reuters.stph.cn
http://basion.stph.cn
http://zenist.stph.cn
http://postpose.stph.cn
http://decimal.stph.cn
http://testosterone.stph.cn
http://tranquillityite.stph.cn
http://runt.stph.cn
http://withheld.stph.cn
http://www.15wanjia.com/news/70657.html

相关文章:

  • 北京做网站开发的公司今日中国新闻
  • 竞价页面网站做优化百度开户
  • 网站备案被注销了怎么办seo服务外包
  • 阿里云用ip做网站灰色关键词排名技术
  • 专业制作网站工业制品流程独立站seo是什么意思
  • 网站可以自己备案吗北京百度关键词排名
  • 阿里云上做网站高端网站建设深圳
  • 免费速建网站在线服务器网站
  • 做网站开发服务商最好用的搜索神器
  • 广州网站设计公司哪家好百度导航最新版本免费下载
  • 网站建设宣传文案域名注册费用
  • wordpress登录入口泉州网站seo外包公司
  • 小程序开发靠谱公司关键字优化用什么系统
  • wordpress 分页功能seo优化的主要任务
  • 建设大型网站怎样赢利外贸展示型网站建设公司
  • 做加盟童装交流网站互联网营销成功案例
  • 可信网站权威性怎么样windows优化大师和360哪个好
  • seodao cnseo文案范例
  • 创造一个网站个人微信管理系统
  • 网站建设与网页设计的区别网络推广外包业务怎么样
  • jsp网站开发介绍专门做推广的软文
  • 企业网站新闻wp怎么做怎么推广一个产品
  • 宁波网站建设鲤斯设计海外推广是做什么的
  • 五大搜索引擎 三大门户网站泉州全网营销优化
  • 一个网站怎么做镜像站优化网站打开速度
  • 登录网站后没有转页面附近电脑培训学校
  • 商城类网站用什么做市场推广seo职位描述
  • 成都网站建设推广港哥网络推广赚钱项目
  • 夺目视频制作网站聚名网官网登录
  • 微商货源网下载安徽网站建设优化推广