菜鸟学习笔记:Java面向对象篇上

  • Java面向对象的思想
  • Java程序执行过程内存分析
  • Java垃圾回收机制
  • 构造方法
  • 方法重载(overload)
  • static关键字
  • this关键字

Java面向对象的思想

在之前的教程中往往都会反复强调什么c语言是面向过程的语言啦,Java是面向对象的语言啦,什么面向对象和面向过程有很大区别了。。。。。。感觉总是把人给绕的一头雾水,其实从程序执行流程(这里是程序执行,不包括类加载之类的JVM内部操作)的角度来说Java和C没啥区别,同样是从main函数开始从上往下一行一行依次执行,区别在于Java可以将一部分属性和方法绑定在一起,从而组成一个对象,在主程序中直接对对象进行操作,(专业概念是以类方式组织代码,以对象方式封装数据)这种思想使得我们可以把一个种类的事务进行统一管理,让编程更加符合现实生活。

是对具体事物的抽象,具有一定相同属性和行为的事务都可以归为一类,在同一类当中又可以根据其事务的不同点进一步划分出子类。下面举例来说明:

#mermaid-svg-YLPdNuqogFSoFNUi .label{font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family);fill:#333;color:#333}#mermaid-svg-YLPdNuqogFSoFNUi .label text{fill:#333}#mermaid-svg-YLPdNuqogFSoFNUi .node rect,#mermaid-svg-YLPdNuqogFSoFNUi .node circle,#mermaid-svg-YLPdNuqogFSoFNUi .node ellipse,#mermaid-svg-YLPdNuqogFSoFNUi .node polygon,#mermaid-svg-YLPdNuqogFSoFNUi .node path{fill:#ECECFF;stroke:#9370db;stroke-width:1px}#mermaid-svg-YLPdNuqogFSoFNUi .node .label{text-align:center;fill:#333}#mermaid-svg-YLPdNuqogFSoFNUi .node.clickable{cursor:pointer}#mermaid-svg-YLPdNuqogFSoFNUi .arrowheadPath{fill:#333}#mermaid-svg-YLPdNuqogFSoFNUi .edgePath .path{stroke:#333;stroke-width:1.5px}#mermaid-svg-YLPdNuqogFSoFNUi .flowchart-link{stroke:#333;fill:none}#mermaid-svg-YLPdNuqogFSoFNUi .edgeLabel{background-color:#e8e8e8;text-align:center}#mermaid-svg-YLPdNuqogFSoFNUi .edgeLabel rect{opacity:0.9}#mermaid-svg-YLPdNuqogFSoFNUi .edgeLabel span{color:#333}#mermaid-svg-YLPdNuqogFSoFNUi .cluster rect{fill:#ffffde;stroke:#aa3;stroke-width:1px}#mermaid-svg-YLPdNuqogFSoFNUi .cluster text{fill:#333}#mermaid-svg-YLPdNuqogFSoFNUi div.mermaidTooltip{position:absolute;text-align:center;max-width:200px;padding:2px;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family);font-size:12px;background:#ffffde;border:1px solid #aa3;border-radius:2px;pointer-events:none;z-index:100}#mermaid-svg-YLPdNuqogFSoFNUi .actor{stroke:#ccf;fill:#ECECFF}#mermaid-svg-YLPdNuqogFSoFNUi text.actor>tspan{fill:#000;stroke:none}#mermaid-svg-YLPdNuqogFSoFNUi .actor-line{stroke:grey}#mermaid-svg-YLPdNuqogFSoFNUi .messageLine0{stroke-width:1.5;stroke-dasharray:none;stroke:#333}#mermaid-svg-YLPdNuqogFSoFNUi .messageLine1{stroke-width:1.5;stroke-dasharray:2, 2;stroke:#333}#mermaid-svg-YLPdNuqogFSoFNUi #arrowhead path{fill:#333;stroke:#333}#mermaid-svg-YLPdNuqogFSoFNUi .sequenceNumber{fill:#fff}#mermaid-svg-YLPdNuqogFSoFNUi #sequencenumber{fill:#333}#mermaid-svg-YLPdNuqogFSoFNUi #crosshead path{fill:#333;stroke:#333}#mermaid-svg-YLPdNuqogFSoFNUi .messageText{fill:#333;stroke:#333}#mermaid-svg-YLPdNuqogFSoFNUi .labelBox{stroke:#ccf;fill:#ECECFF}#mermaid-svg-YLPdNuqogFSoFNUi .labelText,#mermaid-svg-YLPdNuqogFSoFNUi .labelText>tspan{fill:#000;stroke:none}#mermaid-svg-YLPdNuqogFSoFNUi .loopText,#mermaid-svg-YLPdNuqogFSoFNUi .loopText>tspan{fill:#000;stroke:none}#mermaid-svg-YLPdNuqogFSoFNUi .loopLine{stroke-width:2px;stroke-dasharray:2, 2;stroke:#ccf;fill:#ccf}#mermaid-svg-YLPdNuqogFSoFNUi .note{stroke:#aa3;fill:#fff5ad}#mermaid-svg-YLPdNuqogFSoFNUi .noteText,#mermaid-svg-YLPdNuqogFSoFNUi .noteText>tspan{fill:#000;stroke:none}#mermaid-svg-YLPdNuqogFSoFNUi .activation0{fill:#f4f4f4;stroke:#666}#mermaid-svg-YLPdNuqogFSoFNUi .activation1{fill:#f4f4f4;stroke:#666}#mermaid-svg-YLPdNuqogFSoFNUi .activation2{fill:#f4f4f4;stroke:#666}#mermaid-svg-YLPdNuqogFSoFNUi .mermaid-main-font{font-family:"trebuchet ms", verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-YLPdNuqogFSoFNUi .section{stroke:none;opacity:0.2}#mermaid-svg-YLPdNuqogFSoFNUi .section0{fill:rgba(102,102,255,0.49)}#mermaid-svg-YLPdNuqogFSoFNUi .section2{fill:#fff400}#mermaid-svg-YLPdNuqogFSoFNUi .section1,#mermaid-svg-YLPdNuqogFSoFNUi .section3{fill:#fff;opacity:0.2}#mermaid-svg-YLPdNuqogFSoFNUi .sectionTitle0{fill:#333}#mermaid-svg-YLPdNuqogFSoFNUi .sectionTitle1{fill:#333}#mermaid-svg-YLPdNuqogFSoFNUi .sectionTitle2{fill:#333}#mermaid-svg-YLPdNuqogFSoFNUi .sectionTitle3{fill:#333}#mermaid-svg-YLPdNuqogFSoFNUi .sectionTitle{text-anchor:start;font-size:11px;text-height:14px;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-YLPdNuqogFSoFNUi .grid .tick{stroke:#d3d3d3;opacity:0.8;shape-rendering:crispEdges}#mermaid-svg-YLPdNuqogFSoFNUi .grid .tick text{font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-YLPdNuqogFSoFNUi .grid path{stroke-width:0}#mermaid-svg-YLPdNuqogFSoFNUi .today{fill:none;stroke:red;stroke-width:2px}#mermaid-svg-YLPdNuqogFSoFNUi .task{stroke-width:2}#mermaid-svg-YLPdNuqogFSoFNUi .taskText{text-anchor:middle;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-YLPdNuqogFSoFNUi .taskText:not([font-size]){font-size:11px}#mermaid-svg-YLPdNuqogFSoFNUi .taskTextOutsideRight{fill:#000;text-anchor:start;font-size:11px;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-YLPdNuqogFSoFNUi .taskTextOutsideLeft{fill:#000;text-anchor:end;font-size:11px}#mermaid-svg-YLPdNuqogFSoFNUi .task.clickable{cursor:pointer}#mermaid-svg-YLPdNuqogFSoFNUi .taskText.clickable{cursor:pointer;fill:#003163 !important;font-weight:bold}#mermaid-svg-YLPdNuqogFSoFNUi .taskTextOutsideLeft.clickable{cursor:pointer;fill:#003163 !important;font-weight:bold}#mermaid-svg-YLPdNuqogFSoFNUi .taskTextOutsideRight.clickable{cursor:pointer;fill:#003163 !important;font-weight:bold}#mermaid-svg-YLPdNuqogFSoFNUi .taskText0,#mermaid-svg-YLPdNuqogFSoFNUi .taskText1,#mermaid-svg-YLPdNuqogFSoFNUi .taskText2,#mermaid-svg-YLPdNuqogFSoFNUi .taskText3{fill:#fff}#mermaid-svg-YLPdNuqogFSoFNUi .task0,#mermaid-svg-YLPdNuqogFSoFNUi .task1,#mermaid-svg-YLPdNuqogFSoFNUi .task2,#mermaid-svg-YLPdNuqogFSoFNUi .task3{fill:#8a90dd;stroke:#534fbc}#mermaid-svg-YLPdNuqogFSoFNUi .taskTextOutside0,#mermaid-svg-YLPdNuqogFSoFNUi .taskTextOutside2{fill:#000}#mermaid-svg-YLPdNuqogFSoFNUi .taskTextOutside1,#mermaid-svg-YLPdNuqogFSoFNUi .taskTextOutside3{fill:#000}#mermaid-svg-YLPdNuqogFSoFNUi .active0,#mermaid-svg-YLPdNuqogFSoFNUi .active1,#mermaid-svg-YLPdNuqogFSoFNUi .active2,#mermaid-svg-YLPdNuqogFSoFNUi .active3{fill:#bfc7ff;stroke:#534fbc}#mermaid-svg-YLPdNuqogFSoFNUi .activeText0,#mermaid-svg-YLPdNuqogFSoFNUi .activeText1,#mermaid-svg-YLPdNuqogFSoFNUi .activeText2,#mermaid-svg-YLPdNuqogFSoFNUi .activeText3{fill:#000 !important}#mermaid-svg-YLPdNuqogFSoFNUi .done0,#mermaid-svg-YLPdNuqogFSoFNUi .done1,#mermaid-svg-YLPdNuqogFSoFNUi .done2,#mermaid-svg-YLPdNuqogFSoFNUi .done3{stroke:grey;fill:#d3d3d3;stroke-width:2}#mermaid-svg-YLPdNuqogFSoFNUi .doneText0,#mermaid-svg-YLPdNuqogFSoFNUi .doneText1,#mermaid-svg-YLPdNuqogFSoFNUi .doneText2,#mermaid-svg-YLPdNuqogFSoFNUi .doneText3{fill:#000 !important}#mermaid-svg-YLPdNuqogFSoFNUi .crit0,#mermaid-svg-YLPdNuqogFSoFNUi .crit1,#mermaid-svg-YLPdNuqogFSoFNUi .crit2,#mermaid-svg-YLPdNuqogFSoFNUi .crit3{stroke:#f88;fill:red;stroke-width:2}#mermaid-svg-YLPdNuqogFSoFNUi .activeCrit0,#mermaid-svg-YLPdNuqogFSoFNUi .activeCrit1,#mermaid-svg-YLPdNuqogFSoFNUi .activeCrit2,#mermaid-svg-YLPdNuqogFSoFNUi .activeCrit3{stroke:#f88;fill:#bfc7ff;stroke-width:2}#mermaid-svg-YLPdNuqogFSoFNUi .doneCrit0,#mermaid-svg-YLPdNuqogFSoFNUi .doneCrit1,#mermaid-svg-YLPdNuqogFSoFNUi .doneCrit2,#mermaid-svg-YLPdNuqogFSoFNUi .doneCrit3{stroke:#f88;fill:#d3d3d3;stroke-width:2;cursor:pointer;shape-rendering:crispEdges}#mermaid-svg-YLPdNuqogFSoFNUi .milestone{transform:rotate(45deg) scale(0.8, 0.8)}#mermaid-svg-YLPdNuqogFSoFNUi .milestoneText{font-style:italic}#mermaid-svg-YLPdNuqogFSoFNUi .doneCritText0,#mermaid-svg-YLPdNuqogFSoFNUi .doneCritText1,#mermaid-svg-YLPdNuqogFSoFNUi .doneCritText2,#mermaid-svg-YLPdNuqogFSoFNUi .doneCritText3{fill:#000 !important}#mermaid-svg-YLPdNuqogFSoFNUi .activeCritText0,#mermaid-svg-YLPdNuqogFSoFNUi .activeCritText1,#mermaid-svg-YLPdNuqogFSoFNUi .activeCritText2,#mermaid-svg-YLPdNuqogFSoFNUi .activeCritText3{fill:#000 !important}#mermaid-svg-YLPdNuqogFSoFNUi .titleText{text-anchor:middle;font-size:18px;fill:#000;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-YLPdNuqogFSoFNUi g.classGroup text{fill:#9370db;stroke:none;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family);font-size:10px}#mermaid-svg-YLPdNuqogFSoFNUi g.classGroup text .title{font-weight:bolder}#mermaid-svg-YLPdNuqogFSoFNUi g.clickable{cursor:pointer}#mermaid-svg-YLPdNuqogFSoFNUi g.classGroup rect{fill:#ECECFF;stroke:#9370db}#mermaid-svg-YLPdNuqogFSoFNUi g.classGroup line{stroke:#9370db;stroke-width:1}#mermaid-svg-YLPdNuqogFSoFNUi .classLabel .box{stroke:none;stroke-width:0;fill:#ECECFF;opacity:0.5}#mermaid-svg-YLPdNuqogFSoFNUi .classLabel .label{fill:#9370db;font-size:10px}#mermaid-svg-YLPdNuqogFSoFNUi .relation{stroke:#9370db;stroke-width:1;fill:none}#mermaid-svg-YLPdNuqogFSoFNUi .dashed-line{stroke-dasharray:3}#mermaid-svg-YLPdNuqogFSoFNUi #compositionStart{fill:#9370db;stroke:#9370db;stroke-width:1}#mermaid-svg-YLPdNuqogFSoFNUi #compositionEnd{fill:#9370db;stroke:#9370db;stroke-width:1}#mermaid-svg-YLPdNuqogFSoFNUi #aggregationStart{fill:#ECECFF;stroke:#9370db;stroke-width:1}#mermaid-svg-YLPdNuqogFSoFNUi #aggregationEnd{fill:#ECECFF;stroke:#9370db;stroke-width:1}#mermaid-svg-YLPdNuqogFSoFNUi #dependencyStart{fill:#9370db;stroke:#9370db;stroke-width:1}#mermaid-svg-YLPdNuqogFSoFNUi #dependencyEnd{fill:#9370db;stroke:#9370db;stroke-width:1}#mermaid-svg-YLPdNuqogFSoFNUi #extensionStart{fill:#9370db;stroke:#9370db;stroke-width:1}#mermaid-svg-YLPdNuqogFSoFNUi #extensionEnd{fill:#9370db;stroke:#9370db;stroke-width:1}#mermaid-svg-YLPdNuqogFSoFNUi .commit-id,#mermaid-svg-YLPdNuqogFSoFNUi .commit-msg,#mermaid-svg-YLPdNuqogFSoFNUi .branch-label{fill:lightgrey;color:lightgrey;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-YLPdNuqogFSoFNUi .pieTitleText{text-anchor:middle;font-size:25px;fill:#000;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-YLPdNuqogFSoFNUi .slice{font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-YLPdNuqogFSoFNUi g.stateGroup text{fill:#9370db;stroke:none;font-size:10px;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-YLPdNuqogFSoFNUi g.stateGroup text{fill:#9370db;fill:#333;stroke:none;font-size:10px}#mermaid-svg-YLPdNuqogFSoFNUi g.statediagram-cluster .cluster-label text{fill:#333}#mermaid-svg-YLPdNuqogFSoFNUi g.stateGroup .state-title{font-weight:bolder;fill:#000}#mermaid-svg-YLPdNuqogFSoFNUi g.stateGroup rect{fill:#ECECFF;stroke:#9370db}#mermaid-svg-YLPdNuqogFSoFNUi g.stateGroup line{stroke:#9370db;stroke-width:1}#mermaid-svg-YLPdNuqogFSoFNUi .transition{stroke:#9370db;stroke-width:1;fill:none}#mermaid-svg-YLPdNuqogFSoFNUi .stateGroup .composit{fill:white;border-bottom:1px}#mermaid-svg-YLPdNuqogFSoFNUi .stateGroup .alt-composit{fill:#e0e0e0;border-bottom:1px}#mermaid-svg-YLPdNuqogFSoFNUi .state-note{stroke:#aa3;fill:#fff5ad}#mermaid-svg-YLPdNuqogFSoFNUi .state-note text{fill:black;stroke:none;font-size:10px}#mermaid-svg-YLPdNuqogFSoFNUi .stateLabel .box{stroke:none;stroke-width:0;fill:#ECECFF;opacity:0.7}#mermaid-svg-YLPdNuqogFSoFNUi .edgeLabel text{fill:#333}#mermaid-svg-YLPdNuqogFSoFNUi .stateLabel text{fill:#000;font-size:10px;font-weight:bold;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-YLPdNuqogFSoFNUi .node circle.state-start{fill:black;stroke:black}#mermaid-svg-YLPdNuqogFSoFNUi .node circle.state-end{fill:black;stroke:white;stroke-width:1.5}#mermaid-svg-YLPdNuqogFSoFNUi #statediagram-barbEnd{fill:#9370db}#mermaid-svg-YLPdNuqogFSoFNUi .statediagram-cluster rect{fill:#ECECFF;stroke:#9370db;stroke-width:1px}#mermaid-svg-YLPdNuqogFSoFNUi .statediagram-cluster rect.outer{rx:5px;ry:5px}#mermaid-svg-YLPdNuqogFSoFNUi .statediagram-state .divider{stroke:#9370db}#mermaid-svg-YLPdNuqogFSoFNUi .statediagram-state .title-state{rx:5px;ry:5px}#mermaid-svg-YLPdNuqogFSoFNUi .statediagram-cluster.statediagram-cluster .inner{fill:white}#mermaid-svg-YLPdNuqogFSoFNUi .statediagram-cluster.statediagram-cluster-alt .inner{fill:#e0e0e0}#mermaid-svg-YLPdNuqogFSoFNUi .statediagram-cluster .inner{rx:0;ry:0}#mermaid-svg-YLPdNuqogFSoFNUi .statediagram-state rect.basic{rx:5px;ry:5px}#mermaid-svg-YLPdNuqogFSoFNUi .statediagram-state rect.divider{stroke-dasharray:10,10;fill:#efefef}#mermaid-svg-YLPdNuqogFSoFNUi .note-edge{stroke-dasharray:5}#mermaid-svg-YLPdNuqogFSoFNUi .statediagram-note rect{fill:#fff5ad;stroke:#aa3;stroke-width:1px;rx:0;ry:0}:root{--mermaid-font-family: '"trebuchet ms", verdana, arial';--mermaid-font-family: "Comic Sans MS", "Comic Sans", cursive}#mermaid-svg-YLPdNuqogFSoFNUi .error-icon{fill:#522}#mermaid-svg-YLPdNuqogFSoFNUi .error-text{fill:#522;stroke:#522}#mermaid-svg-YLPdNuqogFSoFNUi .edge-thickness-normal{stroke-width:2px}#mermaid-svg-YLPdNuqogFSoFNUi .edge-thickness-thick{stroke-width:3.5px}#mermaid-svg-YLPdNuqogFSoFNUi .edge-pattern-solid{stroke-dasharray:0}#mermaid-svg-YLPdNuqogFSoFNUi .edge-pattern-dashed{stroke-dasharray:3}#mermaid-svg-YLPdNuqogFSoFNUi .edge-pattern-dotted{stroke-dasharray:2}#mermaid-svg-YLPdNuqogFSoFNUi .marker{fill:#333}#mermaid-svg-YLPdNuqogFSoFNUi .marker.cross{stroke:#333}:root { --mermaid-font-family: "trebuchet ms", verdana, arial;}#mermaid-svg-YLPdNuqogFSoFNUi {color: rgba(0, 0, 0, 0.75);font: ;}生物类寿命生活环境...grow()reproduction()动物类代谢方式...move()植物类微生物类鱼类鸟类哺乳类

