1 / 17

第五讲 面向对象程序设计

第五讲 面向对象程序设计. 教学目的. 理解类、对象、属性、事件、方法、继承、重载等概念 掌握定义类的规则和使用 掌握实例化对象的规则和使用 掌握类和对象的属性的引用 掌握方法的定义和引用 掌握定义子类和继承的规则 掌握方法的重载规则和使用 理解面向过程的方法是面向对象的方法的基础 理解面向对象基本概念体系. 面向对象方法的基本概念. 面向对象的思想 面向对象的分析( OOA ) 面向对象的设计( OOD ) 面向对象的编程( OOP ) 对象:单个事物 类:一批对象的相同特点的抽象定义 属性:类和对象的特征 方法:类和对象的操作(功能或能力)

misae
Download Presentation

第五讲 面向对象程序设计

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 第五讲 面向对象程序设计

  2. 教学目的 • 理解类、对象、属性、事件、方法、继承、重载等概念 • 掌握定义类的规则和使用 • 掌握实例化对象的规则和使用 • 掌握类和对象的属性的引用 • 掌握方法的定义和引用 • 掌握定义子类和继承的规则 • 掌握方法的重载规则和使用 • 理解面向过程的方法是面向对象的方法的基础 • 理解面向对象基本概念体系

  3. 面向对象方法的基本概念 • 面向对象的思想 • 面向对象的分析(OOA) • 面向对象的设计(OOD) • 面向对象的编程(OOP) • 对象:单个事物 • 类:一批对象的相同特点的抽象定义 • 属性:类和对象的特征 • 方法:类和对象的操作(功能或能力) • 事件:状态的变化、操作的发生、时刻的到来等

  4. 定义类 class 类名 { 类体 }

  5. 定义类 class 类名 { 变量 方法 }

  6. 定义类实例1 类声明:类Apple 成员变量:字符型、浮点型、整型变量color、price、num public class Apple{ String color; float price; int num; public Apple(){ color=“Rea”; price=8.34f; num=10; } public float count(){ return price*num; } public static void main(String[] arges){ Apple apple=new Apple(); System.out.println(“苹果颜色:”+apple.color); System.out.println(“苹果总价格:”+apple.count()); } } 构造方法:和类同名,没有类型,用于初始化对象 方法:返回值为float类型的方法,可以被引用,引用时不需要参数,非静态方法,不能通过类直接引用 main方法:程序的入口,静态方法,可以通过类直接引用,没有返回值 用构造方法初始化对象 创建对象 引用对象的变量 引用对象的方法

  7. 定义类实例1 • 定义了一个public类 • 定义了3个非static成员变量 • 定义了一个构造方法(可以定义多个构造方法) • 定义了一个非static的成员方法(count)(可以定义多个成员方法) • 定义了一个public的、static的、main方法(只能有一个main方法,这是程序执行的入口位置)

  8. 定义类实例2 class Apple_01{ String color;float price;int num; // 成员变量三个 Apple_01(int n){color=“Rea”; price=8.34f; num=10;} // 构造方法一 Apple_01(int n,int m){color="Rea红"; price=18.34f; num=50;} // 构造方法二 float count(int l){return price*num;} // 同名成员方法1(非静态方法) float count(int l,int j){return price+num;}// 同名成员方法2(非静态方法)(重载方法) public static void main(String[] arges){ // main方法(静态方法) Apple_01 apple_01=new Apple_01(3); // 用构造方法一初始化对象 System.out.println("苹果颜色:"+apple_01.color); System.out.println("苹果总价格:"+apple_01.count(3)); Apple_01 apple_01_01=new Apple_01(3,3); // 用构造方法二初始化对象 System.out.println("苹果颜色:"+apple_01_01.color); System.out.println("苹果总价格:"+apple_01_01.count(3)); System.out.println("苹果颜色:"+apple_01.color); System.out.println("苹果总价格:"+apple_01.count(3,3)); System.out.println("苹果颜色:"+apple_01_01.color); System.out.println("苹果总价格:"+apple_01_01.count(3,3));}}

  9. 类、对象、方法定义及应用 import java.util.Scanner; class A0302 {public static void abc() { int a=0;int b=0;int c=0; System.out.println("请输入三个整数,数与数之间用空格分隔。"); Scanner scanner=new Scanner(System.in); a=scanner.nextInt();b=scanner.nextInt();c=scanner.nextInt(); if(a>=b) if(b>=c)System.out.println(a+" "+b+" "+c); else if(a>=c) System.out.println(a+" "+c+" "+b); else System.out.println(c+" "+a+" "+b); else if(a>=c)System.out.println(b+" "+a+" "+c); else if(b>=c) System.out.println(b+" "+c+" "+a); else System.out.println(c+" "+b+" "+a);}}

  10. 类、对象、方法定义及应用 class A0303 { void px(int n) { int s=0; for(int i=1;i<=n;i++) s=s+i; System.out.println(s); } }

  11. 类、对象、方法定义及应用 class A0304 { void lj(int n) { int a[]=new int[n]; int s=0; for(int i=0;i<=n-1;i++) a[i]=i+1; for(int i=0;i<=n-1;i++) s=s+a[i]; System.out.println(s); } }

  12. 类、对象、方法定义及应用 import java.util.Scanner; class A0305{ static void pxx(int n){ int k; int a[]=new int[n]; Scanner scanner=new Scanner(System.in); for(int i=0;i<=n-1;i++) a[i]=scanner.nextInt(); for(int i=0;i<=n-2;i++) for(int j=i+1;j<=n-1;j++) if(a[i]<a[j]) {k=a[i];a[i]=a[j];a[j]=k;} for(int i=0;i<=n-1;i++) System.out.println(a[i]);}}

  13. 类、对象、方法定义及应用 import java.util.Scanner; class A0306{ static void pxx(int n){ int k;int a[]=new int[n]; Scanner scanner=new Scanner(System.in); for(int i=0;i<=n-1;i++)a[i]=scanner.nextInt(); for(int i=0;i<=n-2;i++) for(int j=i+1;j<=n-1;j++)if(a[i]<a[j]){k=a[i];a[i]=a[j];a[j]=k;} for(int i=0;i<=n-1;i++)System.out.println(a[i]);} static void pxx(int n,int m){ int k;int a[]=new int[n]; Scanner scanner=new Scanner(System.in); for(int i=0;i<=n-1;i++)a[i]=scanner.nextInt(); for(int i=0;i<=n-2;i++) for(int j=i+1;j<=n-1;j++)if(a[i]>a[j]){k=a[i];a[i]=a[j];a[j]=k;} for(int i=0;i<=n-1;i++)System.out.println(a[i]);}}

  14. 类、对象、方法定义及应用 import java.util.Scanner; class A030501 extends A0305{ static void pxxx(int n){ int k; int a[]=new int[n]; Scanner scanner=new Scanner(System.in); for(int i=0;i<=n-1;i++) a[i]=scanner.nextInt(); for(int i=n-1;i>=0;i--) System.out.println(a[i]);}}

  15. 类、对象、方法定义及应用 class A0301{ public static void main(String[] arges){ A0302.abc(); A0302 a0302=new A0302();a0302.abc(); A0303 a0303=new A0303();a0303.px(50); A0304 a0304=new A0304();a0304.lj(50); A0305.pxx(10); A0305 a0305=new A0305();a0305.pxx(5); A0306.pxx(5,20); A0306 a0306=new A0306();a0306.pxx(5,20); A030501.pxxx(5); A030501.pxx(6);}}

  16. 本讲小结 • 如果,你觉得你写的这个程序太过复杂,那一定是这个程序中间包含太多的问题的解。怎么办呢?定义一个个的类,在类中定义一个个的方法。再通过引用特定类的特定方法,或通过实例化对象后,再应用对象的方法来解决特定的问题。那么,每一个类都会显得非常简单。不仅如此,这样还有利于程序的开发、测试、管理、维护、升级等等工作。这就是面向对象方法的思想基础。 • 今天,面向对象的思想和方法,已经是当今世界程序设计的主流思想和方法。学习程序设计的人,学习和掌握面向对象的方法是非常有必要的。 • 但是,每一个学习程序设计的人都不要忘了,面向过程的思想和方法,不仅是面向对象方法的思想基础,也是技术基础。任何人都不要指望可以在没有面向过程程序设计基础的前提下,直接进行面向对象程序设计方法的学习和训练,并能取得良好效果。

  17. 作业 • 阅读教材指定内容 • 编写3个解决指定问题的类及方法,并引用之。 • 运行编写的程序

More Related