赞
踩
首先,明确:接口可以提供模糊的方法,方案;那么具体的实现需要另外创建实现类去实现;
另外:明确接口的特点:接口回调,接口的多态性;
具体解释:
接口的特点:
1.接口回调:接口回调是指可以把实现某一接口的类创建的对象的引用赋给该接口声明的接口变量中,那么该接口变量就可以调用被类重写的接口方法法;(接口变量 = new 实现类())-->(接口变量.实现类的方法)
2.接口的多态性:由接口产生的多态就是指不同的类在实现同一个接口时可能具有不同的实现方式,那么接口变量在回调接口方法时就可能具有多种形态。这儿就是类似行为多态
下面是例题:
1.准备一个学生类
- package test2;
-
- public class Student {
- //学生类
- private String name;
- private char sex;
- private double score;
-
- public Student() {
- }
-
- public Student(String name, char sex, double score) {
- this.name = name;
- this.sex = sex;
- this.score = score;
- }
-
- public String getName() {
- return name;
- }
-
- public void setName(String name) {
- this.name = name;
- }
-
- public char getSex() {
- return sex;
- }
-
- public void setSex(char sex) {
- this.sex = sex;
- }
-
- public double getScore() {
- return score;
- }
-
- public void setScore(double score) {
- this.score = score;
- }
- }
-
2.准备学生管理类----班级类:注意:创建接口变量接收实现类创建的对象(的地址);
- package test2;
-
- import java.util.ArrayList;
-
- public class Class {
- //班级管理平台;
- //班级管理需要整合班级学生;
-
- //面向接口编程;
- private Scheme scheme = new Scheme2();//这里就体现了多态的解耦合性
-
- //创建学生集合;
- private ArrayList<Student> students = new ArrayList<>();
- //写出class类的构造器为集合初始化;
- public Class()
- {
- //在学生集合中添加学生对象;
- students.add(new Student("张三",'男',85));
- students.add(new Student("李四",'男',99));
- students.add(new Student("小舞",'女',60));
- students.add(new Student("赵六",'男',100));
- students.add(new Student("四娘",'女',10));
- }
-
- //准备各项方法;(这里要求有两种方法)-- 用接口写出抽象方法,再分别由两个实现类分别作为两套方案具体实现;
-
- //打印全部学生信息;这里写一个方法存放实现类的方法一:
- public void info()
- {
- scheme.printinfo(students);
- }
- //打印学生的平均分;这里写一个方法存放实现类的方法二:
- public void scores(){
- scheme.printscore(students);
- }
-
- }
3.准备接口:
- package test2;
-
- import java.util.ArrayList;
-
- public interface Scheme {
- //方案一:
- //1.打印全部学生信息;
- //2.打印学生成绩平均分;
- //----------------------------
- //方案二:
- //1.打印全部学生信息(+统计男女人数);
- //2.打印学生(去掉最值后的)成绩平均分(挑出最高分,最低分);
- void printinfo(ArrayList<Student> students);
- void printscore(ArrayList<Student> students);
- }
4.准备两个实现类----两个方案:分别重写接口的方法
- package test2;
-
- import java.util.ArrayList;
-
- public class Scheme1 implements Scheme{
-
- //1.打印全部学生信息;
- //2.打印学生成绩平均分;
- @Override
- public void printinfo(ArrayList<Student> students) {
- System.out.println("====班级学生全部信息===");
- for (int i = 0; i < students.size(); i++) {
- Student s = students.get(i);
- System.out.println("姓名:" + s.getName());
- System.out.println("性别:" + s.getSex());
- System.out.println("分数:" + s.getScore());
- System.out.println("-------------------------");
- }
- }
-
- @Override
- public void printscore(ArrayList<Student> students) {
- System.out.println("======班级成绩======");
- double sum = 0;
- for (int i = 0; i < students.size(); i++) {
- Student s = students.get(i);
- sum += s.getScore();
- }
- double ave = sum/students.size();
- System.out.println(ave);
- System.out.println("------------------------------");
- }
- }
- package test2;
-
- import java.util.ArrayList;
-
- public class Scheme2 implements Scheme{
- //1.打印全部学生信息(+统计男女人数);
- //2.打印学生(去掉最值后的)成绩平均分(挑出最高分,最低分);
-
- @Override
- public void printinfo(ArrayList<Student> students) {
- System.out.println("====班级学生全部信息===");
- int countman = 0,countwoman = 0;
- for (int i = 0; i < students.size(); i++) {
- Student s = students.get(i);
- if (s.getSex() == '男') countman++;
- else countwoman++;
- System.out.println("姓名:" + s.getName());
- System.out.println("性别:" + s.getSex());
- System.out.println("分数:" + s.getScore());
- System.out.println("-------------------------");
- }
- System.out.println("男生人数为:" + countman);
- System.out.println("女生人数为:" + countwoman);
- }
-
- @Override
- public void printscore(ArrayList<Student> students) {
- System.out.println("======班级成绩======");
- double sum = 0;
- double max = students.get(0).getScore(),min = students.get(0).getScore();
-
- for (int i = 0; i < students.size(); i++) {
- Student s = students.get(i);
- sum += s.getScore();
- if(max<s.getScore()) max = s.getScore();
- if (min<s.getScore()) min = s.getScore();
- }
- sum -= (max+min);
- double ave = sum/(students.size()-2);
- System.out.println(ave);
- System.out.println("------------------------------");
- }
- }
5.准备测试类Test:创建学生管理对象
- package test2;
-
- public class Test {
- public static void main(String[] args) {
- Class command = new Class();
- command.info();
- command.scores();
- }
- }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。