1.实验名称 抽象类与接口

2.实验目的(1)熟练掌握抽象类及接口的定义及使用;(2)掌握多态的含义,学会在实际应用中使用多态;(3)理解接口回调的含义,学会在实际应用中使用。

3.训练知识点集群(1)抽象类(定义,使用,方法重写,多态的实现等)(2)接口(定义,使用,方法重写,多态的实现,接口回调)

4.实验内容

内容1:利用抽象类和多态的知识完成宠物商店宠物的“自我介绍”程序。

要求:设计一个模拟发声器,可以根据输入的类型输出对应的宠物的信息。

输出格式为:我是一只狗。我叫乐乐,是一只黑色的牧羊犬。我会汪汪汪…。

我是一只猫。我叫咪咪,是一只白色的波斯猫。我会喵喵喵…。

内容2:首先为线性表定义接口及抽象类,然后使用数组实现线性表。

(1)定义一个接口MyList,里面包含插入、查找、删除、替换、获取线性表长度、判断线性表是否为空、清空线性表等抽象方法;

(2)定义一个抽象类MyAbstractList实现接口MyList,该抽象类拥有唯一的属性size(protected修饰),表示当前线性表的长度,并提供无参和有参构造方法完成线性表的初始化,重写接口中添加、删除、判断线性表是否为空及返回线性表长度的方法;

(3)利用数组实现线性表:定义一个MyArrayList类,继承自抽象类MyAbstractList,并利用数组实现所有操作。

(4)给出测试类

内容3完善电商购物平台项目,在已有类(商品类目类、商品类和商品数据库类)的基础上,增加商品信息查询接口BookDao类和接口的实现类BookDaoaImpl,完成商品信息录入,修改,查询等功能。

内容4:(学生自由创造)

根据你理解的接口的含义,自己写一个有关接口的案例,并测试运行。请在下面描述你的案例:

一 自我介绍