生物类是对所有有生命事物的抽象,而生物又可以分为植物、动物和微生物,它们有生物所有的属性和行为,但又有着自己独有的特征,所以它们是生物类的子类。同样的鱼类鸟类也是动物类的子类。
对象则是某个类的具体实例,像小明这个人就属于哺乳类的一个实例,同时也是动物类、生物类的一个实例,人有自己的生活环境、有自己的寿命,也可以成长、繁殖等等生物类所定义的属性都具有,同样动物类所有的属性和行为也都具备,这也是之后要说的多态。
Java类和对象的概念大致就是如此,对象是Java的核心。下面用一个学生的例子展示Java之中的类定义方法:

class Student {   //注意类的命名习惯String name;   //属性int id;public void study(){   //方法System.out.println(name + "在学习"); //在使用String类型时"+"代表字符串拼接,与int类型不同。}
}

我们在在main函数中使用这个类

public static void main(String[] args) {Student oStu1 = new Student();  //Student属于引用类型,驼峰命名法中一般用o代表对象Student oStu2 = new Student();oStu1.name = "小明";  //设置属性oStu1.id = 123;oStu2.name = "老王";oStu2.id = 456;oStu1.study(); //调用方法会打印小明在学习。
}

这里说明一下属性是类中定义的每个成员应有的值,对每个对象都是不同的,而方法则是每个这一类所以实例都可以进行的操作。
在对象初始化时如果不对属性进行赋值则会保持其默认值。
Java运用引用类型的变量来表示一个对象,通常对象存储在内存中,引用类型通过存储对象的地址来找到对象。

