, TIJ2 

[ Pobierz całość w formacie PDF ]
.The goal of the clientprogrammer is to collect a toolbox full of classes to use for rapidapplication development.The goal of the class creator is to build a classthat exposes only what s necessary to the client programmer and keepseverything else hidden.Why? Because if it s hidden, the clientprogrammer can t use it, which means that the class creator can changethe hidden portion at will without worrying about the impact to anyoneelse.The hidden portion usually represents the tender insides of an objectthat could easily be corrupted by a careless or uninformed clientprogrammer, so hiding the implementation reduces program bugs.Theconcept of implementation hiding cannot be overemphasized.In any relationship it s important to have boundaries that are respected byall parties involved.When you create a library, you establish arelationship with the client programmer, who is also a programmer, but3I m indebted to my friend Scott Meyers for this term.Chapter 1: Introduction to Objects 35 one who is putting together an application by using your library, possiblyto build a bigger library.If all the members of a class are available to everyone, then the clientprogrammer can do anything with that class and there s no way to enforcerules.Even though you might really prefer that the client programmer notdirectly manipulate some of the members of your class, without accesscontrol there s no way to prevent it.Everything s naked to the world.So the first reason for access control is to keep client programmers handsoff portions they shouldn t touch parts that are necessary for the internalmachinations of the data type but not part of the interface that users needin order to solve their particular problems.This is actually a service tousers because they can easily see what s important to them and what theycan ignore.The second reason for access control is to allow the library designer tochange the internal workings of the class without worrying about how itwill affect the client programmer.For example, you might implement aparticular class in a simple fashion to ease development, and then laterdiscover that you need to rewrite it in order to make it run faster.If theinterface and implementation are clearly separated and protected, youcan accomplish this easily.Java uses three explicit keywords to set the boundaries in a class: public,private, and protected.Their use and meaning are quitestraightforward.These access specifiers determine who can use thedefinitions that follow.public means the following definitions areavailable to everyone.The private keyword, on the other hand, meansthat no one can access those definitions except you, the creator of thetype, inside member functions of that type.private is a brick wallbetween you and the client programmer.If someone tries to access aprivate member, they ll get a compile-time error.protected acts likeprivate, with the exception that an inheriting class has access toprotected members, but not private members.Inheritance will beintroduced shortly.Java also has a  default access, which comes into play if you don t useone of the aforementioned specifiers.This is sometimes called  friendlyaccess because classes can access the friendly members of other classes in36 Thinking in Java www.BruceEckel.com the same package, but outside of the package those same friendlymembers appear to be private.Reusing theimplementationOnce a class has been created and tested, it should (ideally) represent auseful unit of code.It turns out that this reusability is not nearly so easy toachieve as many would hope; it takes experience and insight to produce agood design.But once you have such a design, it begs to be reused.Codereuse is one of the greatest advantages that object-oriented programminglanguages provide.The simplest way to reuse a class is to just use an object of that classdirectly, but you can also place an object of that class inside a new class.We call this  creating a member object [ Pobierz całość w formacie PDF ]
  • zanotowane.pl
  • doc.pisz.pl
  • pdf.pisz.pl
  • anikol.xlx.pl