java教程3java面向对象编程.ppt

上传人:wuy****n92 文档编号:80475940 上传时间:2023-03-23 格式:PPT 页数:95 大小:1.29MB
返回 下载 相关 举报
java教程3java面向对象编程.ppt_第1页
第1页 / 共95页
java教程3java面向对象编程.ppt_第2页
第2页 / 共95页
点击查看更多>>
资源描述

《java教程3java面向对象编程.ppt》由会员分享,可在线阅读,更多相关《java教程3java面向对象编程.ppt(95页珍藏版)》请在得力文库 - 分享文档赚钱的网站上搜索。

1、第第3 3章章JavaJava面向对象编程面向对象编程Sunny Liu本课教学内容w定义类w创建对象w成员数据和方法w给方法传参数w构造函数w访问说明符本课教学内容w修饰符w重载w包w继承的概念w在Java中实现继承本课教学内容w接口w方法覆盖w多态性w使用super问题陈述 w顾客介绍过程中的Customer类有如下的属性和行为:w创建一个顾客类Customer。该类存储顾客的个人信息:顾客ID号、顾客名、电话、邮编。为顾客类编写一个方法,该方法能显示顾客的个人信息。Customer CustomerIdCustomerNameCustomerPhoneString CustomerPos

2、tcode displayDetails()定义类w语法 class/body of class其中,class 是创建类所使用的关键字,是类的名称,包含属性和方法定义类w类的命名规则:不能为不能为 Java 中的关键字。中的关键字。不能包含空格或点号不能包含空格或点号“.”。可以以下划线可以以下划线“_”、字母或、字母或“$”符号开头。符号开头。class Box/成员数据/方法创建对象w创建一个对象分两步进行创建一个对象分两步进行:声明对象的引用变量或名称。声明对象的引用变量或名称。创建对象的一个实例。创建对象的一个实例。w语法语法 myboxnull创建对象w例子例子Box mybox;

3、在使用对象之前必须给它们分配内存。由new操作符来完成。mybox=new Box();Boxmybox成员数据w定义一个成员数据的语法为:定义一个成员数据的语法为:w例子例子public class Box private double length;private double width;private double depth;方法w语法:语法:(参数列表)/语句w例子例子 public double volume()return width*height*depth;返回类型返回类型方法名方法名参数列表参数列表访问说明符访问说明符方法首方法首部声明部声明方法体方法体使用成员数据和方法

4、w使用成员数据使用成员数据成员数据能用在不同的类中,通过创建类的对象然成员数据能用在不同的类中,通过创建类的对象然后用点后用点”.”.”引用对象的成员数据。引用对象的成员数据。w调用方法调用方法调用方法,必须是方法名后跟括弧和分号。调用方法,必须是方法名后跟括弧和分号。如果两方法在同一个类里面,可以直接使用该方法如果两方法在同一个类里面,可以直接使用该方法的名字进行调用。类的一个方法能调用相同类里的的名字进行调用。类的一个方法能调用相同类里的另一个方法。另一个方法。如果两方法不在同一个类中,一个方法通过创建类如果两方法不在同一个类中,一个方法通过创建类的对象然后用的对象然后用”.”操作符指向那

5、方法,从而调用操作符指向那方法,从而调用不同类里的方法。不同类里的方法。示例:使用成员数据和方法class Box double width;double height;double depth;double volume()return width*height*depth;public class BoxDemo public static void main(String args)Box mybox=new Box();double vol;/给盒子的实例变量赋值mybox.width=10;mybox.height=20;mybox.depth=15;/返回盒子的体积 vol=myb

6、ox.volume();System.out.println(Volume is +vol);圆点操作符实例分析1说明类2说明类的变量3说明类的方法4初始化变量5编写代码显示测试值6编写main()方法的代码7调用方法8编译运行程序任务单任务单实例分析w步骤步骤1:说明类:说明类 public class Customer实例分析w步骤步骤2:定义类的变量:定义类的变量 public class Customer public String CustomerId;public String CustomerName;public String CustomerPhone;public Stri

