黑马程序员技术交流社区

标题: 一个完整的手机类 [打印本页]

作者: MengYa    时间: 2015-8-31 23:10
标题: 一个完整的手机类
class Demo3_Phone {
        public static void main(String[] args) {
                Phone ph=new Phone();                //创建对象
                ph.setBrand("apple");       
                ph.setPrice(6800);
                System.out.println("brand="+ph.getBrand()+"  "+"price="+ph.getPrice());
                ph.show();                                        //仅仅是显示(再用brand或者price时就无法再调用)

                System.out.println("-----------------------------");
                Phone ph1=new Phone("三星",5000);
                ph1.show();
               
                ph.call();                       
                ph.sendMessage();
                ph.playGame();
        }
}
class Phone {                       
        private String brand;                        //封装: 属性私有化
        private int price;
       
        public Phone(){                                        //无参构造(无返回值类型)
        }
         
        public Phone(String brand ,int price){                        //有参构造
                        this.brand=brand;
                        this.price=price;
         }
       
        public void setBrand(String brand){                //设置品牌
                        this.brand=brand;                                               
        }
       
        public String getBrand(){                                        //获得当前对象属性设定的品牌                       

                        return brand;
        }
       
        public void setPrice(int price){
                        this.price=price;
        }
       
        public int getPrice(){
                        return price;
        }
         
       
        public void show(){                                //显示方法
                System.out.println("手机品牌是:"+brand+"   "+"价格是:"+price);
         }

        public void call(){                                                //类中特有方法
        System.out.println("打电话");
        }
       
        public void sendMessage(){
        System.out.println("发信息");
        }
       
        public void playGame(){
        System.out.println("打游戏");
        }
}

作者: 15074907819    时间: 2015-8-31 23:17
我的天都这么专业,我也要抓紧去努力了!




欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/) 黑马程序员IT技术论坛 X3.2