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

上传人:b****4 文档编号:16599324 上传时间:2022-11-24 格式:DOCX 页数:74 大小:44.09KB
下载 相关 举报
ch03 面向对象程序设计基础Word格式文档下载.docx_第1页
第1页 / 共74页
ch03 面向对象程序设计基础Word格式文档下载.docx_第2页
第2页 / 共74页
ch03 面向对象程序设计基础Word格式文档下载.docx_第3页
第3页 / 共74页
ch03 面向对象程序设计基础Word格式文档下载.docx_第4页
第4页 / 共74页
ch03 面向对象程序设计基础Word格式文档下载.docx_第5页
第5页 / 共74页
点击查看更多>>
下载资源
资源描述

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

《ch03 面向对象程序设计基础Word格式文档下载.docx》由会员分享,可在线阅读,更多相关《ch03 面向对象程序设计基础Word格式文档下载.docx(74页珍藏版)》请在冰豆网上搜索。

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

五、类的概念(class)

类是一组具有相同属性和行为的对象的抽象。

第二节类及对象的创建

一、类的定义

类是数据和函数的集合。

类是一种类型。

类是Java语言基本元素,类由成员变量和方法组成;

类定义的一般形式如下:

class类名{

变量(常量)定义

构造方法定义

方法定义

 

}

其中访问权限为public、private、protected或取默认值。

publicclassStudentTest{

publicstaticvoidmain(Stringargs[]){

StudentstudentA=newStudent();

StudentstudentB=newStudent();

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());

publicclassStudent{

privateStringid;

privateStringname;

privateintmath;

privateintchinese;

publicvoidsetId(Stringid){

this.id=id;

}

publicvoidsetName(Stringname){

this.name=name;

publicvoidsetMath(intmath){

this.math=math;

publicvoidsetChinese(intchinese){

this.chinese=chinese;

publicintSum(){

returnmath+chinese;

例题2:

publicStringgetId(){

returnid;

publicStringgetName(){

returnname;

publicintgetMath(){

returnmath;

publicintgetChinese(){

returnchinese;

returngetMath()+getChinese();

20002"

System.out.println("

name:

"

+studentA.getName()+"

\tId:

+studentA.getId()+"

\tscore:

+studentA.Sum());

+studentB.getName()+"

+studentB.getId()+"

\tscore"

+studentB.Sum());

studentA

1111

li

90

80

Stu

dentB

20002

李四

结合上例讲解什么是类、数据成员、方法

二、上例的执行流程

从主方法开始执行

对上例的改进

publicStringgetStudentInfo(){

return"

+getName()+"

+getId()+"

+Sum();

******

System.out.println(studentA.getStudentInfo());

改进2:

publicStringtoString(){

System.out.println(studentA);

System.out.println(studentB);

三、创建对象

对象是一种复合数据。

由类产生。

1.定义对象

定义对象的格式为:

类名对象名

如:

Studentst;

2.建立对象

建立对象使用NEW 

关键字。

(1)为该对象分配内存空间;

(2)返回该内存空间的引用,返回首地址;

(引用是指指针的别名)

(3)调用构造函数,构造函数自动将成员函数赋初值。

st=newStudent();

st包括新建对象的“引用”---地址。

注:

可将上两步合二为一

Studentst=newStudent();

四、生成临时对象

publicclassStudentTest

{

publicstaticvoidmain(Stringargs[])

{

newStudent().setAge("

Li"

}

classStudent

privateintage;

voidsetName(Strings)

name=s;

publicStringgetName()

returnname;

voidsetAge(inta)

age=a;

publicintgetAge()

returnage;

五、成员的调用

一般情况下,我们只能通过对象访问公有成员(public)保护成员(protected),不能通过对象访问私有成员(private)

具体方法是:

对象名.成员名

Stringname;

intage;

publicclassStudentTest2{

/**CreatesanewinstanceofStudentTest2*/

publicstaticvoidmain(Stringa[]){

Studentstudent=newStudent();

student.name="

li"

;

student.age=30;

System.out.println(student.name);

System.out.println(student.age);

六、null关键字

七、为什么要使用类和对象

对象用来处理复合数据。

但由于其复杂性不能一一定义,所以由类生成。

当然还有封装、继承、多态。

八、类什么时候被使用

类用来生成对象和被继承

九、类分为哪几种

自定义类

标准类库

第三方类库

十、简单方法的命名规则

一般通过set设置数据成员(常量和变量)的值

一般通过get获取数据成员(常量和变量)的值

十一、面向对象的特性

抽象、封装、继承、多态

实例:

publicclassTriangle{

doublesideA,sideB,sideC;

doublearea;

booleantriangle;

publicvoidsetSideA(doublea){

sideA=a;

publicvoidsetSideB(doubleb){

sideB=b;

publicvoidsetSideC(doublec){

sideC=c;

publicdoublegetArea(){

doublep=(sideA+sideB+sideC)/2.0;

area=Math.sqrt(p*(p-sideA)*(p-sideB)*(p-sideC));

returnarea;

publicbooleanisTriangle(){

if(sideA<

sideB+sideC&

&

sideB<

sideA+sideC&

sideC<

sideA+sideB){

triangle=true;

}else{

triangle=false;

returntriangle;

importjava.util.Scanner;

publicclassTriangleTest{

Triangletriangle=newTriangle();

Scannerscanner=newScanner(System.in);

请输入三角形第一边"

inta=scanner.nextInt();

请输入三角形第二边"

intb=scanner.nextInt();

请输入三角形第三边"

intc=scanner.nextInt();

triangle.setSideA(a);

triangle.setSideB(b);

triangle.setSideC(c);

if(triangle.isTriangle()){

System.out.println(triangle.getArea());

此三边不能构成三角形"

第二节构造方法(Constructor)

一、构造方法(Constructor)

1.构造方法无返回值,前面也不加void

2、和类的其它方法一样,构造方法有名称、参数和方法体。

3、构造方法名与类同名。

4、使用new来创建对象时,系统会自动调用相应类的构造方法。

构造方法的主要作用是:

是在创建一个对象时,对这个对象进行初始化。

例:

1、无参数构造方法

publicStudent(){

id="

20074071123"

this.name="

zhang"

this.math=90;

this.chinese=100;

说明:

无参构造方法,相对用的比较少。

2、有参数的构造方法

publicStudent(Stringid,Stringname,intmath,intchinese){

****

StudentstudentA=newStudent("

20084071110"

"

78,90);

其他实例:

教材实例:

//EmpInfoC.java

publicclass

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 农林牧渔 > 水产渔业

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

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