Java程序执行过程内存分析

这一块本打算将类的加载过程也融入进来一块讲解,但写着写着发现不太适合Java新手阅读,所以单独抽出来作为一篇文章,大家想了解可以看 这里。
首先介绍几个概念:

当前执行线程的独占空间,以栈的数据结构出现。线程私有,它保存一个线程方法的调用状态(方法中的变量也在其中),压入栈的单位为栈帧。程序执行时main方法的栈帧先入栈,如果main方法中调用了A方法,那么在执行到A方法时它的栈帧入栈,等A方法执行完成后栈帧出栈,继续执行main方法。

是一块不连续的内存空间,JVM运行过程中最大的一块内容,被所有线程共享,主要用来存放new出来的对象以及数组。
方法区
是堆的一部分,用来存放字面量(文本字符串、八种基本类型的值、被声明为final的常量等)以及符号引用(类和方法的全限定名、字段的名称和描述符、方法的名称和描述符),它是所有的线程共享的一块区域,当方法区不能满足内存分配需求时会报出OutOfMemoryError。
对于类加载过程的说明我们继续以上面学生类为例。方便大家理解简化一下main函数

public static void main(String[] args) {Student oStu1 = new Student();  //Student属于引用类型,驼峰命名法中一般用o代表对象oStu1.name = "小明";  //设置属性oStu1.id = 123;oStu1.study(); //调用方法会打印小明在学习。
}

