第7章圆和矩形——类与对象讲述.ppt

  1. 1、本文档共68页,可阅读全部内容。
  2. 2、有哪些信誉好的足球投注网站(book118)网站文档一经付费(服务费),不意味着购买了该文档的版权,仅供个人/单位学习、研究之用,不得用于商业用途,未经授权,严禁复制、发行、汇编、翻译或者网络传播等,侵权必究。
  3. 3、本站所有内容均由合作方或网友上传,本站不对文档的完整性、权威性及其观点立场正确性做任何保证或承诺!文档内容仅供研究参考,付费前请自行鉴别。如您付费,意味着您自己接受本站规则且自行承担风险,本站不退款、不进行额外附加服务;查看《如何避免下载的几个坑》。如果您已付费下载过本站文档,您可以点击 这里二次下载
  4. 4、如文档侵犯商业秘密、侵犯著作权、侵犯人身权等,请点击“版权申诉”(推荐),也可以打举报电话:400-050-0827(电话支持时间:9:00-18:30)。
查看更多
问题 A: 设计Rectangle类 * 新建Rectangle类 * * 谢谢! 返回目录 结束放映 * 构造方法的多样性 根据需求定义不同的构造方法 Date myDate = new Date(); Date yourDate = new Date(13, 4, 2016); Date date3 = new Date(13); Date date4 = new Date(13, 4); Date date2 = new Date(yourDate); 构造方法的多样性 根据需求定义不同的构造方法 class Date { ... ... public Date(int a, int b) { day = a; month = b; year = 2016; } public Date(int a) { day = a; month = 4; year =2016; } public Date(Date d) { day = d.day; month = d.month; year = d.year; } } 【例7-2】在例7-1基础上,构建若干个圆对象,并计算圆的面积和周长。 …try{ Circle circ = new Circle(3.5); System.out.printf(构建了半径为%.2f的圆,圆面积%.2f、周长%.2f\n, circ.getRadius(), circ.calcArea(), circ.calcGirth()); System.out.printf(目前圆对象个数为 %d\n,Circle.getNum()); Circle circ2 = new Circle(10); … Circle circ3 = new Circle(); System.out.printf(构建了半径为%.2f的圆\n, circ3.getRadius()); circ3.setRadius(1); System.out.printf(圆半径更改为%.2f,这时圆面积%.2f、周长%.2f\n, circ3.getRadius(), circ3.calcArea(), circ3.calcGirth()); … } catch(Exception e){ System.out.println(异常: + e); } finally { System.out.print(——程序结束。); } * 简易学生类例子 定义一个学生类,包含两个属性:学号、成绩。 定义3个构造方法,分别以学号、学号和成绩,以及学生对象为参数。 实例化3个学生对象,分别调用三个不同的构造方法。 * class Student { int number; int score; public Student(int num) { number = num; } public Student( Student s ) { number = s.number; score = s.score; } public Student(int num, int sco) { number = num; score = sco; } } Student s1 = new Student(1); Student s2 = new Student(2, 534); Student s3 = new Student(s2); 类和对象 类的定义 类的构造 构造方法没有返回值 默认构造方法、自定义构造方法 构造方法的多样性:根据需求定义不同的构造方法 类的实例化,即对象的创建 * 类和对象 类的定义 类的构造 类的实例化,即对象的创建 对象的使用 类的定义 类的构造 类的实例化,即对象的创建 对象的使用 * 对象创建的过程 例如:Dog myDog = new Dog(); Dog myDog = new Dog(); 创建一个Dog的引用 Dog myDog = new Dog(); 创建一个Dog的对象 Dog myDog = new Dog(); 将创建的Dog对象赋值给myDog引用 Dog myDog * 每个对象都是独立的 在内存中存储的位置是不一样的 myDate … yourDate … myDate2 … * 对象的使用方法 用对象引用的“.”运算符 对象.变量 对象.方法() * 访问对象的属性和方法 “new”来创建对象 用对象的“.”运算符访问其属性

文档评论(0)

6952225 + 关注
实名认证
内容提供者

该用户很懒,什么也没介绍

1亿VIP精品文档

相关文档