1、实验02 面向对象编程实验二 面向对象编程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
2、及类BookManagement,在类BookManagement中输出Book类的对象的两个private属性;(2)重写父类的toString()方法,实现Book类实例的两个属性的输出。package cn.edu.nwsuaf.jp.p2.data;public class Book private String title; private float listPrice; public static int num;public Book(String t, double p) this.title = t; this.listPrice = (float) p; num+;pub
3、lic String getTitle() return title;public void setTitle(String title) this.title = title;public float getListPrice() return listPrice;public void setListPrice(float listPrice) this.listPrice = listPrice;Overridepublic String toString() return getTitle() + n¥:+ getListPrice();package cn.edu.nwsuaf.jp
4、.p2;import javax.swing.JOptionPane;import cn.edu.nwsuaf.jp.p2.data.Book;public class BookManagement public static void main(String args) Book book1 = new Book(闲话中国人,28.5); Book book2 = new Book(呼啸山庄,20); /System.out.println(book.toString(); JOptionPane.showMessageDialog(null, Book:n + book1 + n + bo
5、ok2 + n +There are + Book.num + books.); 实验题2 有两个类:MobileManagement和Mobile,Mobile类的的两个实例分别描述如图3.4所示两部手机名称及价格,类MobileManagement在包cn.edu.nwsuaf.jp.p3中,而Mobile在包cn.edu.nwsuaf.jp.p3.data中。基本要求:设计Mobile类与MobileManagement类,在类MobileManagement类中实例化Mobile类的对象使程序能够显示两部手机的价格和数量,运行结果如图3.5。 E365, 1780 RMB M330,
6、1450 RMB 图3.4 手机及价格图 图3.5 运行结果package cn.edu.nwsuaf.jp.p3.data;public class Mobile public static int num;private String name;private float price;public Mobile(String name, float price) this.name = name; this.price = price; num+;public String getName() return name;public float getPrice() return price
7、;package cn.edu.nwsuaf.jp.p3;import javax.swing.JOptionPane;import cn.edu.nwsuaf.jp.p3.data.Mobile;public class MobileManagement public static void main(String args) Mobile mobile1 = new Mobile(E365, 1780);Mobile mobile2 = new Mobile(M330, 1450);JOptionPane.showMessageDialog(null, Mobile phones:nn+
8、mobile1.getName()+: +mobile1.getPrice()+RMB+ n+ mobile2.getName()+: +mobile2.getPrice()+RMB+ nn+There are +Mobile.num+ mobile phones);实验题3 有四个类,主类Store在包cn.edu.nwsuaf.jp.p4中,Mobile、Mp3Player、Product在包cn.edu.nwsuaf.jp.p4.data中,Mobile、Mp3Player是Product的子类,Product类为抽象类。基本要求: 设计类Mobile和类MP3Player,使它们和类P
9、roduct、Store组成一个完整的程序,且运行结果如图3.6所示。 图3.6 运行结果package cn.edu.nwsuaf.jp.p4.data;public class Mobile extends Product private String MakePlace; public Mobile(String title, float Price, String makePlace) super(title, Price); this.MakePlace = makePlace; public String getMakePlace() return MakePlace; publi
10、c void setMakePlace(String makePlace) MakePlace = makePlace; Override public String getName() / TODO Auto-generated method stub return title + on + MakePlace + Mobile, + Price + RMBn; package cn.edu.nwsuaf.jp.p4.data;public class Mp3Player extends Product private long MemorySize; public Mp3Player(St
11、ring title, float Price, long memorySize) super(title, Price); this.MemorySize = memorySize; public long getMemorySize() return MemorySize; public void setMemorySize(long memorySize) MemorySize = memorySize; public String getName() return title + ( + MemorySize + MB + ), + Price + RMB; package cn.ed
12、u.nwsuaf.jp.p4.data;public abstract class Product protected String title; protected float Price; protected static int num; public Product(String title, float Price) super(); this.title = title; this.Price = Price; num+; public String getTitle() return title; public void setTitle(String title) this.t
13、itle = title; public float getPrice() return Price; public void setPrice(float price) Price = price; public static int getNum() return num; public static void setNum(int num) Product.num = num; public abstract String getName(); Override public String toString() return getName(); package cn.edu.nwsua
14、f.jp.p4;import javax.swing.JOptionPane;import cn.edu.nwsuaf.jp.p4.data.Mobile;import cn.edu.nwsuaf.jp.p4.data.Mp3Player;import cn.edu.nwsuaf.jp.p4.data.Product;public class Store public static void main(String args) Mobile mobile1 = new Mobile(E365, 1780, China);Mobile mobile2 = new Mobile(M330, 145
15、0, China); Mp3Player mp3Player1 = new Mp3Player(Meizo X3, 399 ,256);Mp3Player mp3Player2 = new Mp3Player(Meizo E5, 580 ,512);Mp3Player mp3Player3 = new Mp3Player(Xlive XM MP3 Player, 930 ,256);JOptionPane.showMessageDialog(null, The products are:nn + mp3Player1 +n + mp3Player2 + n + mp3Player3 + n +
16、 mobile1 + mobile2 +nThere are + Product.getNum() + products.);实验题4 有四个类,主类Store在包cn.edu.nwsuaf.jp.p4中,Mobile、Mp3Player、Product在包cn.edu.nwsuaf.jp.p4.data中,Mobile、Mp3Player是Product的子类, Product类实现Comparable接口,请重写Comparable接口中方法compareTo,实现product对象按照价格排序,运行结果如图3.8所示。 图3.7 运行结果package cn.edu.nwsuaf.jp.
17、p5.data;public class Mobile extends Product private String bus; public Mobile(String buss,String name, float price) super(name,price); this.bus = buss; public String getName() return name; public float getPrice() return price; public static int getCount() return count; public String toString() retur
18、n name + on +bus+, +String.valueOf(price)+ RMB; package cn.edu.nwsuaf.jp.p5.data;public class Mp3Player extends Product private int momery; public Mp3Player(String name,int mm, int price) super(name,(float)price); this.momery = mm; public String getName() return name; public float getPrice() return
19、price; public static int getCount() return count; public String toString() return name + (+momery+ MB+), +String.valueOf(price)+ RMB; package cn.edu.nwsuaf.jp.p5.data;public abstract class Product implements Comparable protected String name; protected float price; protected static int count; protect
20、ed Product(String name, float price) this.name = name; this.price = price; +count; public String getName() return name; public float getPrice() return price; public static int getCount() return count; public int compareTo(Product product) return new Float(product.getPrice().compareTo(price); package
21、 cn.edu.nwsuaf.jp.p5;import java.util.Arrays;import javax.swing.JOptionPane;import cn.edu.nwsuaf.jp.p5.data.Mobile;import cn.edu.nwsuaf.jp.p5.data.Mp3Player;import cn.edu.nwsuaf.jp.p5.data.Product;public class Store public static void main(String args) Mobile mobile1 = new Mobile(China Mobile, E365,
22、1780);Mobile mobile2 = new Mobile(China Mobile, M330,1450);Mp3Player player1 = new Mp3Player(Meizo X3, 256, 399);Mp3Player player2 = new Mp3Player(Meizo E5, 512, 580);Mp3Player player3 = new Mp3Player(Xlive XM MP3 Play,256, 930);Product products=mobile1,mobile2,player1,player2, player3;Arrays.sort(p
23、roducts);String text = ;for(int index = 0; index products.length; +index)text += productsindex.toString()+n;/ Displays the two mobile phones in a dialog box.JOptionPane.showMessageDialog(null,The products are:nn+text+nThere are +Product.getCount()+ products.);3实验总结在这次实习中,主要把课本上这段时间学过的知识又重新运用了一下,更深刻地了解了java面向对象的思想,掌握java程序面向对象编程的基本架构,会运用面向对象的思想编写简单的Java程序。在编写代码中也遇到了一些问题,比如,有些的输出结果与要求的不同,调用问题等,最后通过问同学、查资料解决了相关问题。通过这次实习,更清晰的了解了java中类,继承,接口,等的设计方法和理念,能更好的编写代码。