首先声明,程序执行时针对的是.class字节码文件,字节码文件我们看不懂,但执行的情况也是按照Java程序中一行一行来的,所以以Java程序来讲解,程序执行过程中首先main函数入栈,从第一行开始执行,当运行到Student oStu1 = new Student();时,程序会调用类加载器加载类。这时在方法区就会有类的信息(注意ststic静态代码块会在这个时候执行)。

类加载完后再堆栈中变量oStu1(oStu1就是引用变量)会被存储。

这样Student oStu1这半句已经执行完,到后半句new Student()时,在堆的非方法区部分会生成Student类的实例,实例中的属性会被赋予属性类型的默认值,方法会指向方法区Student类存储的方法,最后引用变量oStu1会指向这一实例,整个内存结构如图所示:

讲到这里大家应该了解引用变量了,其实就是在这个变量中存储的是对象的地址(实际在那个空间存储的是如十六进制的08A4这样的数,它代表对象在内存中的存储地址,程序根据地址可以找到变量并取值,这个过程图中用箭头表示),我们访问时可以通过地址直接取到对象。
然后程序继续执行,到oStu1.name = "小明"执行时,首先会根据oStu1的地址找到Student对象,然后改变对象中属性的值,oStu1.id = 123也是一个道理。
oStu1.study()首先会根据oStu1的地址找到Student对象,再根据对象的study指针找到方法区中的方法代码,将方法代码压入堆栈执行方法,执行到打印函数时System.out.println()函数入栈,栈结构如下:

