重点总结
接口:一组规范,一个标准
//COLLECTION家族:list和set
- //collection 接口存储一组不唯一,无序de对象
List接口存储一组不唯一 有序(插入顺序)的对象
ArrayList:实现了长度可变的数组,在内存中分配连续的空间,遍历元素和随机访问元素的效率比较高
LinkedList:采用链表存储方式。插入、删除元素时效率比较高
Set接口存储一组唯一,无序的对象
//MAP家族
Map接口存储一组键值对象,提供key到value的映射
继承
习题1.租车公司的简单设计与实现,通过继承来实现Moto ,Car,Bus类
代码如下:
父类抽象类
package day81;
public abstract class Moto {
private int no;
private String brand;
private String color;
private double Mile;
public int getNo() {
return no;
}
public void setNo(int no) {
this.no = no;
}
public String getBrand() {
return brand;
}
public void setBrand(String brand) {
this.brand = brand;
}
public String getColor() {
return color;
}
public void setColor(String color) {
this.color = color;
}
public double getMile() {
return Mile;
}
public void setMile(double mile) {
Mile = mile;
}
public Moto() {
super();
}
public Moto(int no, String brand, String color, double mile) {
super();
this.no = no;
this.brand = brand;
this.color = color;
Mile = mile;
}
public abstract void Cal(int days,String no);
}
子类
package day81;
public class Car extends Moto{
private String Type;
public String getType() {
return Type;
}
public void setType(String type) {
Type = type;
}
public Car(String type) {
super();
Type = type;
}
public Car() {
super();
}
public final void Cal(int day,String type){
if(type.equals("您需要支付的费用是:"+"别克商务舱GL8"))
System.out.println("您需要支付的费用是:"+600*day);
else if(type.equals("您需要支付的费用是:"+"宝马550i"))
System.out.println("您需要支付的费用是:"+500*day);
else if(type.equals("别克林荫大道"))
System.out.println("您需要支付的费用是:"+300*day);
}
}
package day81;
public class Bus extends Moto{
private String Seat;
public String getSeat() {
return Seat;
}
public void setSeat(String seat) {
Seat = seat;
}
public Bus() {
super();
}
public Bus(String seat) {
super();
Seat = seat;
}
public final void Cal(int day,String Seat){
if(Seat.equals("大于16座"))
System.out.println("您需要支付的费用是:"+1500*day);
else
System.out.println("您需要支付的费用是:"+800*day);
}
}
Test主函数:
package day81;
import java.util.Scanner;
public class Test {
public static void main(String[] args) {
System.out.println("******欢迎来到租车公司******");
Scanner input = new Scanner(System.in);
System.out.println("请输入想租的车型(轿车或者客车):");
String name= input.next();
if(name.equals("轿车")){
Car aCar = new Car();
System.out.println("请输入向租赁的轿车的车型:");
String type = input.next();
System.out.println("请输入想租赁的天数:");
int day = input.nextInt();
aCar.Cal(day, type);
}
else if(name.equals("客车")){
System.out.println("请输入向租赁的客车的座位:");
String type = input.next();
System.out.println("请输入想租赁的天数:");
int day = input.nextInt();
Bus bus = new Bus();
bus.Cal(day, type);
}
}
}
效果图:
接口
练习2.最经典的打印机问题,打印纸张大小和颜色不同,使用接口来实现
代码如下:
接口paper:
package day92;
public interface Paper {
void size();
}
接口InkBox:
package day92;
public interface InkBox {
void use();
}
打印机主函数:
package day92;
import java.util.Scanner;
public class Printer implements InkBox,Paper{
private String paper;
private String color;
public String getPaper() {
return paper;
}
public void setPaper(String paper) {
this.paper = paper;
}
public String getColor() {
return color;
}
public void setColor(String color) {
this.color = color;
}
public Printer(String paper, String color) {
super();
this.paper = paper;
this.color = color;
}
public void size() {
// TODO Auto-generated method stub
if(paper.equals("A4")){
System.out.println("A4");
}
else
System.out.println("B5");
}
public void use() {
// TODO Auto-generated method stub
if(color.equals("彩色")){
System.out.println("彩色");
}
else
System.out.println("黑白");
}
public static void main(String[] args) {
System.out.println("请输入想要打印的纸张大小:");
Scanner input = new Scanner(System.in);
String paper = input.next();
System.out.println("请输入想要打印的纸张颜色:");
String color = input.next();
Printer aPrinter = new Printer(paper, color);
aPrinter.size();
aPrinter.use();
}
}
结果如下:
有关多态,链表遍历,增删改查的知识
练习三.学生信息管理系统,可以实现增删改,使用链表
代码如下:
学生类代码:
package day10;
public class Student {
private String name;
private String num;
private int age;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getNum() {
return num;
}
public void setNum(String num) {
this.num = num;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public Student(String name, String num, int age) {
super();
this.name = name;
this.num = num;
this.age = age;
}
public Student() {
super();
}
}
Test1代码:
package day10;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Scanner;
public class ArrayList1 {
public static void main(String[] args) {
// TODO Auto-generated method stub
Student []a = new Student[5];
List<Student> students = new ArrayList<Student>();
students.add(new Student("001", "li2", 20));
students.add(new Student("002", "li4", 25));
students.add(new Student("003", "li5", 17));
students.add(new Student("004", "li6", 17));
students.add(new Student("005", "li7", 23));
Iterator<Student> i = students.iterator();
while(i.hasNext())//判断下一个元素之后有值
{
Student aStudent = i.next();
System.out.println(aStudent.getName()+" "+aStudent.getNum()+" "+aStudent.getAge());
}
int q = 0;
Scanner input = new Scanner(System.in);
System.out.println("请输入需要修改的学号:");
String num = input.next();
for (int j = 0; j < 5; j++) {
String aString = students.get(j).getName();
if(num.equals(aString)){
System.out.println("请输出要修改的学员的姓名:");
String name = input.next();
System.out.println("请输出要修改的学员的年龄:");
int age = input.nextInt();
students.set(j, new Student(num,name,age));
Iterator<Student> i1 = students.iterator();
while(i1.hasNext())//判断下一个元素之后有值
{
Student aStudent = i1.next();
System.out.println(aStudent.getName()+" "+aStudent.getNum()+" "+aStudent.getAge());
}
}else {
q++;
if(q==5){
System.out.println("对不起,没有此学员");
break;
}
}
}
}
}
Tesy2代码:
package day10;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Scanner;
public class ArrayList2 {
public static void main(String[] args) {
// TODO Auto-generated method stub
Student []a = new Student[5];
List<Student> students = new ArrayList<Student>();
students.add(new Student("001", "li2", 20));
students.add(new Student("002", "li4", 25));
students.add(new Student("003", "li5", 17));
students.add(new Student("004", "li6", 17));
students.add(new Student("005", "li7", 23));
Iterator<Student> i = students.iterator();
while(i.hasNext())//判断下一个元素之后有值
{
Student aStudent = i.next();
System.out.println(aStudent.getName()+" "+aStudent.getNum()+" "+aStudent.getAge());
}
Scanner input = new Scanner(System.in);
System.out.println("请输入需要删除的学号:");
String num = input.next();
int q=0;
for (int j = 0; j < 5; j++) {
String aString = students.get(j).getName();
if(num.equals(aString)){
System.out.println("删除成功");
students.remove(j);
Iterator<Student> i1 = students.iterator();
while(i1.hasNext())//判断下一个元素之后有值
{
Student aStudent = i1.next();
System.out.println(aStudent.getName()+" "+aStudent.getNum()+" "+aStudent.getAge());
}
break;
}
else {
q++;
if(q==5){
System.out.println("对不起,没有此学员");
break;
}
}
}
}
}