1.定义手机类:手机有品牌(brand),价格(price)和颜色(color)三个属性,有打电话call()和sendMessage()两个功能
2.具体要求:
请定义出手机类,类中要有空参、有参构造方法,set/get方法。
定义测试类,在主方法中使用空参构造创建对象,使用set方法赋值。
3.调用对象的两个功能,打印效果:
正在使用价格为3998元黑色的小米手机打电话....
正在使用价格为3998元黑色的小米手机发短信....
//类的定义
class Phone{
String brand; //声明品牌属性
int price; //声明价格属性
String color; //声明颜色属性
//定义call()方法
void call(){
System.out.println("正在使用价格为"+price+"元"+color+"的"+brand+"手机打电话");
}
//定义sendMessage()方法
void sendMessage(){
System.out.println("正在使用价格为"+price+"元"+color+"的"+brand+"手机发短信");
}
public void setPrice(int price){
this.price=price;
}//有参构造
public int getPrice(){
return price;
}//空参构造
public void setBrand(String brand){
this.brand=brand;
}
public String getBrand(){
return brand;
}
public void setColor(String color){
this.color=color;
}
public String getColor(){
return color;
}
}
public class 手机类 {
public static void main(String[] args) {
Phone phone1=new Phone();//对象的创建
phone1.setPrice(3998); //对象的使用
phone1.setBrand("小米");
phone1.setColor("黑色");
phone1.call(); //调用对象的方法
phone1.sendMessage();
}
}