實驗五 類與對象
實驗目的
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");
//
// }
//
//}