class="tags" href="/tags/Lei.html" title=类>类">class="tags" href="/tags/JAVA.html" title=Java>Java学习之class="tags" href="/tags/Lei.html" title=类>类
学习要点
- 面向过程
- 面向对象
- 抽象
- class="tags" href="/tags/Lei.html" title=类>类
- class="tags" href="/tags/Lei.html" title=类>类的构造方法
- class="tags" href="/tags/Lei.html" title=类>类中常见的关键字
- class="tags" href="/tags/Lei.html" title=类>类的成员方法
- class="tags" href="/tags/Lei.html" title=类>类的成员变量
一、面向过程
程序
程序的概念
程序一词来自生活,通常指完成某些事情的一种既定方式和过程。
可以将程序看成对一系列动作的执行过程的描述。
计算机程序
为了执行某些操作或解决某个问题而编写的一系列有序指令的集合。
面向过程程序的定义
程序=数据+算法+文档;
程序的组织单位为函数,通过函数间的调用实现软件功能。
变量
- 指向一个对象或包含一个值的标识符,都可以称为变量。
- 当变量的class="tags" href="/tags/Lei.html" title=类>类型为基本型别(short, byte, int, long, float, double, boolean, char)时,变量的内容是一个值;当变量的class="tags" href="/tags/Lei.html" title=类>类型为 Object 及其子class="tags" href="/tags/Lei.html" title=类>类时,变量的内容是指向一个对象的引用。
流程控制
常见流程控制:顺序、分支选择、循环。
二、面向对象
什么是对象?
- 万物皆对象!对象具有属性(静态特性)和行为(动态特性)。
- 对象object也被称为实例——instance。
什么是面向对象?
程序的世界是虚拟的,面向对象技术利用“面向对象思想”去描述“面向对象的世界”,实现了虚拟世界和现实世界的一致性。更加符合人的思维习惯。
面向对象设计(OOD)和开发程序(OOP)的好处
- 用户、软件设计人员、软件开发人员之间的交流更加容易和顺畅
- 提高设计和开发效率
三、抽象
在软件设计阶段,如何使用面向对象进行程序的设计?
问题:如何在计算机世界描述他们?
- class="tags" href="/tags/JAVA.html" title=Java>Java语言是面向对象的语言,在描述以上对象的时候是否要一只只小动物进行描述?虚拟世界和现实世界一对一映射,太繁琐,并不高明。所以面向对象的编程语言使用了class模板,即程序员在写代码的时候使用class去描述他们:对这些动物分class="tags" href="/tags/Lei.html" title=类>类,找出共性,做出模板,当计算机虚拟世界需要具体对象的时候,使用模板生成一个具体对象即可。
- 把对象抽象出class="tags" href="/tags/Lei.html" title=类>类,就是面向对象的程序设计。
class="tags" href="/tags/Lei.html" title=类>类三步骤">从现实世界抽象出class="tags" href="/tags/Lei.html" title=类>类三步骤
第一步:发现class="tags" href="/tags/Lei.html" title=类>类
第二步:发现class="tags" href="/tags/Lei.html" title=类>类的属性
第三步:发现class="tags" href="/tags/Lei.html" title=类>类的方法
第四步:使用class="tags" href="/tags/Lei.html" title=类>类图描述class="tags" href="/tags/Lei.html" title=类>类
上机练习:使用代码开发电子宠物系统
- 系统中有狗狗和企鹅
- 系统提供宠物选择 //待完成
- 狗狗有品种选择、企鹅性别可选 //待完成
class="tags" href="/tags/Lei.html" title=类>类">四、class="tags" href="/tags/Lei.html" title=类>类
class="tags" href="/tags/Lei.html" title=类>类模板">javaclass="tags" href="/tags/Lei.html" title=类>类模板
[修饰符] class class="tags" href="/tags/Lei.html" title=类>类名 {
零到多个构造方法(也称为构造函数、构造器)
零到多个成员变量(也称为属性、字段、域。属性专指带有getter和setter访问器)
零到多个成员方法
}
class="tags" href="/tags/Lei.html" title=类>类的访问修饰符:无、public、final、abstract
注意:没有修饰符的class="tags" href="/tags/Lei.html" title=类>类只在本包中可见。
class="tags" href="/tags/Lei.html" title=类>类的使用">class="tags" href="/tags/Lei.html" title=类>类的使用
class="prettyprint">class=" hljs php">
Dog dog;
dog = Dog();
dog.nickname=”旺旺”;
dog. ();
class="tags" href="/tags/Lei.html" title=类>类的构造方法">class="tags" href="/tags/Lei.html" title=类>类的构造方法
构造方法语法分析
[修饰符] 构造方法名(形参列表){
可执行语句;
}
修饰符:public、protected、private
方法名:同class="tags" href="/tags/Lei.html" title=类>类名
private常用于设计单例模式,例如:
class="prettyprint">class=" hljs cs">
Dog {
Dog currentDog = Dog();
Dog () {
currentDog;
}
() {
}
}
构造方法用来构造该class="tags" href="/tags/Lei.html" title=类>类的实例。class="tags" href="/tags/JAVA.html" title=Java>Java通过new关键字来调用构造方法,从而返回该class="tags" href="/tags/Lei.html" title=类>类的实例。
例如:Dog dog = new Dog();
一个class="tags" href="/tags/Lei.html" title=类>类创建对象的根本途径是通过构造方法构造实例。
如果没有自定义构造方法,系统默认提供一个无参构造方法。如果自定义了构造方法,则系统不再提供无参构造方法。
带参数的构造方法
如果希望在构造一个class="tags" href="/tags/Lei.html" title=类>类的实例的同时完成实例的初始化工作,则可以定义带参数的构造方法。
构造方法的形参名如果和成员变量的名称重复,按照java的规则:局部变量名和成员变量名重复,则局部变量覆盖成员变量。如果需要访问成员变量,则需要在成员变量名之前添加this关键字。
构造方法重载
- 重载的概念:方法名相同,参数项不同。与方法的返回值,修饰符无关。
- 构造方法可以重载,普通方法也可以重载。
- 重载构造方法的调用,形参和实参要一一对应。
构造方法的互相调用
class="tags" href="/tags/Lei.html" title=类>类中常见关键字">五、class="tags" href="/tags/Lei.html" title=类>类中常见关键字
this关键字用法
- this关键字表示当前对象的引用;用以区分同名成员变量。
- 在构造方法中引用该构造器正在初始化的对象。
- 在普通方法中引用调用该方法的对象。
static修饰的成员变量和成员方法
- static修饰的成员变量和成员方法可以通过class="tags" href="/tags/Lei.html" title=类>类名.成员访问。
- static可以用来修饰代码块。
- static修饰的成员变量、成员方法、代码块,相应数据和代码保存在内存的静态数据区。
- static修饰的成员变量、方法、内部class="tags" href="/tags/Lei.html" title=类>类、初始化代码块都是属于class="tags" href="/tags/Lei.html" title=类>类,没有static修饰的都属于实例。
示例代码:
class="prettyprint">class=" hljs java">class Web {
String logo= ;
count = ;
String ip = ;
(String ip) {
count++;
.ip = ip;
}
() {
System.out.println( +logo);
System.out.println( + count);
}
() {
System.out.println( + ip);
System.out.println( );
}
}
(String[] args) {
Web local1 = Web( );
Web.printCount();
local1.printIp();
Web local2 = Web( );
Web.printCount();
local2.printIp();
Web local3 = Web( );
Web.printCount();
local3.printIp();
Web local4 = Web( );
Web.printCount();
local4.printIp();
Web local5 = Web( );
Web.printCount();
local5.printIp();
}
}
final修饰的成员变量
- 用final修饰的成员变量称为常量。
- static final 变量class="tags" href="/tags/Lei.html" title=类>类型 变量名 : 静态常量。
class="tags" href="/tags/Lei.html" title=类>类的成员方法">六、class="tags" href="/tags/Lei.html" title=类>类的成员方法
语法格式
[修饰符] 方法返回值class="tags" href="/tags/Lei.html" title=类>类型 方法名( 形参列表 ) {
方法执行语句;
}
修饰符
public、protected、private、static、final、abstract。
其中public、protected、private只能选择其一,abstract与final只能出现其一;他们都可以和static组合起来修饰方法。
返回值
返回值需要使用return语句返回结果,结果的class="tags" href="/tags/Lei.html" title=类>类型必须和返回值class="tags" href="/tags/Lei.html" title=类>类型相匹配。如果没有返回值,则方法的返回值class="tags" href="/tags/Lei.html" title=类>类型必须为void。
class="prettyprint">class=" hljs cs">
Test {
age;
Test (){
age++;
;
}
(String[] args) {
Test t= Test();
t.grow().grow().grow().grow();
System. .println( +t.age);
}
}
class="tags" href="/tags/Lei.html" title=类>类中方法的调用">class="tags" href="/tags/Lei.html" title=类>类中方法的调用
- class="tags" href="/tags/Lei.html" title=类>类方法只能调用另外的class="tags" href="/tags/Lei.html" title=类>类方法,语法格式:class="tags" href="/tags/Lei.html" title=类>类名.方法名。class="tags" href="/tags/Lei.html" title=类>类名可省略。
- 实例方法间的调用,语法格式:对象名.方法名。实际写成this.方法名。this可省略。
- 实例方法可以调用class="tags" href="/tags/Lei.html" title=类>类方法(static修饰的方法)。
- 方法传递参数的机制(实参是如何传递参数给形参的?)。
- javaclass="tags" href="/tags/Lei.html" title=类>类中的方法都是按值方式传递参数的,即传递实参的副本给形参。
- 基本class="tags" href="/tags/Lei.html" title=类>类型传递值,引用class="tags" href="/tags/Lei.html" title=类>类型传递对象的地址(不是对象的副本)。
- Stringclass="tags" href="/tags/Lei.html" title=类>类型数据,如果是定义为双引号class="tags" href="/tags/Lei.html" title=类>类型的数据,则按照传值方式传递数据,如果采用构造方式构建的字符串,则传递字符串对象的引用。
形参个数可变的方法
在定义方法时,如果在最后一个形参的class="tags" href="/tags/Lei.html" title=类>类型后增加三个点(…),则表明该形参可以接受多个参数值,多个参数值被当成数组传入。
class="prettyprint">class=" hljs cs">
targs( a , String … names){
(String item : names){
System. .println(item);
}
}
注意:可变个数形参一个方法中只能有一个,并且要放在方法参数列表最后。
class="tags" href="/tags/Lei.html" title=类>类的成员变量">七、class="tags" href="/tags/Lei.html" title=类>类的成员变量
语法格式
[修饰符] class="tags" href="/tags/Lei.html" title=类>类型 成员变量名 [=默认值];
修饰符
public、protected、private、static、final
其中public、protected、private只能选择其一,可以与static、final组合起来修饰成员变量。
成员变量和局部变量
成员变量:实例变量(不以static修饰的变量)、class="tags" href="/tags/Lei.html" title=类>类变量(static修饰)
局部变量:形参、方法局部变量、代码块内局部变量
实际开发中尽量避免通过实例访问class="tags" href="/tags/Lei.html" title=类>类变量。
上机练习:使用代码开发电子宠物系统
- 系统中有狗狗和企鹅 //已完成
- 系统提供宠物选择
- 狗狗有品种选择、企鹅性别可选