输出结束后执行完成后System.out.println()方法出栈,继续执行study()函数,发现函数执行完成,study()函数出栈,继续执行main函数中的代码,然后main函数也执行完成,这时main函数出栈,整个程序结束。

Java垃圾回收机制

上面讲到再用new关键字创建对象时,堆中会分配一块内存给对象,但如果一真建立下去内存肯定会占满,所以及时清理不用的对象释放资源非常必要,在C语言中需要我们在完成操作后手动的释放资源(比如释放链表),在Java中的垃圾回收机制可以帮我们解决这个问题,在当没有引用变量指向一个对象时,这个对象将被Java的垃圾回收器自动清除,比如在上例中我们在添加一句:

public static void main(String[] args) {Student oStu1 = new Student();  //Student属于引用类型,驼峰命名法中一般用o代表对象oStu1.name = "小明";  //设置属性oStu1.id = 123;oStu1.study(); //调用方法会打印小明在学习。oStu1 = null; //这时堆中的Student对象就会被垃圾回收器清理掉
}

在Java中程序员无权调用垃圾回收器,虽然可以通过System.gc()方法通知GC运行或者finalize释放资源,但一般情况都靠程序自己来处理。

构造方法

构造方法又称构造器,用于构造类的实例(也就是对象),它是一种特殊的方法,方法名称与类名相同,会在执行new语句时调用。
下面举例来说明,同样是学生类,我们加上构造方法:

