package com.mashibing.interfaceDemo3;
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;
}
}
Handset handset; 就是定义一个手机对象。
初始化的话,你可以用:
Handset handset=new AptitudeHandset(); //创建智能手机对象
Handset handset=new CommonHandset(); //创建普通手机对象
Handset 是AptitudeHandset的父类。
Handset是CommonHandset()的父类。
类似 int a;
定义一个对象变量,使用前要用new给它初始化