7、ng CustomerPostcode;实例分析w步骤步骤3:说明类中的方法:说明类中的方法 public class Customer public String CustomerId;public String CustomerName;public String CustomerPhone;public String CustomerPostcode;public void displayDetails()/写入显示顾客的信息的代码写入显示顾客的信息的代码实例分析w步骤步骤4:初始化变量:初始化变量 public class Customer public String Customer

8、Id;public String CustomerName;public String CustomerPhone;public String CustomerPostcode;public Customer()CustomerId=ALFKI;CustomerName=Maria Anders;CustomerPhone=(171)555-2222;CustomerPostcode=12209;public void displayDetails()/写入显示顾客的信息的代码实例分析w步骤步骤5:编写代码显示测试值:编写代码显示测试值 public class Customer public

9、 String CustomerId;public String CustomerName;public String CustomerPhone;public String CustomerPostcode;public Customer()CustomerId=ALFKI;CustomerName=Maria Anders;CustomerPhone=(171)555-2222;CustomerPostcode=12209;public void displayDetails()System.out.println(“Id of an Customer is+CustomerId);Sys

10、tem.out.println(“Name of an Customer is+CustomerName);System.out.println(“Phone of an Customer is+CustomerPhone);System.out.println(“Postcode of an Customer is+CustomerPostcode);实例分析w步骤6:编写main()方法w步骤7:调用方法w步骤8:编译运行程序public class Customer public String CustomerId;public String CustomerName;public St

11、ring CustomerPhone;public String CustomerPostcode;public Customer()CustomerId=ALFKI;CustomerName=Maria Anders;CustomerPhone=(171)555-2222;CustomerPostcode=12209;public void displayDetails()System.out.println(“Id of an Customer is+CustomerId);System.out.println(“Name of an Customer is+CustomerName);S

12、ystem.out.println(“Phone of an Customer is+CustomerPhone);System.out.println(“Postcode of an Customer is+CustomerPostcode);public static void main(String args )Customer customerObject=new Customer();customerObject.displayDetails();稍等一下w编写CustomerCollection类,该类保存并显示三个顾客的信息。w以下是提供给你的Customer类的代码模板。cla

13、ss Customer public String CustomerId;public String CustomerName;public String CustomerPhone;public String CustomerPostcode;public void displayDetails()/显示顾客的信息的代码System.out.println(CustomerId);System.out.println(CustomerName);System.out.println(CustomerPhone);System.out.println(CustomerPostcode);给方法

14、传参数w传值调用传值调用w引用调用引用调用传值调用 calling()int percent=10;System.out.println(“Before:percent=”+percent);called(percent);System.out.println(“After:percent=”+percentt);called(int x)x=3*x;System.out.println(“End of method:x=”+x);1030值被拷贝值增至3倍percent=x=引用调用 calling()Person harry=new Person();called(harry);calle

15、d(Person x)x.raiseSalary(200);拷贝引用薪金增至3倍harry=x =Person构造函数w声明构造函数的语法规则w默认构造函数构造函数的语法规则w一个新对象初始化的最终步骤是去调用对象的构造函数。w构造函数必须满足以下条件:方法名必须与类名称完全相匹配;方法名必须与类名称完全相匹配;不要声明返回类型;不要声明返回类型;不能被不能被static、final、synchronized、abstract、native修饰。修饰。默认构造函数w默认构造方法是没有参数的构造函数,你可以显式定义类的默认构造函数。w当一个类没有包含构造函数时,Java将自动提供一个默认构造函数

16、。该构造函数没有参数,用public 修饰,而且方法体为空。格式如下:public classname()w只要类中显式定义了一个或多个构造函数,而且所有显式定义的构造函数都带参数,那么将失去默认构造函数。示例:构造函数/第一个构造函数,无参数,默认给出长宽高Box()width=20;height=30;depth=15;/第二个构造函数,给出长宽高的参数Box(double w,double h,double d)width=w;height=h;depth=d;/第三个构造函数,给出另一个Box作参数Box(Box r)width=r.getWidth();height=r.getHei