class Student {   //注意类的命名习惯String name;   //属性int id;public Student(String name, int id){ //构造方法this.name = name;this.id = id;}public void study(){   //方法System.out.println(name + "在学习"); //在使用String类型时"+"代表字符串拼接,与int类型不同。}
}

这时实例化对象就可以这样来:

public static void main(String[] args) {Student oStu1 = new Student("小明",123);  oStu1.study(); //调用方法会打印小明在学习。
}

大家应该明白构造方法是干嘛得了吧,但为什么之前的例子中没有这个构造方法不会报错那,因为如果在类中没有写构造方法那相当于:

//这个类的定义和最初例子中的类一致
class Student {   //注意类的命名习惯String name;   //属性int id;public Student(){ } //最初的例子默认自动添加public void study(){ //方法System.out.println(name + "在学习");}
}

构造方法的作用是帮助我们在对象初始化时进行一定的操作,比如对属性赋初值(static静态代码块是在类初始化时执行,先于构造方法,这个在面试中常考)。

方法重载(overload)

方法重载这个概念非常简单,但初学者比较容易和以后要讲的方法重写给搞混了,这就需要大家随用随看,多过几遍自然记住了。
方法重载就是指在一个类中可以有多个同名方法,在调用时根据参数数量不同选择不同的方法进行调用,构造方法同样可以重载,话不多说直接上例子,现在上面学生例子中需要添加学习的具体科目,所以可以在添加一个study方法,同时还需要在定义学生对象时就设置好姓名和id,所以代码可以修改成如下形式:

