A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© MengYa 中级黑马   /  2015-8-31 23:10  /  323 人查看  /  1 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

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("打游戏");
        }
}

1 个回复

倒序浏览
我的天都这么专业,我也要抓紧去努力了!
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马