17、ght();depth=r.getDepth();Box boxes=new Box3;boxes0=new Box();boxes1=new Box(12,20,10);boxes2=new Box(boxes0);访问说明符w信息隐藏是OOP最重要的功能之一,也是使用访问说明符的原因w信息隐藏的原因包括:对任何实现细节所作的更改不会影响使用该类的代码对任何实现细节所作的更改不会影响使用该类的代码防止用户意外删除数据防止用户意外删除数据此类易于使用此类易于使用 访问修饰符访问修饰符访问说明符访问说明符访问说明符访问说明符privateprotectedpublic默认public访问说明符w

18、所有的类除了内部类(在其它类里的类)都能有public访问说明符。w你能从任何Java程序中的任何对象里使用共有类、成员变量和方法。w例子:public class PublicClass public int publicVariable;public void publicMethod().public class Applicant public String applicantID;public String applicantName;public String applicantAddress;public String applicantPosition;public void

19、displayDetails()System.out.println(“I am a Applicant”);示例:public访问说明符public class otherClass public static void main(String args)Applicant applicant;applicant=new Applicant();applicant.displayDetails();protected 访问说明符w在类中声明为protected的变量、方法和内部类能被其子类所访问。w你定义了一个protected成员,那么这个成员可以在以下情况下被访问:类本身类本身相同包中的

20、其它类相同包中的其它类子类(可以在相同包中或不同包中)子类(可以在相同包中或不同包中)package HR;public class Applicant protected String applicantID;protected String applicantName;protected String applicantAddress;protected String applicantPosition;protected void displayDetails()System.out.println(“I am a Applicant”);示例:protected访问说明符package

21、 Other;import HR.*;public class testDifferentPackage public static void main(String args)Applicant applicant;applicant=new Applicant();applicant.displayDetails();Why wrong?private 访问说明符w只有相同类的对象才能访问私有变量或方法。w只能规定变量、方法和内部类是私有的。w例子private class Applicant /some codes hereWhy wrong?class Applicant privat

22、e String applicantID;private String applicantName;private String applicantAddress;private String applicantPosition;private void displayDetails()System.out.println(“I am a Applicant”);示例:private访问说明符public class testPrivateClass public static void main(String args)Applicant applicant;applicant=new Ap

23、plicant();applicant.displayDetails();Why wrong?package访问说明符w如果我们没有规定访问说明符,那就是friendly(友元)或(package)的。wfriendly不是关键字。w拥有友元访问符的类、变量或方法对包里所有的类来说都是可以访问的。示例:package访问说明符class Xclass Yclass ZPackage APackage B方法accessMe()已在类X中声明。下列表格告诉你从类Y和Z中访问方法accessMe()的可能性。访问说明符类Y类Zprotected accessMe()可访问,Y是子类可访问,Z是子类

24、(即使它在另一包中)accessMe()可访问,在同一包中不可访问,不在同一包中访问说明符访问说明符同一个类同包 不同包,子类不同包,非子类private protected public default 修饰符wstaticwfinalwabstract wnativewtransientwsynchronizedwvolatilestatic修饰符w类(static)变量w类(static)方法w静态初始化程序static(静态域)w用static修饰的域是仅属于类的静态域。w静态域是类中每个对象共享的域。w它们是类的域,不属于任何一个类的具体对象。w静态域是一个公共的存储单元,任何一个类

25、的对象访问它时,取到的都是相同的数值。public class Count private int serialNumber;private static int counter;public Count()counter+;serialNumber=counter;System.out.println(My serialNumber is +serialNumber);.static(静态域)public static void main(String args)System.out.println(At first,counter=+counter);Count count1=new Co