class Student {   //注意类的命名习惯String name;   //属性int id;public Student(){ }  //构造方法public Student(String name, int id){ //构造方法重载this.name = name;this.id = id;}public void study(){  //方法System.out.println(name + "在学习"); }public void study(String str){    //方法重载System.out.println(name + "在学习" + str); }
}

然后就可以这样调用:

public static void main(String[] args) {Student oStu1 = new Student("小明",123);  oStu1.study(); //调用方法会打印小明在学习。oStu1.study("语文"); //调用方法会打印小明在学习语文。Student oStu2 = new Student();  //另一种构造方式oStu2.name = "老王";oStu2.id = 456;oStu2.study("数学"); //调用方法会打印老王在学习数学。
}

注意参数不同表示参数类型、个数或顺序不同,如果只是形参的名称不同则不构成方法重载。

public Student(String name, int id){ }
public Student( int id, String name){ }
//这是两个不同方法形成方法重载public Student(String name, int id){ }
public Student( String name1,int id1){ }
//不构成重载会报错public void study(String str){}
public int study(String str){}
//也不构成重载会报错

static关键字

上面例子中的变量和方法调用必须要把类进行实例化,而由一类变量或方法直接通过类名就可以调用,这就是用static关键字修饰的类名和方法。实际项目中static变量主要运用于定义方法工具栏,我们将一些共性变量和方法用static类型存储在一个类中,以便我们在程序中随时调用。
static方法修饰的变量和方法由一个特点,在类被载入时就完成了初始化,一个类型只有一份,并且它被所有类的实例所共享。在调用时用“类名.属性”来调用。还是上面的例子

class Student {   //注意类的命名习惯static String cType = "学生类";public static void getType(){System.out.println("这是" + cType); }String name;   //属性 int id;public void study(){ //方法System.out.println(name + "在学习,"+ "我是" + cType); }
}

在调用时

