ch03 面向对象程序设计基础.docx
《ch03 面向对象程序设计基础.docx》由会员分享,可在线阅读,更多相关《ch03 面向对象程序设计基础.docx(74页珍藏版)》请在冰豆网上搜索。
ch03面向对象程序设计基础
第三章面向对象程序设计基础
士有百折不挠之真心,方有万变不穷之妙用
天下本无难事,心以为难,斯乃真难。
苟不存一难之见于心,则运用之术自出
第一节面向对象程序设计
一、面向过程的程序设计
1、作什么通过函数完成
2、如何做函数串在一起
二、面向对象的设计方法
1、找对人、物
2、分析行为、属性
3、如何做
三、对象(object)的讨论
对象是对现实世界的描述
对象有以下特性:
1、万物皆为对象
2、每个对象都是唯一的
3、对象具有属性和行为
4、对象都属于某个类
四、抽象的概念
抽象是指从特定角度出发,从已经存在的一些事物中,
抽取我们所关注特性形成一个新的事物的思维过程。
1.从问题领域的事物到软件模型中的对象的抽象
2.从对象到类的抽象
3.从子类到父类的抽象
五、类的概念(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("20001");
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:
publicclassStudent{
privateStringid;
privateStringname;
privateintmath;
privateintchinese;
publicStringgetId(){
returnid;
}
publicStringgetName(){
returnname;
}
publicintgetMath(){
returnmath;
}
publicintgetChinese(){
returnchinese;
}
publicvoidsetId(Stringid){
this.id=id;
}
publicvoidsetName(Stringname){
this.name=name;
}
publicvoidsetMath(intmath){
this.math=math;
}
publicvoidsetChinese(intchinese){
this.chinese=chinese;
}
publicintSum(){
returngetMath()+getChinese();
}
}
publicclassStudentTest{
publicstaticvoidmain(Stringargs[]){
StudentstudentA=newStudent();
StudentstudentB=newStudent();
studentA.setId("20001");
studentA.setName("张三");
studentA.setChinese(90);
studentA.setMath(80);
studentB.setId("20002");
studentB.setName("张三");
studentB.setChinese(90);
studentB.setMath(85);
System.out.println("name:
"+studentA.getName()+"\tId:
"+studentA.getId()+"\tscore:
"+studentA.Sum());
System.out.println("name:
"+studentB.getName()+"\tId:
"+studentB.getId()+"\tscore"+studentB.Sum());
studentA
1111
li
90
80
Stu
dentB
20002
李四
90
80
结合上例讲解什么是类、数据成员、方法
二、上例的执行流程
从主方法开始执行
对上例的改进
publicclassStudent{
privateStringid;
privateStringname;
privateintmath;
privateintchinese;
publicStringgetId(){
returnid;
}
publicStringgetName(){
returnname;
}
publicintgetMath(){
returnmath;
}
publicintgetChinese(){
returnchinese;
}
publicvoidsetId(Stringid){
this.id=id;
}
publicvoidsetName(Stringname){
this.name=name;
}
publicvoidsetMath(intmath){
this.math=math;
}
publicvoidsetChinese(intchinese){
this.chinese=chinese;
}
publicintSum(){
returngetMath()+getChinese();
}
publicStringgetStudentInfo(){
return"name:
"+getName()+"\tId:
"+getId()+"\tscore:
"+Sum();
}
}
******
publicclassStudentTest{
publicstaticvoidmain(Stringargs[]){
StudentstudentA=newStudent();
StudentstudentB=newStudent();
studentA.setId("20001");
studentA.setName("张三");
studentA.setChinese(90);
studentA.setMath(80);
studentB.setId("20002");
studentB.setName("张三");
studentB.setChinese(90);
studentB.setMath(85);
System.out.println(studentA.getStudentInfo());
System.out.println(studentA.getStudentInfo());
}
}
改进2:
publicclassStudent{
privateStringid;
privateStringname;
privateintmath;
privateintchinese;
publicStringgetId(){
returnid;
}
publicStringgetName(){
returnname;
}
publicintgetMath(){
returnmath;
}
publicintgetChinese(){
returnchinese;
}
publicvoidsetId(Stringid){
this.id=id;
}
publicvoidsetName(Stringname){
this.name=name;
}
publicvoidsetMath(intmath){
this.math=math;
}
publicvoidsetChinese(intchinese){
this.chinese=chinese;
}
publicintSum(){
returngetMath()+getChinese();
}
publicStringtoString(){
return"name:
"+getName()+"\tId:
"+getId()+"\tscore:
"+Sum();
}
}
publicclassStudentTest{
publicstaticvoidmain(Stringargs[]){
StudentstudentA=newStudent();
StudentstudentB=newStudent();
studentA.setId("20001");
studentA.setName("张三");
studentA.setChinese(90);
studentA.setMath(80);
studentB.setId("20002");
studentB.setName("张三");
studentB.setChinese(90);
studentB.setMath(85);
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
{
privateStringname;
privateintage;
voidsetName(Strings)
{
name=s;
}
publicStringgetName()
{
returnname;
}
voidsetAge(inta)
{
age=a;
}
publicintgetAge()
{
returnage;
}
}
五、成员的调用
一般情况下,我们只能通过对象访问公有成员(public)保护成员(protected),不能通过对象访问私有成员(private)
具体方法是:
对象名.成员名
classStudent
{
Stringname;
intage;
voidsetName(Strings)
{
name=s;
}
publicStringgetName()
{
returnname;
}
voidsetAge(inta)
{
age=a;
}
publicintgetAge()
{
returnage;
}
}
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(sideAtriangle=true;
}else{
triangle=false;
}
returntriangle;
}
}
importjava.util.Scanner;
publicclassTriangleTest{
publicstaticvoidmain(Stringargs[]){
Triangletriangle=newTriangle();
Scannerscanner=newScanner(System.in);
System.out.println("请输入三角形第一边");
inta=scanner.nextInt();
System.out.println("请输入三角形第二边");
intb=scanner.nextInt();
System.out.println("请输入三角形第三边");
intc=scanner.nextInt();
triangle.setSideA(a);
triangle.setSideB(b);
triangle.setSideC(c);
if(triangle.isTriangle()){
System.out.println(triangle.getArea());
}else{
System.out.println("此三边不能构成三角形");
}
}
}
第二节构造方法(Constructor)
一、构造方法(Constructor)
1.构造方法无返回值,前面也不加void
2、和类的其它方法一样,构造方法有名称、参数和方法体。
3、构造方法名与类同名。
4、使用new来创建对象时,系统会自动调用相应类的构造方法。
构造方法的主要作用是:
是在创建一个对象时,对这个对象进行初始化。
例:
1、无参数构造方法
publicclassStudent{
privateStringid;
privateStringname;
privateintmath;
privateintchinese;
publicStringgetId(){
returnid;
}
publicStudent(){
id="20074071123";
this.name="zhang";
this.math=90;
this.chinese=100;
}
publicStringgetName(){
returnname;
}
publicintgetMath(){
returnmath;
}
publicintgetChinese(){
returnchinese;
}
publicvoidsetId(Stringid){
this.id=id;
}
publicvoidsetName(Stringname){
this.name=name;
}
publicvoidsetMath(intmath){
this.math=math;
}
publicvoidsetChinese(intchinese){
this.chinese=chinese;
}
publicintSum(){
returngetMath()+getChinese();
}
publicStringtoString(){
return"name:
"+getName()+"\tId:
"+getId()+"\tscore:
"+Sum();
}
}
publicclassStudentTest{
publicstaticvoidmain(Stringargs[]){
StudentstudentA=newStudent();
System.out.println(studentA);
}
}
说明:
无参构造方法,相对用的比较少。
2、有参数的构造方法
publicclassStudent{
privateStringid;
privateStringname;
privateintmath;
privateintchinese;
publicStringgetId(){
returnid;
}
publicStudent(Stringid,Stringname,intmath,intchinese){
this.id=id;
this.name=name;
this.math=math;
this.chinese=chinese;
}
publicStringgetName(){
returnname;
}
publicintgetMath(){
returnmath;
}
publicintgetChinese(){
returnchinese;
}
publicvoidsetId(Stringid){
this.id=id;
}
publicvoidsetName(Stringname){
this.name=name;
}
publicvoidsetMath(intmath){
this.math=math;
}
publicvoidsetChinese(intchinese){
this.chinese=chinese;
}
publicintSum(){
returngetMath()+getChinese();
}
publicStringtoString(){
return"name:
"+getName()+"\tId:
"+getId()+"\tscore:
"+Sum();
}
}
****
publicclassStudentTest{
publicstaticvoidmain(Stringargs[]){
StudentstudentA=newStudent("20084071110","张三",78,90);
System.out.println(studentA);
}
}
其他实例:
教材实例:
//EmpInfoC.java
publicclass