26、unt();System.out.println(after creat count1,counter=+counter);Count count2=new Count();System.out.println(At last counter=+counter);System.out.println(count1.serialNumber“+count1.serialNumber);System.out.println(count2.serialNumber“+count2.serialNumber);System.out.println(count1.counter+count1.count

27、er);System.out.println(count2.counter+count2.counter);System.out.println(Count.counter+Count.counter);static(静态域)堆区Count对象serialNumber=1方法区Count的类型信息counter=1count1引用变量堆区Count对象serialNumber=1方法区Count的类型信息counter=2count1引用变量count2引用变量Count对象serialNumber=2Count count1=new Count();Count count2=new Coun

28、t();Count.counter/合法Count.serialNumber/非法静态方法w成员方法分为类方法和实例方法。用static修饰的方法叫类方法,或静态方法。w静态方法也和静态变量一样,不需创建类的实例,可以直接通过类名被访问。wstatic方法不能被修饰成protected和abstract。静态方法public class Wrong int x;void method()x+;public static void test()x=1;/非法 method();/非法 public static void main(String args)x=9;/非法 method();/非法

29、 堆区Wrong对象实例变量xWrong对象实例变量xWrong.test()?静态方法中不允许直接访问实例变量和实例方法静态方法中不允许直接访问实例变量和实例方法。静态方法public class Correctint x;void method()x+;/合法 public static void main(String args)Correct r1=new Correct();r1.x=9;/合法 r1.method();/合法 Correct r2=new Correct();r2.x=10;/合法 r2.method();/合法 System.out.println(r1.x);S

30、ystem.out.println(r2.x);堆区Correct对象实例变量xCorrect对象实例变量x引用变量r1引用变量r2静态初始化程序w 类中可以包含静态代码块,它不存在于任何方法体中。当类被装载时,静态代码块只被执行一次。类中不同的静态块按它们在类中出现的顺序被依次执行。public class Samplestatic int i=5;static (First Static code i=+i+);static (Second Static code i=+i+);public static void main(String args)Sample s1=new Sample

31、();Sample s2=new Sample();System.out.println(At last,i=+i);打印First Static code i=5Second Static code i=6At last,i=7 final修饰符 w最终域最终域用用final修饰的域,实际上就是修饰的域,实际上就是Java中的常量。中的常量。w最终方法最终方法用用final修饰的方法是最终方法。修饰的方法是最终方法。最终方法是不能被当前类的子类重新定义的方法。最终方法是不能被当前类的子类重新定义的方法。w最终类最终类如果一个类被如果一个类被final修饰符所修饰和限定,说明这个类不修饰符所修

32、饰和限定,说明这个类不可能有子类。可能有子类。abstract修饰符w由abstract修饰的方法叫抽象方法;由abstract修饰的类叫抽象类。抽象方法必须声明在抽象类中。w抽象方法语法:abstract returntype method_name(parameter_list);w声明抽象类语法:abstract class w限制:抽象类不能被实例化。抽象类不能被实例化。子类必须重载超类的抽象方法。子类必须重载超类的抽象方法。抽象类必须有子类。抽象类必须有子类。this关键字wthis关键字引用当前实例w在static方法中不能使用this关键字public class ThisTes

33、t int x;ThisTest(int x)this.x=x;method(this);void method(ThisTest t)t.x+;/合法 public static void test()this.x+;/非法 ThisTest t1=new ThisTest(1);ThisTest t2=new ThisTest(2);ThisTest.out.println(t1.x);ThisTest.out.println(t2.x);堆区ThisTest对象实例变量xThisTest对象实例变量x引用变量t1引用变量t2对象的引用重载w方法重载方法重载w构造函数重载构造函数重载方法重

34、载(overload)w对于类的方法(包括从父类中继承的方法),如果有两个方法的方法名相同,但参数不一致,那么可以说,一个方法是另一个方法的重载方法。w重载方法必须满足以下条件:方法名相同;方法名相同;方法的参数类型、个数、顺序至少有一项不相同;方法的参数类型、个数、顺序至少有一项不相同;方法的返回类型可以不同;方法的返回类型可以不同;方法的修饰符可以不同。方法的修饰符可以不同。示例类的用于取最大值的max方法,/有多个重载方法。public static int max(int a,int b)public static long max(long a,long b)public stati

