版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
1、实验五 Java的类及多态性一、实验目的:1. 掌握Java类及多态性的含义。2. 掌握instanceof的使用方法。3. 掌握应用变量的转换。二、实验要求: 编写一个能体现多态性的Java应用程序。三、实验内容:(1)类1、常量的声明 :声明一个常量PI,编译它,查看有何错误并修改。 错误:class Point int x,y;static int z;final double PI=3.; 修改:赋值3.Point(int a,int b) x=a; y=b; Point() this(1,1);static void output() System.out.println(outpu
2、t() called); System.out.println(z); void output(int a,int y) output(); z=5; this.x=x; this.y=y;public static void main(String args) Point pt1=new Point(); Point pt2=new Point(); pt1.z=5; pt2.z=6; System.out.println(pt1.z);2、声明一个静态的常量:修改上题的前半部分,如下。编译,查看有何错误,并修改错误:class Point int x,y;static int z;fina
3、l double PI ; 修改:删除static。Point(int a,int b) PI=3.;x=a;y=b;Point() this(1,1);static void output() System.out.println(output() called); System.out.println(z); void output(int a,int y) output(); z=5; this.x=x; this.y=y;public static void main(String args) Point pt1=new Point(); Point pt2=new Point();
4、pt1.z=5; pt2.z=6; System.out.println(pt1.z);3、继承:新建一个Animal.java文件,并编译它。查看源文件的当前目录下生成了几个类文件,并选择一个执行它,输出结果。class Animal int height,weight; void eat() System.out.println(animal eat); void sleep() System.out.println(animal sleep); void breathe() System.out.println(animal breathe); class Fish extends An
5、imalclass Integration public static void main(String args) Animal an=new Animal(); Fish fh=new Fish(); an.breathe(); fh.height=30; fh.breathe(); 4、修改上题中的class Fish extends Animal类,如下,并执行它,查看结果。class Fish extends Animal void breathe() System.out.println(animal bubble); 5、再次修改class Fish extends Animal
6、类和Integration类,如下,并执行,查看结果。并思考课件中对于super的说明。class Animal int height,weight; void eat() System.out.println(animal eat); void sleep() System.out.println(animal sleep); void breathe() System.out.println(animal breathe); class Fish extends Animal int height; void breathe() super.breathe(); super.height=
7、40; System.out.println(fish bubble); class Integration public static void main(String args) /Animal an=new Animal(); Fish fh=new Fish(); /an.breathe(); fh.height=30; fh.breathe(); Super 用法:o 提供了对父类的访问。o 可以使用super访问父类被子类隐藏的变量或覆盖的方法。6、分别在Animal类和Fish类中,添加构造方法如下:并将Integration类,修改如下class Animal int heig
8、ht,weight; void eat() System.out.println(animal eat); void sleep() System.out.println(animal sleep); void breathe() System.out.println(animal breathe); Animal() System.out.println(animal construct); class Fish extends Animal int height; void breathe() super.breathe(); super.height=40; System.out.pri
9、ntln(fish bubble); Fish() System.out.println(fish construct); class Integration public static void main(String args) /Animal an=new Animal(); Fish fh=new Fish(); 查看执行结果,并思考为什么思考:当实类化对象时,会默认执行里边的构造方法。7、执行下边程序,并修改错误class Animal int height,weight;Animal(int height,int weight) System.out.println(animal
10、construct); void eat() System.out.println(animal eat); void sleep() System.out.println(animal sleep); void breathe() System.out.println(animal breathe); class Fish extends Animal int height; Fish() super(); System.out.println(fish construct); void breathe() /super.breathe(); /super.height=40; System
11、.out.println(fish bubble); class Integration public static void main(String args) /Animal an=new Animal(); Fish fh=new Fish(); 修改:给super赋值。(2)多态性1、注意成员变量和成员方法的调用,查看输出结果 class Animal int height=1,weight=1;Animal() void eat() System.out.println(animal eat); void sleep() System.out.println(animal sleep
12、); void breathe() System.out.println(animal breathe); class Fish extends Animal int height=10; Fish() void breathe() System.out.println(fish breathe); void swim() System.out.println(fish swim); class Integration static void fn(Animal an) an.breathe(); public static void main(String args) Animal an=n
13、ew Animal(); System.out.println(an.height); an.eat(); an.breathe(); Fish fh=new Fish(); System.out.println(fh.height); fh.breathe(); fh.swim(); Animal x=new Fish(); System.out.println(x.height); x.eat(); x.breathe(); 2、在main方法中,用x调用swim方法,添加代码如下: x.swim();编译后,发现有何错误?修改:引用变量只能调用声明该变量时所用类里包含的方法。3、inst
14、anceof方法的使用在上题main方法中添加如下代码,并写出结果 class Animal int height=1,weight=1;Animal() void eat() System.out.println(animal eat); void sleep() System.out.println(animal sleep); void breathe() System.out.println(animal breathe); class Fish extends Animal int height=10; Fish() void breathe() System.out.println
15、(fish breathe); void swim() System.out.println(fish swim); class Integration static void fn(Animal an) an.breathe(); public static void main(String args) Animal an=new Animal(); System.out.println(an.height); an.eat(); an.breathe(); Fish fh=new Fish(); System.out.println(fh.height); fh.breathe(); fh
16、.swim(); Animal x=new Fish(); System.out.println(x.height); x.eat(); x.breathe(); Fish fh2=(Fish)x; fh2.swim(); if(x instanceof Fish) System.out.println(x is fishs instance); else System.out.println(x isnt fishs instance); if(fh instanceof Animal) System.out.println(fh is animals instance); else Sys
17、tem.out.println(fh isnt animals instance); 4、引用变量之间的强制类型转换阅读程序,并编译,查看有何错误public class TestConversionpublic static void main(String args) double d = 13.4;long l = (long)d;System.out.println(l);int in = 5;/下面代码编译时出错:试图把一个数值型变量转换为boolean型,/编译时候会提示: 不可转换的类型/boolean b = (boolean)in;Object obj = Hello;/ob
18、j变量的编译类型为Object,是String类型的父类,可以强制类型转换/而且obj变量实际上类型也是String类型,所以运行时也可通过String objStr = (String)obj;System.out.println(objStr);/定义一个objPri变量,编译类型为Object,实际类型为IntegerObject objPri = new Integer(5);/objPri变量的编译类型为Object,是String类型的父类,可以强制类型转换/而objPri变量实际上类型是Integer类型,所以下面代码运行时引发ClassCastException异常String st
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 2025-2026学年安徽合肥七中高二上第一次月考数学试卷含答案
- 2026-2027学年上学期单元测试九年级数学真题卷及答案解析
- 2025韩语TOPIK初级听力历年真题及答案
- 2025焊工操作证焊接基础每日一练及答案
- 2025安全生产负责人安全生产法律法规易错题及答案
- 2025保安员安全防范章节练习题及答案
- 港口油品储罐建设方案
- 确认某客户订阅服务到期续费的通知函(7篇范文)
- 绿色建材选购与施工指导手册
- 工业生产线操作与质量控制岗位绩效考核表
- 2026浙江宁波市宁海县国有企业招聘考试参考题库及答案解析
- 2026年中国抗体药物市场现状调研分析及前景预测报告
- 2026消防宣传月培训课件(含火灾案例)
- 2026医工交叉研究发展分析及跨学科创新预测报告
- 基于PDCA循环的长期医嘱口服药发放质量改进与安全管理实践
- 家装托管服务合同协议书
- 2026河南政治高考真题试卷(含答案解析)
- 35kv集电线路运行规程
- 专题01古代诗歌鉴赏客观选择题考点解析(教案)
- 脱硫塔改造合同协议
- 2024中医内科临床诊疗指南眩晕病(原发性高血压)
评论
0/150
提交评论