手机类练习

/*定义一个类,用来模拟“手机”事物
成员变量(属性)
String brand;//品牌
double price;//价格
String color;//颜色
成员方法(行为)
public void call(String who){}//打电话
public void sendMessage(){}//群发短信
*/
public class Phone{
    //成员变量
    String brand;//品牌
    double price;//价格
    String color;//颜色
    
    //成员方法(行为)
    public void call(String who){
        System.out.println("给"+who+"打电话");
    }
    public void sendMessage(){
        System.out.println("群发短信");
    }
}

public class Demo01PhoneOne{
    public static void main(String[] args){
        //根据Phone类,创建一个名为one的对象
        //格式:类名称 对象名=new 类名称();
        Phone one=new Phone();
        System.out.println(one.brand);//null
        System.out.println(one.price);//0.0
        System.out.println(one.color);//null
        System.out.println("=============");
        
        one.brand="苹果";
        one.price=8388.0;
        one.color="黑色";
        System.out.println(one.brand);//苹果
        System.out.println(one.price);//8388.0
        System.out.println(one.color);//黑色
        System.out.println("=============");
        
        one.call("乔布斯");
        one.sendMessage();    
    }
}


posted on 2019-03-13 15:49  呀小王呀  阅读(243)  评论(0编辑  收藏  举报

导航