创建手机类Phonejava,成员变量包括brand(品牌)、price(价格)、颜色color,成员方法包括call()(打电话)、sendMessage()(发消息)。实例化手机对象,为成员变量赋值,并调用成员方法。
时间: 2024-06-12 10:06:24 浏览: 70
Phone.java
代码如下:
```java
public class Phone {
private String brand;
private double price;
private String color;
public Phone(String brand, double price, String color) {
this.brand = brand;
this.price = price;
this.color = color;
}
public void call(String number) {
System.out.println("正在拨打电话:" + number);
}
public void sendMessage(String message, String number) {
System.out.println("正在发送短信:" + message + ",收件人:" + number);
}
public static void main(String[] args) {
Phone phone = new Phone("Apple", 6999.0, "黑色");
phone.call("10086");
phone.sendMessage("你好,今天天气不错!", "13800138000");
}
}
```
输出结果为:
```
正在拨打电话:10086
正在发送短信:你好,今天天气不错!,收件人:13800138000
```
阅读全文