Java学习笔记之封装与继承
封装 1,将属性定义为私有的( private ) 不能通过 对象名.属性 来直接访问,但是可以通过 方法 来间接的访问, 2, 封装的意义:公有属性可以被随意修改,并且不能被程序察觉。封装后,别人通过方法来访问属性时,我们可以添加限制,访问可以被程序察觉。 下面我们用封装定义一个手机类(这种类一般我们称为 javabean ) 1 public class Phone { 2 private String type; 3 private int price; 4 private String color; 5 6 public String getType(){ 7 return type; 8 } 9 public void setType(String type){ 10 this.type=type; 11 } 12 public int getPrice() { 13 return price; 14 } 15 public void setPrice(int price) { 16 this.price = price; 17 } 18 public String getColor() { 19 return color; 20 } 21 public void setColor(String color) { 22 this.color = color; 23 }