实验02-面向对象编程.docVIP

  • 37
  • 0
  • 约6.8千字
  • 约 9页
  • 2021-06-28 发布于广东
  • 举报
第 PAGE 1 页 共 NUMPAGES 9 页 实验二 面向对象编程 1.实验目的 (1)掌握类与对象基本知识; (2)Java中的继承机制及包(package)、接口(interface)等的设计方法; (3)掌握static、this、super等关键字的使用; (4)掌握Java中两种比较器的用法。 2.实验内容 实验题1 定义一个类Book,包含两个属性:一个是private的String类型的属性title、一个是private的float类型的属性listPrice,封装这两个属性的四个方法setTitle()和getTitle()、setListPrice( )和geListPrice()。 基本要求: (1) 设计类Book及类BookManagement,在类BookManagement中输出Book类的对象的两个private属性; (2)重写父类的toString()方法,实现Book类实例的两个属性的输出。 package package1; public class Book { private String title; private float listPrice; public Book(String Title,float price){ super(); this.title=Title; this.listPrice=price; } public Book(){ super(); } public void settitle(String Title){ this.title=Title; } public String gettitle(){ return title; } public void setlistPrice(float price){ this.listPrice=price; } public float getlistPrice(){ return listPrice; } public String toString(){ return(name:+this.title+ Price+this.listPrice); } } package package1; public class BookManagement { public static void main(String[] args){ Book b1=new Book(How to learn Java?,45); System.out.println(name:+b1.gettitle()+ Price+b1.getlistPrice()); System.out.println(b1.toString()); } } 实验运行结果: 实验题2 有两个类:MobileManagement和Mobile,Mobile类的的两个实例分别描述如图3.4所示两部手机名称及价格,类MobileManagement在包.nwsuaf.jp.p3中,而Mobile在包.nwsuaf.jp.p3.data中。 基本要求:设计Mobile类与MobileManagement类,在类MobileManagement类中实例化Mobile类的对象使程序能够显示两部手机的价格和数量,运行结果如图3.5。 E365, 1780 RMB M330, 1450 RMB 图3.4 手机及价格图 图3.5 运行结果 package .nwsuaf.jp.p3.data; public class Mobile { /** * @param args */ private String Mobilename; private String Mobileprice; public void setMoilename(String name){ this.Mobilename=name; } public void setMoileprice(String price){ this.Mobileprice=price; } public String getMobilename(){ return this.Mobilename; } public String getMobileprice(){ return this.Mobileprice; } public Mobile(String name,String price){ t

文档评论(0)

1亿VIP精品文档

相关文档