Design a mobile phone type Phone by analyzing the actual mobile phone. This category includes the basic attributes of mobile phones (such as brand brand, model type, operating system os, etc.) and basic functions (such as: display device information inf

实验五 类与对象
实验目的
1.熟练掌握如何自定义一个类。
2.熟练掌握如何创建对象以及调用对象的成员变量和成员方法。
3.掌握方法调用时参数的传递。
主要仪器设备及耗材
安装了 JDK1.8 的 PC 一台
实验内容
1. 通过对现实中手机进行分析,设计一个手机类 Phone。该类包括手机的基本属性(如:
品牌 brand、型号 type、操作系统 os 等)和基本功能(如:显示设备信息 info( )、拨
号 call( String number)等))。另外,编写类 TestPhone 创建手机类 Phone 的对象,
并测试各项功能。
public class Phone{
【补充代码】
}
public class TestPhone{
【补充代码】
}

 

Phone.java

package com.temp;

import java.util.Scanner;

/**
 * @Author lanxiaofang
 * @email [email protected]
 * @date 2020/09/29 00:25
 */
public class Phone {

    private static String brand; //品牌
    private static String type; //型号
    private static String os; //操作系统

    public Phone() {
    }

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

    public static void setType(String type) {
        Phone.type = type;
    }

    public static void setOs(String os) {
        Phone.os = os;
    }


    private static final String REGEX_MOBILE = "^[1][3,4,5,6,7,8,9][0-9]{9}$";

    public static void info(){
        System.out.println("--品牌: " + brand + "\n--型号: " + type + "\n--操作系统: " + os);
    }

    public static void call(String number){

        Scanner sca = new Scanner(System.in);
        System.out.println("--请输入对方11位手机号:");
        String number_other = sca.nextLine();

        boolean number_regex = number_other.matches(REGEX_MOBILE); // 验证输入的手机号的合法性

        if(number_regex){
            System.out.println(number + " 正在打电话给 " + number_other);
            if(number.equals(number_other)){
                System.out.println("--请拨打的电话正在通话中,请稍后再拨~");
            }
        }else{
            System.out.println("--您输入的手机号有误,请重新输入...");
        }


    }


}

TestPhone.java

package com.temp;

/**
 * @Author lanxiaofang
 * @email [email protected]
 * @date 2020/09/29 00:33
 */
public class TestPhone{

    public static void main(String[] args) {

        Phone phone = new Phone();

        phone.setBrand("Honor 10");
        phone.setType("COL-AL10");
        phone.setOs("Android 10");

        phone.info();
        phone.call("13212660403");

    }

}


//public class TestPhone extends Phone{
//
//    public static void main(String[] args) {
//
//        Phone.setBrand("Honor 10");
//        Phone.setType("COL-AL10");
//        Phone.setOs("Android 10");
//
//        Phone.info();
//        Phone.call("13212660403");
//
//    }
//
//}

 

Guess you like

Origin blog.csdn.net/c_lanxiaofang/article/details/108860807