Java程序设计项目教程 教学课件 ppt 作者 郭庚麒 周江_ 05章 类和对象

上传人:E**** 文档编号:89542112 上传时间:2019-05-27 格式:PPTX 页数:26 大小:175.77KB
返回 下载 相关 举报
Java程序设计项目教程 教学课件 ppt 作者 郭庚麒 周江_ 05章 类和对象_第1页
第1页 / 共26页
Java程序设计项目教程 教学课件 ppt 作者 郭庚麒 周江_ 05章 类和对象_第2页
第2页 / 共26页
Java程序设计项目教程 教学课件 ppt 作者 郭庚麒 周江_ 05章 类和对象_第3页
第3页 / 共26页
Java程序设计项目教程 教学课件 ppt 作者 郭庚麒 周江_ 05章 类和对象_第4页
第4页 / 共26页
Java程序设计项目教程 教学课件 ppt 作者 郭庚麒 周江_ 05章 类和对象_第5页
第5页 / 共26页
点击查看更多>>
资源描述

《Java程序设计项目教程 教学课件 ppt 作者 郭庚麒 周江_ 05章 类和对象》由会员分享,可在线阅读,更多相关《Java程序设计项目教程 教学课件 ppt 作者 郭庚麒 周江_ 05章 类和对象(26页珍藏版)》请在金锄头文库上搜索。

1、,第5章 类和对象,【学习目标】 Java是一种面向对象程序设计语言,类和对象是学习“面向对象编程”的基础,读者通过本章学习,应该达到以下目标: 初步理解面向对象的概念; 通过private关键字、static关键字了解java语言的封装性; 熟悉类的定义及该类的对象的生成; 使用某个对象的方法或变量以及类方法和类变量 熟悉数组与对象的结合等扩展应用。,5.1面向对象的概念,5.1.1类的定义 修饰符 class 类名extends父类名implements 接口 修饰符 类型 成员变量1; 修饰符 类型 成员变量2; 修饰符 类型 成员变量n; 修饰符 类型 成员方法1 方法体1 修饰符 类

2、型 成员方法2 方法体2 修饰符 类型 成员方法n 方法体n ,【任务5-1】定义一个学生类Student 。,public class Student String name; int No; String dorm; String tel; void checkIn(String a,int b) name=a; No=b; void assignDorm(String a) dorm=a; void provideTel(String b) tel=b; ,5.1.2对象的生命周期,1.对象的生成 2.对象的使用 3对象的清除,5.1.3由类的定义产生对象,【任务5-2】使用Studen

3、t类,public class TermBegins public static void main(String args) Student zhang; zhang=new Student(); zhang.checkIn(“张三“, 001); zhang.assignDorm(“A-101“); zhang.provideTel(“13012345678“); System.out.println(“姓名:“+zhang.name); System.out.println(“学号:“+zhang.No); System.out.println(“宿舍:“+zhang.dorm); Sy

4、stem.out.println(“联系电话:“+zhang.tel); ,姓名:张三 学号:1 宿舍:A-101 联系电话:13012345678,5.1.4类的构造函数,【任务5-3】为Student类定义构造方法。,public class Student String name; int No; String dorm; String tel; Student(String aName,int aNo) name=aName; No=aNo; void assignDorm(String a) dorm=a; void provideTel(String b) tel=b; publi

5、c static void main(String args) Student zhang=new Student(“张三“,001); zhang.assignDorm(“A-101“); zhang.provideTel(“13012345678“); ,【任务5-4】为Student类定义多个构造方法。,源代码在备注中,5.2 封装性,封装的定义包括下面几个方面: (1)一个清晰的边界,所有对象的内部软件范围限定在这个边界之内。 (2)一个接口,该接口描述当前对象和其他对象之间的交互作用。 (3)内部实现,对象内部的实现是受保护的,这个实现给出了软件对象功能的细节,定义当前对象的类的外面

