ImageVerifierCode 换一换
格式:DOCX , 页数:74 ,大小:44.09KB ,
资源ID:16599324      下载积分:3 金币
快捷下载
登录下载
邮箱/手机:
温馨提示:
快捷下载时,用户名和密码都是您填写的邮箱或者手机号,方便查询和重复下载(系统自动生成)。 如填写123,账号就是123,密码也是123。
特别说明:
请自助下载,系统不会自动发送文件的哦; 如果您已付费,想二次下载,请登录后访问:我的下载记录
支付方式: 支付宝    微信支付   
验证码:   换一换

加入VIP,免费下载
 

温馨提示:由于个人手机设置不同,如果发现不能下载,请复制以下地址【https://www.bdocx.com/down/16599324.html】到电脑端继续下载(重复下载不扣费)。

已注册用户请登录:
账号:
密码:
验证码:   换一换
  忘记密码?
三方登录: 微信登录   QQ登录  

下载须知

1: 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。
2: 试题试卷类文档,如果标题没有明确说明有答案则都视为没有答案,请知晓。
3: 文件的所有权益归上传用户所有。
4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
5. 本站仅提供交流平台,并不能对任何下载内容负责。
6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

版权提示 | 免责声明

本文(ch03 面向对象程序设计基础Word格式文档下载.docx)为本站会员(b****4)主动上传,冰豆网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。 若此文所含内容侵犯了您的版权或隐私,请立即通知冰豆网(发送邮件至service@bdocx.com或直接QQ联系客服),我们立即给予删除!

ch03 面向对象程序设计基础Word格式文档下载.docx

1、五、类的概念(class)类是一组具有相同属性和行为的对象的抽象。第二节 类及对象的创建一、类的定义类是数据和函数的集合。类是一种类型。类是Java语言基本元素,类由成员变量和方法组成;类定义的一般形式如下:class 类名 变量(常量)定义构造方法定义方法定义其中访问权限为 public、private、protected或取默认值。 public class StudentTest public static void main(String args) Student studentA = new Student(); Student studentB = new Student();

2、studentA.setId(20001); studentA.setName(张三 studentA.setChinese(90); studentA.setMath(80); studentB.setId( studentB.setName( studentB.setChinese(90); studentB.setMath(85); System.out.println(studentA.Sum(); System.out.println(studentB.Sum();public class Student private String id; private String name;

3、 private int math; private int chinese; public void setId(String id) this.id = id; public void setName(String name) this.name = name; public void setMath(int math) this.math = math; public void setChinese(int chinese) this.chinese = chinese; public int Sum() return math + chinese;例题2: public String

4、getId() return id; public String getName() return name; public int getMath() return math; public int getChinese() return chinese; return getMath() + getChinese();20002 System.out.println(name:+studentA.getName()+tId:+studentA.getId()+tscore:+studentA.Sum();+studentB.getName()+studentB.getId()+tscore

5、+studentB.Sum();studentA1111li9080StudentB20002李四结合上例讲解什么是类、数据成员、方法二、上例的执行流程从主方法开始执行对上例的改进 public String getStudentInfo() return +getName()+getId()+Sum();* System.out.println(studentA.getStudentInfo();改进2: public String toString() System.out.println(studentA); System.out.println(studentB);三、创建对象对象是一

6、种复合数据。由类产生。1.定义对象定义对象的格式为: 类名 对象名如:Student st;2.建立对象建立对象使用NEW关键字。(1)为该对象分配内存空间;(2)返回该内存空间的引用,返回首地址;(引用是指指针的别名)(3)调用构造函数,构造函数自动将成员函数赋初值。st=new Student();st包括新建对象的“引用”-地址。注:可将上两步合二为一Student st=new Student();四、生成临时对象public class StudentTest public static void main(String args) new Student().setAge(Li c

7、lass Student private int age; void setName(String s) name=s; public String getName() return name; void setAge(int a) age=a; public int getAge() return age;五、成员的调用一般情况下,我们只能通过对象访问公有成员(public)保护成员(protected),不能通过对象访问私有成员(private)具体方法是:对象名.成员名 String name; int age;public class StudentTest2 /* Creates a

8、 new instance of StudentTest2 */ public static void main(String a) Student student=new Student(); student.name=li; student.age=30; System.out.println(student.name); System.out.println(student.age);六、 null 关键字 七、为什么要使用类和对象对象用来处理复合数据。但由于其复杂性不能一一定义,所以由类生成。当然还有封装、继承、多态。八、类什么时候被使用类用来生成对象和被继承九、类分为哪几种自定义 类

9、标准类库第三方类库十、简单方法的命名规则一般通过set设置数据成员(常量和变量)的值一般通过get获取数据成员(常量和变量)的值十一、面向对象的特性抽象、封装、继承、多态实例:public class Triangle double sideA, sideB, sideC; double area; boolean triangle; public void setSideA(double a) sideA = a; public void setSideB(double b) sideB = b; public void setSideC(double c) sideC = c; publi

10、c double getArea() double p = (sideA + sideB + sideC) / 2.0; area = Math.sqrt(p * (p - sideA) * (p - sideB) * (p - sideC); return area; public boolean isTriangle() if (sideA sideB + sideC & sideB sideA + sideC & sideC sideA + sideB) triangle = true; else triangle = false; return triangle; import jav

11、a.util.Scanner;public class TriangleTest Triangle triangle = new Triangle(); Scanner scanner = new Scanner(System.in);请输入三角形第一边 int a = scanner.nextInt();请输入三角形第二边 int b = scanner.nextInt();请输入三角形第三边 int c = scanner.nextInt(); triangle.setSideA(a); triangle.setSideB(b); triangle.setSideC(c); if (tri

12、angle.isTriangle() System.out.println(triangle.getArea();此三边不能构成三角形第二节 构造方法(Constructor)一、构造方法(Constructor) 1构造方法无返回值,前面也不加void 2、和类的其它方法一样,构造方法有名称、参数和方法体。 3、构造方法名与类同名。 4、使用new来创建对象时,系统会自动调用相应类的构造方法。构造方法的主要作用是:是在创建一个对象时,对这个对象进行初始化。例:1、无参数构造方法 public Student() id = 20074071123 this.name = zhang this.math =90; this.chinese = 100;说明:无参构造方法,相对用的比较少。2、有参数的构造方法 public Student(String id, String name, int math, int chinese) * Student studentA = new Student(20084071110,78,90);其他实例:教材实例 :/ EmpInfoC.javapublic class

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1