Encapsulation is said to wrap up on data and method and fun functionality (method and example variables) for external Not necessarily the object of this
My question is making a variable personal and public encryption? Or is there an encapsulation to create a class with abstract?
For example: To create a switch class I have switched (electric switch) class on or off I have used abstraction and I have used the intuitive switch class to explain that I Motor or bulb or any electrical device can map
public class switch {private boolean isOff = true; Private iSwitch Listener listener; Public Switch (iSwitch Listener Listener) {this.listener = Listener; } Public Zero Trigger () {isOff =! IsOff; If (isOff) {listener.off (); } And {listener.on (); }}} The public square bulb implies the Iswish Listener {@ Override Public Wide On () {// TODO Auto-Generated Method Stub System. Out.printline ("bulb shining"); } @ Override Public Wide Off () {// Tudo Auto-Generated Math Stub System. Out.printline ("bulb does not shine"); }} Public Interface ISwitchListener {Public at zero (); Public (from zero); } Public category executives {public static zero main (string [] args) {// Todo auto-generated method stub switch swt = new switch (new bulb ()); Swt.trigger (); }} If I am not using intangible here, then I will have a class like below
switch to public class {private boolean isOff = true ; Public Zero Trigger () {isOff =! IsOff; Bulb b = new bulb (); If (iceoff) is {BO (); } And {bøn}; }}} When I want to map the motor to the switch, I need to convert the class to blood
public class switch {Private boolean isOff = true; Public Zero Trigger () {isOff =! IsOff; Bulb b = new bulb (); If (iceoff) is {BO (); } And {bøn}; }}} Public class motor {at public zero () {// Tudo auto-generated method stub system. Out.printLN ("Motor is moving"); } Public closed with zero () {// TODO auto generated method stub System.out.println ("turning motor to rotate"); In general, no, intangible and encapsulation are two different things.}}
special As an abstract, the intangible means is to remove the statement that is unnecessary for the intended purpose of the code or model. For example, if you write a program to calculate how much color you need for a home, then your model of the house needs to be included in the surface area, but to include the address, or size of the yard is not needed. Encapsulation means to hide the internal functioning of an object or module, such as the coupling between objects can be controlled and easily changed. If there is no encapsulation, then the object's clients can directly reference their internal elements, which means that you have to change all the clients to modify the object.
Regarding the code example, I think the better solution would be to create a "switchable" interface and apply the motor and bulb to it. Such as:
switchable to public interface {zero (on); From zero (); } Public class motor applies Switchable {public void on () {System.out.println ("Motor is moving"); } Public Zero Closure () {System.out.println ("Closing to rotate the motor"); }} Public class switch {private boolean isOff = true; Private switchable switchable; Public Switch (switchable switch) {ie switchable = switchable; } Public Zero Trigger () {isOff =! IsOff; If (isOff) {switchable.off (); } And {switchable.on (); }}} Hope that helps.
No comments:
Post a Comment