今天学习了面向对象的特点之封装, 且敲出了一标准的话的封装案例 虽然不是啥难事 但态度还是不错的额
- class Test1 {
- public static void main(String[] args) {
- Phone p = new Phone();
- p.setBrand("小米");
- p.setPrice(98);
- p.setColur("红色");
-
-
- System.out.println(p.getBrand());
- System.out.println(p.getPrice());
- System.out.println(p.getColur());
- p.call();
- p.senMessage();
- p.playGame();
- }
- }
- class Phone {
- private String brand ;
- private int price ;
- private String colur ;
- 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 setColur (String colur){
- this.colur = colur;
- }
- public String getColur(){
- return colur ;
- }
- public void call(){
- System.out.println("打电话");
- }
- public void senMessage(){
- System.out.println("发短信");
- }
- public void playGame(){
- System.out.println("玩游戏");
- }
- }
复制代码
大家多多分享额 谢谢 |
|