35、c float max(float a,float b)public static double max(double a,double b)int a=Math.max(1,2);double d=Math.max(1,2.0);构造方法重载wJava支持对构造方法的重载,这样一个类就可以有很多个构造方法。class Boxdouble width;double height;double depth;Box()width=20;height=30;depth=15;Box(double w,double h,double d)width=w;height=h;depth=d;BoxCons

36、(BoxCons r)width=r.getWidth();height=r.getHeight();depth=r.getDepth();Box(double len)width=height=depth=len;包w包允许将类组合成较小的单元(类似文件夹),使其易于查找和使用相应的类文件。w有助于避免命名冲突。在使用许多类时,类和方法的名称很难决定。有时需要使用与其它类相同的名称。包基本上隐藏了类并避免了名称上的冲突。w包允许在更广的范围内保护类、数据和方法,可以在包内定义类,而包外的代码不能访问该类。“包将类名空间划分为更加容易管理的块,包既是命名机制也是可见度控制机制”创建包packa

37、ge mypackage;public class Calculate public double volume(double height,double width,double depth)声明包语法:package 包名;导入包import mypackage.Calculate;public class PackageDemo public static void main(String args)Calculate calc=new Calculate();导入包语法:import package_name.*;import package_name.class_name;问题陈述w

38、你的项目组已为Motel168旅馆的HR系统定义了类。在检查类结构之前,项目经理已经将其否定并要求你重做。Customer和VIPCustomer类的结构如下:CustomerCustomerIdCustomerNameCustomerPhoneCustomerPostcodeVIPCustomerCustomerIdCustomerNameCustomerPhoneCustomerPostcodediscount继承的概念w继承是一种在已有类的基础上创建新类的机制。已有类称为超类已有类称为超类(super class)、基类、基类(base class)或父类或父类(parent class

39、)新类称为子类新类称为子类(subclass child class)或派生类或派生类(derived class)。w方法:创建一个共有属性的父类,再创建具有特殊属性的子类。创建一个共有属性的父类,再创建具有特殊属性的子类。继承的类型Animals类Herbivorous类Carnivorous类单继承单继承继承的类型Child类Mother类Father类多继承多继承继承的优点代码的可重用性减少代码冗余父类的属性和方法可用于子类易于修改和维护继承:类的层次父类父类/子类是子类是相对的相对的父父 类类 子类子类 Chrysler类Ford类Trains类类Vehicles类Planes类Au

40、tomobiles类GM类Chevrolet类Malibu类在Java中实现继承wextends关键字用于从超类派生类,换句话说,扩展超类的功能。w一个类只允许有一个直接的父亲。(单继承机制)C+允许多继承允许多继承 Java 通过接口方式来间接实现多继承通过接口方式来间接实现多继承w语法 类的修饰符 class extends /代码w编写一个父类w使用extends关键字,编写子类示例:在JAVA中实现继承 class Applicant String applicantName=Harry Pot;String applicantPosition=Manger;Applicant()vo

41、id displayDetails()(从父类Application中输出的信息);(申请人姓名+applicantName);(申请职位+applicantPosition);(=);public class Candidate extends Applicant String interviewDate=12-July-2007;boolean candidateStatus=false;Candidate()void display()(从子类Application中输出的信息);(面试日期+applicantName);(候选人状态+applicantPosition);(候选人姓名+

42、applicantName);(候选职位+applicantPosition);public class CandidateTest public static void main(String args)Candidate candidate=new Candidate();candidate.displayDetails();candidate.display();抽象类w抽象类定义了其子类的共同属性和行为。w它用作基类派生相同种类的特定类。w它定义了派生类的通用属性。w抽象类不能被实例化,即不允许创建抽象类本身的实例。w例子abstract class Base abstract voi

43、d method1();abstract void method2();class Sub extends Base /编译出错,Sub类必须声明为抽象类 void method1()System.out.println(method1);示例:抽象类abstract class Shape public abstract float calculateArea();public void displayDetails()System.out.println(Hello Shape);public class Circle extends Shape float radius;public f

