使用接口实现手机功能------完善

  1. 实现思路
  2. 编写类及接口
  3. 编写测试类、让手机播放音频、发信息和通电话
  4. 让智能手机上网、播放视频、照相、发信息和通电话
    在这里插入图片描述
TheakePictures接口
//照相
 public interface TheakePictures {
   		 void takePictures();
		}``

//连接网络
NetWork接口
public interface NetWork {
    void netWorkConn();
}

PlayWiring 接口
//播放
public interface PlayWiring {
    void play();
/**
 * 手机抽象类
 */
public abstract  class Handset {
    private  String brand;
    private  String type;

    public abstract void sendInfo();
    public abstract void call();
    public abstract void info();
    public String getBrand() {
        return brand;
    }

    public void setBrand(String brand) {
        this.brand = brand;
    }

    public String getType() {
        return type;
    }

    public void setType(String type) {
        this.type = type;
    }
    public abstract void show();
}
/**
 * 智能手机类
 */
public class AptitudeHandset extends Handset implements NetWork,TheakePictures,PlayWiring{
    @Override
    public void sendInfo() {
        System.out.println(this.getBrand()+this.getType()+"发信息");
    }

    @Override
    public void call() {
        System.out.println(this.getBrand()+this.getType()+"打电话");
    }

    @Override
    public void info() {
        System.out.println(this.getBrand()+this.getType()+"收信息");
    }

    @Override
    public void netWorkConn() {
        System.out.println(this.getBrand()+this.getType()+"上网");
    }

    @Override
    public void play() {
        System.out.println(this.getBrand()+this.getType()+"播放视频");
    }

    @Override
    public void takePictures() {
        System.out.println(this.getBrand()+this.getType()+"照照片");
    }
    public void show(){
        this.netWorkConn();
        this.call();
        this.sendInfo();
        this.takePictures();
        this.play();


    }
}
/**
 * 普通手机类
 */
public class CommonHandset extends Handset implements PlayWiring {
    @Override
    public void sendInfo() {
        System.out.println(this.getBrand()+this.getType()+"手机发信息");
    }

    @Override
    public void call() {
        System.out.println(this.getBrand()+this.getType()+"手机打电话");
    }

    @Override
    public void info() {
        System.out.println(this.getBrand()+this.getType()+"手机收信息");
    }

    @Override
    public void play() {
        System.out.println(this.getBrand()+this.getType()+"手机播放视频");
    }

    @Override
    public void show() {
        this.call();
        this.sendInfo();
        this.play();
    }
}

import java.util.Scanner;

/**
 * 装配手机类
 */
public class Host {
    Scanner sc=new Scanner(System.in);

    int brandId,typeId;//手机品牌 手机型号

    public Handset select(int type){
        Handset handset;
        if(type==1){
            /**
             * 实现智能手机功能
             */

            handset=new AptitudeHandset();
            System.out.println("1、小米 2、华为、 3、苹果");
            System.out.println("请选择手机品牌:");
            brandId=sc.nextInt();
            switch (brandId){
                case 1:
                    //设置手机品牌
                    handset.setBrand("小米");
                    // System.out.println(aptitudeHandset.getBrand());
                    System.out.println("1、红米 2、小米note 3、小米8");
                    System.out.println("请选择小米手机类型");
                    typeId=sc.nextInt();
                    //设置小米手机类型
                    if(typeId==1){
                        handset.setType("红米");
                        }else if (typeId==2){
                                handset.setType("小米note");
                            }else {
                                handset.setType("小米8");
                            }
                break;
                case 2:
                    handset.setBrand("华为");
                    System.out.println("1、荣耀  2、nava  3、华为10");
                    System.out.println("请选择华为手机类型");
                    typeId=sc.nextInt();
                    //设置小米手机类型
                    if(typeId==1){
                         handset.setType("荣耀 ");
                    }else if (typeId==2){
                         handset.setType("nava");
                    }else {
                         handset.setType("华为10");
                          }
                break;
                default:
                    handset.setBrand("苹果");
                    System.out.println("1、iphone7  2、iphoneX  3、iphone9");
                    System.out.println("请选择华为手机类型");
                    typeId=sc.nextInt();
                    //设置小米手机类型
                    if(typeId==1){
                        handset.setType("iphone7 ");
                    }else if (typeId==2){
                        handset.setType("iphoneX");
                    }else {
                        handset.setType("iphone9");
                    }
            break;
            }
        }else{
            /**
             * 实现普通手机功能
             */
            handset=new CommonHandset();
            System.out.println("1、诺基亚 2、金立手机 3、三星");
            System.out.println("请选择普通手机品牌");
            brandId=sc.nextInt();
            switch (brandId){
                case 1:
                    //设置手机品牌
                    handset.setBrand("诺基亚");
                    System.out.println("1、210黑色直板 2、105老人备用机 3、3.1plus移动版");
                    System.out.println("请选择诺基亚手机类型");
                    typeId=sc.nextInt();
                    if (typeId==1){
                        handset.setType("210黑色直板");
                    }else if(typeId==2){
                        handset.setType("105老人备用机");
                    }else {
                        handset.setType("3.1plus移动版");
                    }
                    break;
                case 2:
                    handset.setBrand("金立");
                    System.out.println("1、语音王 2、A350");
                    System.out.println("请选择金立手机类型");
                    typeId=sc.nextInt();
                    if(typeId==1){
                        handset.setType("语音王");
                    }else {
                        handset.setType("A350");
                    }
                    break;
                default:
                    handset.setBrand("三星");
                    System.out.println("1、B289电信 2、E1150老人机");
                    System.out.println("请选择三星手机类型");
                    typeId=sc.nextInt();
                    if(typeId==1){
                        handset.setType("B289电信");
                    }else {
                        handset.setType("E1150老人机");
                    }
                    break;
            }

        }
        return  handset;
    }
}
import java.util.Scanner;

/**
 * 测试类
 */
public class Test {
    public static void main(String[] args) {
        Scanner sc=new Scanner(System.in);
        Host host=new Host();
//        AptitudeHandset aptitudeHandset=new AptitudeHandset();
//        CommonHandset commonHandset=new CommonHandset();
        Handset handset;

        System.out.println("1、智能手机 2、普通手机");
        System.out.println("请选择手机类型:");
        int chiooce=sc.nextInt();

        handset=host.select(chiooce);
        handset.show();
    }
}

猜你喜欢

转载自blog.csdn.net/qq_35327989/article/details/89332079
今日推荐