public static void main(String[] args) {System.out.println(Student.cType); //打印学生类System.out.println(Student.getType()); //打印这是学生类Student oStu1 = new Student("小明",123);  oStu1.study(); //调用方法会打印小明在学习,我是学生类。表明对象可以使用static变量。}

static变量存放在方法区中,在之前讲解类加载过程时已经说明 ,形式如下:

注意由于静态属性和方法在类加载完成后就完成了初始化,所以静态方法不能调用非静态属性和方法。
对static关键字还有一个点就是静态代码块static{},这里的代码会在类初始化完成后进行,它可以使用静态变量。
对于静态变量方法与普通变量方法的区别是面试的常考问题,根据上面的原理基本可以分析出来,我在这里找了一篇比较好的总结大家可以看看。
链接

this关键字

相信学过JavaScript的同学对this关键字不太陌生,也相当头疼(本人也是这么过来的,以后有机会我也会发一遍JavaScript教程,希望大家也能支持),Java中this作用与其类似但没有那么恶心。
this被称为隐式参数,当我们调用方法时即使没有定义参数,程序也会默认传入一个名为this的参数,在普通方法中使用它会指向正在调用该方法的对象。而在构造方法中调用它会指向正要初始化的对象。

class Student {   //注意类的命名习惯public Student(){System.out.println(“构造方法被调用”); }public Student(String name, int id){ this.name = name;//这里是this的主要用途,因为此时该函数中的name代表外边传入的name,我们只能通过this获取类中的name属性。this.id = id;}String name;   //属性 int id;public void study(){   //方法this(); //this的特殊用法,用this调用构造方法,了解即可System.out.println(this.name + "在学习,"); }
}
public static void main(String[] args) {Student oStu1 = new Student("小明",123);  //这里调用this指向正要初始化的对象。也就是oStu1指向的对象Student oStu2 = new Student();  //另一种构造方式oStu2.name = "老王";oStu2.id = 456;oStu2.study(); //调用方法会构造方法被调用换行后在打印打印老王在学习,this正在调用该方法的对象也就是也就是oStu2指向的对象。
}

this关键字不能用于static修饰的静态方法,因为静态方法随类而创建,此时还没有对象,根据前面的知识很好理解这一点。

上一篇:菜鸟学习笔记:Java基础篇2(变量、运算符、流程控制语句、方法)
下一篇:菜鸟学习笔记:Java基础篇4(面向对象三大特征).

菜鸟学习笔记:Java基础篇3(面向对象思想、程序执行过程内存分析、面向对象重要概念)相关推荐

  1. Java学习笔记之基础篇

    Java学习笔记之基础篇 目录 Java如何体现平台的无关性? 面向对象(OO)的理解 面向对象和面向过程编程的区别 面向对象三大特征 静态绑定和动态绑定(后期绑定) 延伸:类之间的关系 组合(聚合) ...

  2. jqGrid 学习笔记整理——基础篇

    jqGrid 学习笔记整理--基础篇 jqGrid 实例中文版网址:http://blog.mn886.net/jqGrid/ 国外官网:http://www.trirand.com/blog/ 本人 ...

  3. 学习笔记-java基础-网络编程

    刚在培训机构学习完JAVA基础和WEB,感觉边学边忘.就把这作为记忆的笔记,我也不知道要不要记笔记,写在本子上太耗费时间了. 一.要想实现网络通信,首先要解决两个问题: ①:如何从网络中定位到一台或多 ...

  4. Java菜鸟教程系列 学习笔记总结 基础篇(1)

    基础语法 本博客通过学习菜鸟教程Java专栏,并整理得出的Java基础知识. 命名规范 1.项目名全部小写 2.包名全部小写 3.类名首字母大写,如果类名由多个单词组成,每个单词的首字母都要大写.如: ...

  5. golang学习笔记(基础篇)

    LCY~~Golang学习笔记 一.Go语言开发环境 ##安装Go开发包以及VsCode Go开发包与vscode配置安装教程网址:https://www.liwenzhou.com/posts/Go ...

  6. 狂神说Java学习笔记 Java基础

    目录 机器语言 第二代语言(汇编语言) 第三代语言 高级语言 Java特性和优势 JDK(Java Development Kit) JRE(Java Runtime Enviroment) JVM( ...

  7. Excel学习笔记之基础篇

    记录一下跟着网易云课堂大饼老师Excel课程学习的笔记,只能起一个大纲的作用吧,用来练习的数据表不方便上传,也懒得截图录屏做成GIF操作啥的.课程共分为基础篇,进阶篇之函数,进阶篇之数据透视,进阶篇之 ...

  8. 【ESP32最全学习笔记(基础篇)——1.ESP32简介】

      ESP32 新手?从这里开始! 关于本教程: ESP32 基础篇                                 1.ESP32简介 ☑ 2.ESP32 Arduino 集成开发环 ...

  9. MySQL学习笔记(基础篇未完待补充)

    一.MySQL数据库基 目录 一.MySQL数据库基础篇 1.数据库概述与MySQL安装篇 第1章:数据库概述 1.为什么要使用数据库 2. 数据库与数据库管理系统 2.2 数据库与数据库管理系统的关 ...

最新文章

  1. 软考-网络总结-协议
  2. [BZOJ1007] [HNOI2008] 水平可见直线 (凸包)
  3. python import变量_Python import模块调用
  4. php datatype=quot;requirequot;,我的问题谁能帮我解决一下??
  5. 听说蒂姆·库克来了?这和我们有什么关系!
  6. 7.python xmlrpclib及allownone作用
  7. win7系统服务器环境配置,win7系统服务器环境配置
  8. 2022大厂面试必问要点总结
  9. 剑指offer之 从尾到头打印链表
  10. 【7】测试用例设计-等价类分析法
  11. python如何读取文件建立字典_如何使用python从文本文件创建字典
  12. 什么是防病毒网关 防病毒网关的功能特点
  13. linux中mvn命令的下载与安装
  14. 被发哥拉着跪向观众,朱军为何如此惊魂?
  15. fcitx 添加输入法但并没有输入候选项
  16. 信息系统项目管理师学习笔记5——信息化与信息系统5
  17. OOP Class具体解释
  18. 微信小程序用户登录信息过期处理
  19. 【操作系统】内存管理
  20. Array与ArrayList有什么区别

热门文章

  1. python绘图设置正交坐标等距_matplotlib画图教程,设置坐标轴标签和间距
  2. 泸州职称计算机,2018年11月四川泸州职称计算机考试10月8日开始报名
  3. php哈希取模,PHP取模hash和一致性hash操作Memcached分布式集群
  4. ai电磁组属于什么组_星巴克的“气氛组”究竟是一个什么样的神秘组织?
  5. Kubernetes-保障集群内节点和网络安全
  6. vue-cli3.0 初体验
  7. Python学习 Day4-1 Python3 条件控制、循环语句
  8. Crazypony四轴飞行器代码框架
  9. 自动生成数学题型二(框架struts2)题型如((a+b)*c=d)
  10. Uva 11178 Morley定理