44、loat calculateArea()return radius*22/7;public static void main(String args)Shape circle=new Circle();circle.displayDetails();抽象方法不具有任何实现代码final关键字wfinal类:不能被继承wfinal方法:不能被子类覆盖wfinal变量:必须被显式的初始化,并且只能初始化一次public final class Apublic class B extends A /非法public class A public final int method()return 1;

45、public class B extends A public int method()/非法 return 2;稍等一下w以下代码段在编译阶段产生错误:w下列代码段在编译时产生错误:abstract final class Customer/Customer类的代码final class Customer/Customer类的代码class VIPCustomer extends Customer/VIPCustomer类的代码稍等一下w下列代码段在编译时产生错误:试确定错误产生的原因,并纠正错误。abstract class Customerpublic static void main(

46、String args )/创建类的对象Customer c=new Customer();接口w接口就是某个事物对外提供的一些功能的声明。w可以利用接口实现多态,同时接口也弥补了Java单一继承的弱点。w使用interface关键字定义接口。w一般使用接口声明方法或常量,接口中的方法只能是声明,不能是具体的实现。public interface InterfaceName/接口体定义接口w语法:w例子public interface Color final int white=0;final int black=1;final int red=2;.public void changeCol

47、or(int newColor);/该方法没有方法体实现接口的步骤w导入接口。w使用implements关键字后跟接口的名字来说明类。w保证类能实现在接口中定义的每种方法。w用.java扩展名保存文件。w编译applet或创建的应用程序。示例:接口public interface MyInterfacepublic void calculatePerimeter(double height,double width);public void calculateVolume(double height,double width,double depth);public class Interfa

48、ceDemo implements MyInterface public void calculatePerimeter(double height,double width)(“the perimeter is”+2*(height+width);public void calculateVolume(double height,double width,double depth)(“the volumn is”+(height*width*depth);public static void main(String args)MyInterface d=new MyInterfaceDemo

49、();d.calculatePerimeter(10.20);d.calculateVolume(9,10,11);接口规则 w类能实现许多接口。w所有接口的方法是抽象的。w所有接口的变量是public,static和 final。w在说明方法和变量时,不需要指定上述关键字。w不需要在接口里说明private 或protected 方法或变量,因为方法必须在其它类里重载。w接口的方法不能是final,因为final方法不能被任何类修改。方法覆盖父类子类-计算矩形面积子类 计算圆的面积ShapeShapecalculateArea()TrianglecalculateArea()calculat

50、eRectangleAreaCircle calculateArea()calculateCircleArea w例如:Rectangle(矩形)和Circle(圆)都是 Shape(图)的子类。Shape类 有calculateArea()方法。而计算图的面积的方法是不同的。因此calcu lateArea()方法以不同方式在类Rectangle和Circle里实现。示例:方法覆盖wWheelChair(轮椅)类派生自Chair类。两个类都有方adjustHeight()。然而轮椅类的调整方法不同于其它椅子的调整方法。因此,轮椅类覆盖超类的adjustHeight()方法。class Cha

展开阅读全文
相关资源
相关搜索

当前位置:首页 > 教育专区 > 大学资料

本站为文档C TO C交易模式,本站只提供存储空间、用户上传的文档直接被用户下载,本站只是中间服务平台,本站所有文档下载所得的收益归上传人(含作者)所有。本站仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。若文档所含内容侵犯了您的版权或隐私,请立即通知得利文库网,我们立即给予删除!客服QQ:136780468 微信:18945177775 电话:18904686070

工信部备案号:黑ICP备15003705号-8 |  经营许可证:黑B2-20190332号 |   黑公网安备:91230400333293403D

© 2020-2023 www.deliwenku.com 得利文库. All Rights Reserved 黑龙江转换宝科技有限公司 

黑龙江省互联网违法和不良信息举报
举报电话:0468-3380021 邮箱:hgswwxb@163.com