19、20、22章 ●●●●● ●●●● ●●。●●
19、20、22章
●●●●● ●●●● ●●● 对象的可见性 ●●● ●●●● ●可见性是对象引用其它对象的能力 ●可见性与范围,或者成为作用域问题有关: 某一资源是否在另一个资源的范围之内 四种可见性 属性可见性:B是A的属性。 参数可见性:B是A中方法的参数 局部可见性:B是A中方法的局部对象 全局可见性:B具有某种方式的全局可见性
对象的可见性 ⚫ 可见性是对象引用其它对象的能力 ⚫ 可见性与范围,或者成为作用域问题有关: 某一资源是否在另一个资源的范围之内。 ⚫ 四种可见性 ⚫ 属性可见性:B是A的属性。 ⚫ 参数可见性:B是A中方法的参数。 ⚫ 局部可见性:B是A中方法的局部对象。 ⚫ 全局可见性:B具有某种方式的全局可见性
●●●●● ●●●● ●●● 属性可见性 ●●● ●●●● 当B作为A的属性时,则存在A到B的属性可见性。 class Register public void enterltem(itemID, gty) private ProductCatalog catalog s desc= catalog. getProduct Desc(itemID enterltem Productcatalog (itemID, quantit desc= getProductDesc( itemID
属性可见性 : Register enterItem (itemID, quantity) : ProductCatalog desc = getProductDesc( itemID ) public void enterItem(itemID, qty) { ... desc = catalog.getProductDesc(itemID) ... } class Register { ... private ProductCatalog catalog; ... } ⚫ 当B作为A的属性时,则存在A到B的属性可见性
●●●●● ●●●● ●●● 参数可见性 ●●● ●●●● ●当B作为参数传递给A的方法时,则存在A到B的 参数可见性。 enterltem(id, gty) 2: make Lineltem(desc, aty) Register sale 1: desc= getProductDesc(id) 2.1: create(desc, qty) Product atalog makeLineltem(ProductDescription desc, int gty) sI: SalesLineltem sl= new SalesLineltem(desc, gty)
参数可见性 enterItem 2: makeLineItem(desc, qty) (id, qty) 1: desc = getProductDesc(id) 2.1: create(desc, qty) :Register :Sale :Product Catalog sl : SalesLineItem makeLineItem(ProductDescription desc, int qty) { ... sl = new SalesLineItem(desc, qty); ... } ⚫ 当B作为参数传递给A的方法时,则存在A到B的 参数可见性
●●●●● ●●●● 局部可见性 ●●● ●●● ●●●● ●当B声明为A中方法的局部对象时,则存在A到B 的局部可见性 enterltem(id, qty) /local visibility via assignment of returning object ProductDescription desc= catalog. getProductDes(id) enterltem egister ProductCatalog ( itemID, quantity) desc= getProductDesc( itemID)
局部可见性 : Register enterItem (itemID, quantity) : ProductCatalog desc = getProductDesc( itemID ) enterItem(id, qty) { ... // local visibility via assignment of returning object ProductDescription desc = catalog.getProductDes(id); ... } ⚫ 当B声明为A中方法的局部对象时,则存在A到B 的局部可见性