6、不能访问这些实现细节。,5.2.1类变量和类方法,【任务5-5】用static变量统计Student类对象个数。,/static变量和方法的使用 public class Student String name; int No; String dorm; String tel; static int count=0; Student(String aName,int aNo) name=aName; No=aNo; count+; Student(int aNo) No=aNo; Student(String aName) name=aName; static void printCount(

7、) System.out.println(“已报到人数:“+count); public static void main(String args) Student.printCount(); Student zhang=new Student(“张三“,001); Student li=new Student(“李四“,003); System.out.println(Student.count); ,已报到人数:0 已报到人数:2,5.2.2 使用private将变量封装起来,【任务5-6】private修饰符让Student类的数据更安全。,/设置private变量 public cla

8、ss Student String name; int No; String dorm; String tel; private static int count=0; Student(String aName,int aNo) name=aName; No=aNo; count+; static void printCount() System.out.println(“总人数:“+ count); ,public class useStudent public static void main(String args) Student zhang=new Student(“张三“,001)

9、; Student li=new Student(“李四“,003); Student.printCount();/利用函数访问count变量 /System.out.println(Student.count); /上句有语法错误,Student私有变量count不能被访问 ,5.3扩展应用,5.3.1数组与对象 【任务5-7】用数组处理多个学生对象。,public class Student String name; int no ; int score; void printInfo() System.out.println(name+“t“+no+“t“+score); ,public

10、 class useStudent public static void main(String args) Student netClass=new Student10; int score=90,67,78,90,65,45,67,89,76,67,; String name=“susan“,“tom“,“jerry“,“jack“,“rose“, “maggie“,“elisha“,“Dick “,“Harry“,“John“; for (int i=0;i=9;i+) netClassi=new Student();/为每个数组元素分配空间 netClassi.no=i+1; netC

11、lassi.name=namei; netClassi.score=scorei; System.out.println(“name“+“t“+“no“+“t“+“score“); for (int i=0;i=9;i+) netClassi.printInfo(); ,name no score susan 1 90 tom 2 67 jerry 3 78 jack 4 90 rose 5 65 maggie 6 45 elisha 7 67 Dick 8 89 Harry 9 76 John 10 67,5.3.2 对象作为方法的参数和返回值,【任务5-8】输入和返回参数为学生类对象。,p

12、ublic class Student String name; int no ; double score1;/成绩1 double score2;/成绩2 int sustainers;/支持者 public Student(String name, int no, double score1, double score2, int sustainers) super(); this.name = name; this.no = no; this.score1 = score1; this.score2 = score2; this.sustainers = sustainers; ,/比

13、较两个学生的信息,输出综合指数高的同学作为班长 public class useStudentPk static Student pk(Student a,Student b) double aEx=a.score1*0.2+a.score2*0.3+a.sustainers*0.5; double bEx=b.score1*0.2+b.score2*0.3+b.sustainers*0.5; if (aExbEx) return a; else return b; public static void main(String args) Student zhang=new Student(“

14、zhangsan“,001,90,90,15); Student li=new Student(“lisi“,002,89,90,20); Student wang=new Student(“wangwu“,007,98,92,10); Student zhao=new Student(“zhaoliu“,012,89,70,25); Student tmp=pk(zhang,li); Student monitor=pk(tmp,wang); monitor=pk(tmp,zhao); System.out.print(“取胜者为:“+monitor.name); ,取胜者为:lisi,5.

15、3.3 对象作为类的成员,public class Communication double mobliePhone;/移动电话 String eMail;/电子邮件 double qq;/qq号码 String telephone;/宿舍电话 public Communication(double mobliePhone, String mail, double qq, String telephone) super(); this.mobliePhone = mobliePhone; this.eMail = mail; this.qq = qq; this.telephone = telephone; ,public class Student String name; int no; Communication com1; public Student(String name, int no, Communication com1) super(); this.name = name; this.no = no; 1 = com1; ,public class TestStudent public static void main(String args) Communic

展开阅读全文
相关资源
相关搜索

当前位置:首页 > 高等教育 > 大学课件

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号