JAVA实验8.doc_第1页
JAVA实验8.doc_第2页
JAVA实验8.doc_第3页
JAVA实验8.doc_第4页
JAVA实验8.doc_第5页
已阅读5页,还剩9页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

浙江经贸职业技术学院实训报告系部_ 班级_ 姓名_ 学号_日期_ 地点_ 指导教师_ 成绩 实验九 JAVA 面向对象编程(5) 【实验目的】1.掌握JAVA语言接口的定义和使用。2.掌握JAVA语言抽象类的声明和使用。3.掌握JAVA语言抽象方法的概念和使用。4.掌握类成员变量及方法的使用。【预习内容】1. JAVA 抽象类的定义格式;2.JAVA 接口定义的语法格式;3.this 和 super关键词的功能和区别;4.JAVA构造方法的重载;5.JAVA多态的概念,及重载和覆盖的区别;第 1 页 共 14 页【实验内容及步骤】1定义一个学生类,包括属性:学号(ID),姓名(name),成绩(score);构造方法(带三个参数);每个属性的访问器方法。【程序代码】import java.util.Scanner;class Student String ID = ;String name = ;float Score = 0.0f;Student (String ID, String name, float Score) this.ID = ID; = name;this.Score = Score;public String getID () return ID;public String getName () return name;public float getScore () return Score;public class Test_1 public static void main (String args) Scanner sc = new Scanner(System.in);System.out.println(请输入学号:);String ID = sc.nextLine();System.out.println(请输入姓名:);String name = sc.nextLine();System.out.println(请输入成绩:);float Score = sc.nextFloat();Student a = new Student(01, 小明, 555);System.out.println(学号: + a.getID();System.out.println(姓名: + a.getName();System.out.println(成绩: + a.getScore();【程序运行结果】 2 创建类A1,实现构造方法中输出This is A;创建A1的子类B1,实现构造方法中仅输出This is B;创建B1的子类C1,实现构造方法中仅输出This is C。 【程序代码】class A1 A1 () System.out.println(This is A!);A1 (int i) class B1 extends A1 B1 () super(1);System.out.println(This is B!);B1 (int i) super(1);class C1 extends B1 C1 () super(1);System.out.println(This is C!);public class Test_2 public static void main (String args) A1 a = new A1();B1 b = new B1();C1 c = new C1();【程序运行结果】3定义一个人类(Person),它包含属性:姓名(name),性别(sex);带两个参数的构造方法;属性的访问器方法。定义上面人类的子类学生类(Student),包括属性:学号(ID),带参数的构造方法;属性的访问器方法。【程序代码】class Person String Name = null;String Sex = null;Person (String Name, String Sex) this.Name = Name;this.Sex = Sex;public String getName () return Name;public String getSex () return Sex;class Student extends Person String ID = null;Student (String ID) super(wj, 女);this.ID = ID;public String getID () return ID;public class Test_3 public static void main (String args) Person p = new Person(cc, 男);Student s = new Student(01);System.out.println(姓名: + p.getName() + n + 性别: + p.getSex();System.out.println(姓名: + s.getName() + n + 性别: + s.getSex() + n + 学号: + s.getID();【程序运行结果】【练习题】Static关键字与普通变量的区别: 4. 创建一个名称为StaticDemo的类,并声明一个静态变量和一个普通变量。对变量分别赋予10和5的初始值。在main()方法中输出变量值。【程序代码】class StaticDemo static int i = 10;int j = 5;public class Test_4 public static void main (String args) StaticDemo s = new StaticDemo();System.out.println(i = + s.i + nj = + s.j);【程序运行结果】 【练习题】覆盖、继承 5. 建立一个汽车Auto类,包括轮胎个数,汽车颜色,车身重量、速度等成员变量。并通过不同的构造方法创建实例。至少要求: 汽车能够加速,减速,停车。 再定义一个小汽车类Car,继承Auto,并添加空调、CD等成员变量,覆盖加速,减速的方法【程序代码】import java.util.Scanner;class Auto int sum = 4;String color = red;float Weight = 1.0f;float Speed = 0.0f;Auto (int sum, String color, float Weight, float Speed) this.sum = sum;this.color = color;this.Weight = Weight;this.Speed = Speed;public void addSpeed (float Speed) this.Speed += Speed;getSpeed();public void redSpeed (float Speed) this.Speed -= Speed;getSpeed();public void Stop () Speed = 0.0f;System.out.println(已停车);public void getSpeed () System.out.println(汽车当前速度为: + Speed + KM/H);class Car extends Auto String AirConditioning = 无;String CD = 无;Car (int sum, String color, float Weight, float Speed, String AirConditioning, String CD) super(sum, color, Weight, Speed);this.AirConditioning = AirConditioning;this.CD = CD;public void addSpeed (float Speed) this.Speed += Speed;getSpeed();public void redSpeed (float Speed) this.Speed -= Speed;getSpeed();public class Test_5 public static void main (String args) Scanner sc = new Scanner(System.in);Scanner ss = new Scanner(System.in);System.out.println(请输入汽车轮胎数量:);int sum = sc.nextInt();System.out.println(请输入汽车颜色:);String color = ss.nextLine();System.out.println(请输入汽车重量:);float Weight = sc.nextFloat();System.out.println(请输入汽车当前速度:);float Speed = sc.nextFloat();Auto a = new Auto(sum, color, Weight, Speed);System.out.println(请输入要加速多少KM/H:);float addSpeed = sc.nextFloat();a.addSpeed(addSpeed);System.out.println(请输入要减速多少KM/S);float redSpeed = sc.nextFloat();a.redSpeed(redSpeed);a.Stop();System.out.println(请输入汽车轮胎数量:);sum = sc.nextInt();System.out.println(请输入汽车颜色:);color = ss.nextLine();System.out.println(请输入汽车重量:);Weight = sc.nextFloat();System.out.println(请输入汽车当前速度:);Speed = sc.nextFloat();System.out.println(请输入汽车是否有空调或空调种类:);String AirConditioning = ss.nextLine();System.out.println(请输入汽车是否有CD:);String CD = ss.nextLine();Car c = new Car(sum, color, Weight, Speed, AirConditioning, CD);System.out.println(请输入要加速多少KM/H:);addSpeed = sc.nextFloat();c.addSpeed(addSpeed);System.out.println(请输入要减速多少KM/S);redSpeed = sc.nextFloat();c.redSpeed(redSpeed);c.Stop();【程序运行结果】6.(1)封装一个People类型,具有height和weight属性,具有speakHello、averageHeight、averageWeight功能。 (2)封装一类ChinaPeople类型是People的子类,新增chinaMartial功能,override超类的speakHello、averageHeight、averageWeight功能。(3)封装一类AmericanPeople类型是People的子类,新增AmericanBoxing功能,override超类的speakHello、averageHeight、averageWeight功能。 (4)封装一类BeijingPeople类型是ChinaPeople的子类,新增BeijingOpera功能,override超类的speakHello、averageHeight、averageWeight功能。 (5)用一个程序执行入口Test测试上述对象 【程序代码】class People float Height = 175.0f;float Weight = 70.0f;public void speakHello () System.out.println(People:Hello!);public void averageHeight () System.out.println(Peoples averageHeight: + Height + cm);public void averageWeight () System.out.println(Peoples averageWeight: + Weight + KG);class ChinaPeople extends People ChinaPeople () ChinaPeople (float Height, float Weight) this.Height = Height;this.Weight = Weight;public void chinaMartial () System.out.println(chinaMartial);public void speakHello () System.out.println(ChinaPeople:Hello!);public void averageHeight () System.out.println(ChinaPeoples averageHeight: + Height + cm);public void averageWeight () System.out.println(ChinaPeoples averageWeight: + Weight + KG);class AmericanPeople extends People AmericanPeople () AmericanPeople (float Height, float Weight) this.Height = Height;this.Weight = Weight;public void AmericanBoxing () System.out.println(AmericanBoxing);public void speakHello () System.out.println(AmericanPeople:Hello!);public void averageHeight () System.out.println(AmericanPeoples averageHeight: + Height + cm);public void averageWeight () System.out.println(AmericanPeoples averageWeight: + Weight + KG);class BeijingPeople extends ChinaPeople BeijingPeople () BeijingPeople (float Height, float Weight) this.Height = Height;this.Weight = Weight;public void BeijingOpera () System.out.println(BeijingOpera!);public void speakHello () System.out.println(BeijingPeople:你好!);public

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论