package one;abstract class Pet {private String name;private String color;private String breed;public Pet() {super();}public Pet(String name, String color, String breed) {super();this.name = name;this.color = color;this.breed = breed;}public String getName() {return name;}public void setName(String name) {this.name = name;}public String getColor() {return color;}public void setColor(String color) {this.color = color;}public String getBreed() {return breed;}public void setBreed(String breed) {this.breed = breed;}@Overridepublic String toString() {return "我叫" + name + ", 我是一只" + color + "的" + breed + "。";}//抽象方法:abstract修饰的方法叫抽象方法,抽象方法只有方法声明没有方法体,抽象方法只能放到抽象类中public abstract void cry();}class Dog extends Pet {public Dog() {super();}public Dog(String name, String color, String breed) {super(name, color, breed);}@Overridepublic void cry() {// TODO Auto-generated method stubSystem.out.println("我会汪汪汪...");}@Overridepublic String toString() {return "我是汪星人。" +super.toString();}}class Cat extends Pet{public Cat() {super();}public Cat(String name, String color, String breed) {super(name, color, breed);}@Overridepublic void cry() {// TODO Auto-generated method stubSystem.out.println("我会喵喵喵...");}@Overridepublic String toString() {return "我是喵星人。" +super.toString();}}class Simulator {public static void playSounds(Pet pet) {System.out.println(pet.toString());pet.cry();}}public class TestPet {public static void main(String[] args) {// TODO Auto-generated method stubSimulator si = new Simulator();si.playSounds(new Cat("咪咪", "白色", "波斯猫"));si.playSounds(new Dog("乐乐", "黑色", "牧羊犬"));}

二 线性表

package two;public class TestArrayList {public static void main(String[] args){MyArrayList list = new MyArrayList();list.add(0,"高等数学");list.add(1,"线性代数");list.add(2,"概率论与数理统计");list.add(3,"离散数学");list.printAll();System.out.println("现有元素个数:"+list.size()+"个");list.find("离散数学");list.find("大学物理");list.enprint("高等数学");list.del("概率论与数理统计");list.printAll();list.change(0, "面向对象程序设计");list.printAll();list.clear();list.printAll();list.empty();System.out.println(list.empty());}}class MyArrayList {int size;final int CAPACITY = 100;String[] data;//初始化public MyArrayList(){size = 0;data = new String[CAPACITY];}//线性表个数public int size(){return size;}//index代表插入位置,e代表将要插入的元素public void add(int index,String e){for(int i=size-1;i>=index;i--){data[i+1]=data[i];}data[index] = e;size++;}//线性表查找public void find(String e){int i=0;boolean m=false;for(;i=0;i--){if(data[i].equals(e)){System.out.println(data[i]+"是第"+(i+1)+"个元素");m=true;}}if(m==false){System.out.println("不存在"+e+"这个元素");}}//线性表删除public boolean del(String e){System.out.println("删除字符串"+e);for(int i=0;i<size;i++){if(data[i]==e){for(int j=i;j<size;j++){data[j]=data[j+1];}size--;return true;}}return false;}//线性表替换public void change(int index,String m){System.out.println("将第"+(index+1)+"个元素"+data[index]+"改成"+m);data[index]=m;}//线性表清空public void clear(){data=new String[CAPACITY];size=0;}//线性表判空public boolean empty(){if(size==0)return true;elsereturn false;}//输出public void printAll() {System.out.print("【");for(int i=0;i<size;i++){System.out.print(data[i]);if(i!=size-1)System.out.print(",");}System.out.println("】");}}

三 电商购物平台

package three.onlineshopping.dao;import three.onlineshopping.entity.Book;public interface BookDao {/** * 查询所有的书籍信息 * @return 所有的书籍信息 */public abstract Book[] queryAllBooks();/** * 根据书籍编号查询书籍的详细信息 * @param id 用户输入的书籍编号 * @return 书籍的详细信息 */public abstract Book queryBookById(String id);}
package three.onlineshopping.dao;import three.onlineshopping.db.BookDataSet;import three.onlineshopping.entity.Book;public class BookDaoImpl implements BookDao {private BookDataSet bds;public BookDaoImpl() {super();bds = new BookDataSet();}/** * 查询所有的书籍信息 * @return 所有的书籍信息 */@Overridepublic Book[] queryAllBooks() {// TODO Auto-generated method stubBook[] books = bds.getBooks();return books;}@Overridepublic Book queryBookById(String id) {// TODO Auto-generated method stub//获取所有的书籍信息Book[] books = bds.getBooks();//遍历所有的书籍信息做对比for(int i=0; i<books.length; i++) {if(books[i].getBid().equals(id)) {return books[i];}}//如果没有找到任何书籍信息return null;}}
package three.onlineshopping.db;import three.onlineshopping.entity.Book;import three.onlineshopping.entity.Category;public class BookDataSet {private Book[] books =new Book[4];public BookDataSet(){Category c = new Category(01, "小说", "玄幻小说");books[0] =new Book("b001", "盗墓笔记", "南派三叔", 100, c );books[1] =new Book("b002", "射雕英雄传", "金庸", 98, new Category(01, "小说", "武侠") );books[2] =new Book("b003", "疯狂英语", "李阳", 66, new Category(02, "工具", "英语学习") );books[3] =new Book("b004", "Java", "孙乔", 63, new Category(02, "工具", "软件编程") );}public Book[] getBooks() {return books;}public void setBooks(Book[] books) {this.books = books;}}
package three.onlineshopping.entity;public class Book {private String bid;private String name;private String author;private int num;private Category category;public Book() {super();}public Book(String bid, String name, String author, int num, Category category) {super();this.bid = bid;this.name = name;this.author = author;this.num = num;this.category = category;}public String getBid() {return bid;}public void setBid(String bid) {this.bid = bid;}public String getName() {return name;}public void setName(String name) {this.name = name;}public String getAuthor() {return author;}public void setAuthor(String author) {this.author = author;}public int getNum() {return num;}public void setNum(int num) {this.num = num;}public Category getCategory() {return category;}public void setCategory(Category category) {this.category = category;}@Overridepublic String toString() {return "书籍编号:" + bid + "|书名:" + name + "|作者:" + author + "|库存:" + num + "|书籍类目:" + category;}}
package three.onlineshopping.entity;public class Category {private int id;private String firstLevel;private String secondLevel;public Category() {super();}public Category(int id, String firstLevel, String secondLevel) {super();this.id = id;this.firstLevel = firstLevel;this.secondLevel = secondLevel;}public int getId() {return id;}public void setId(int id) {this.id = id;}public String getFirstLevel() {return firstLevel;}public void setFirstLevel(String firstLevel) {this.firstLevel = firstLevel;}public String getSecondLevel() {return secondLevel;}public void setSecondLevel(String secondLevel) {this.secondLevel = secondLevel;}@Overridepublic String toString() {return firstLevel + ">" + secondLevel;}}
package three.onlineshopping.service;public interface BookService {public void queryBooks();void queryBookById();}
package three.onlineshopping.service;import java.util.Scanner;import three.onlineshopping.dao.BookDao;import three.onlineshopping.dao.BookDaoImpl;import three.onlineshopping.entity.Book;public class BookServiceImpl implements BookService {private BookDao bDao = new BookDaoImpl();@Overridepublic void queryBooks() {// TODO Auto-generated method stubBook[] books = bDao.queryAllBooks();for(Book book:books) {System.out.println(book);}}@Overridepublic void queryBookById() {// TODO Auto-generated method stubSystem.out.println("请输入书籍编号:");Scanner sc = new Scanner(System.in);String id = sc.next();Book book = bDao.queryBookById(id);if(book == null) {System.out.println("没有这本书");}System.out.println(book);}}
package three.onlineshopping;import three.onlineshopping.service.BookService;import three.onlineshopping.service.BookServiceImpl;public class MainClass {public static void main(String[] args) {// TODO Auto-generated method stubBookService bs = new BookServiceImpl();bs.queryBooks();bs.queryBookById();}}

四 USB

package four;class Computer {public void useUSB(USB u) {u.install();u.work();u.close();}}class Mouse implements USB {@Overridepublic void install() {// TODO Auto-generated method stubSystem.out.println("安装鼠标驱动。");}@Overridepublic void work() {// TODO Auto-generated method stubSystem.out.println("使用鼠标~");}@Overridepublic void close() {// TODO Auto-generated method stubSystem.out.println("拔掉鼠标");}}class Printer implements USB {@Overridepublic void install() {// TODO Auto-generated method stubSystem.out.println("安装打印机驱动");}@Overridepublic void work() {// TODO Auto-generated method stubSystem.out.println("开始打印");}@Overridepublic void close() {// TODO Auto-generated method stubSystem.out.println("打印机关闭");}}interface USB {public abstract void install();public abstract void work();public abstract void close();}public class TestComputer {public static void main(String[] args) {// TODO Auto-generated method stubComputer c = new Computer();c.useUSB(new Mouse());c.useUSB(new Printer());}}