Access

0
0
1840 days ago, 532 views
PowerPoint PPT Presentation
void birthday( ) { age = age 1; } void growOlder( ) { birthday( ); } Equivalently, you ... on the off chance that (this.age < 75) this.birthday();/

Presentation Transcript

Slide 1

Get to

Slide 2

Overview Questions secured in this discussion: How would we get to fields and techniques? Why have entry limitations? What can have admittance confinements? How would we give or limit get to?

Slide 3

Instance and class factors You can announce factors inside a class These factors are called occurrence factors , or fields Every protest of that class has its own duplicate of those fields The fields portray something about the question You can likewise proclaim static factors inside a class There is one and only of every static variable A static variable is additionally called a class variable The static variable depicts something about the class all in all

Slide 4

Method factors You can pronounce factors inside a strategy or inside a constructor These are called technique factors , not fields Method factors are fundamentally utilized for calculation Method factors are entirely brief, and are utilized just inside that technique When a technique returns (finishes), every one of its factors are disposed of

Slide 5

Example: a "Rabbit" class Rabbit { static int populace;/class variable (tallies Rabbits) twofold appetite;/case variable twofold dread;/occasion variable twofold bravery = 0.75;/case variable void eat() { twofold temp;/technique variable temp = valor * hunger; if (temp > fear) { System.out.println("Eating!"); hunger = hunger - 1; }

Slide 6

Statements You can announce factors inside a class or inside a technique or a constructor You can put explanations (executable code) just inside strategies and constructors, not inside a class Declarations with instatements are still affirmations, not proclamations

Slide 7

Statements must be in strategies (or in constructors) class Rabbit { twofold craving;/OK- - presentation twofold dread = 5.0;/OK- - still an assertion hunger = 5.0;/illicit - task articulation Rabbit ( ) { hunger = 5.0;/OK—articulation in a constructor } void eat ( ) { hunger = hunger - 1;/OK—articulation in a strategy }

Slide 8

Access from inside a class Inside a class, you can get to different fields and strategies inside the class just by naming them Example: class Person { int age; void birthday( ) { age = age + 1; } void growOlder( ) { birthday( ); } Equivalently, you can utilize the watchword this : void birthday( ) { this .age = this .age + 1; } void growOlder( ) { this .birthday( ); }

Slide 9

Accessing from outside a class, 1 Outside a class (from some different class) you get to case factors and strategies by Naming the question you need to converse with Putting a speck Naming the variable or technique Example:/if NOT in class Person, say: if (john.age < 75) john.birthday(); Inside the class, the catchphrase this signifies "this question": if (this.age < 75) this.birthday();/"this" may mean john

Slide 10

Accessing from outside a class, 2 Outside a class (from some different class) you get to class factors and strategies by Naming the class you need to converse with Putting a dab Naming the variable or technique Examples: Person.population = Person.population + 1; x = Math.abs(y);

Slide 11

Responsibility In Java, items are thought to be dynamic They have practices They are in charge of their own information Data (factors) must be kept steady Example: populace ought to never be adverse all together for a class or protest be in charge of its own information, it must keep control of that information

Slide 12

Loss of control Suppose a Rabbit question, bugsBunny , has a variable named hunger Inside the class, this strategy is fine: void eat ( ) { hunger = hunger - 1; } From outside the class, the accompanying is legitimate: bugsBunny.hunger = bugsBunny.hunger - 1; But would it be advisable for us to be permitted to "reach inside" a rabbit? The class needs to shield itself from mistakes in different classes (and from vindictive conduct)

Slide 13

private factors and strategies If you pronounce a variable or strategy to be private , that variable or technique must be gotten to from inside the class private techniques additionally bode well, e.g. process() If you proclaim a variable or strategy to be open , then any code anyplace can get to it Typically, a class or question has both Methods for use by whatever remains of the program Methods and factors that only it ought to control

Slide 14

Levels of get to private - get to just from inside the class " bundle " - access from inside the class, or from any class in a similar catalog ("envelope") This is the default; there is no bundle watchword ensured - access from inside the class, or from inside any subclass, or from some other class in a similar registry open - access from anyplace by any means

Slide 15

Levels of get to, II To make a variable or technique obvious Only inside this class: private From this class and its subclasses: unrealistic From this class and its subclasses, and some other class in this index: "bundle" (default) From this subclass and its subclasses, and some other classes in this registry: shielded From anyplace: open

Slide 16

Getters and setters One approach to control get to is by means of getters and setters: class Rabbit { private twofold craving;/getter open twofold getHunger() { return hunger; }/setter open void setHunger(double hunger) { this.hunger = hunger; } This appears to be senseless, however it's much more secure and more adaptable

Slide 17

Immutable articles Suppose a Planet has a mass , and you need to have the capacity to see its mass yet not transform it: class Planet { private long mass;/Constructor Planet(long mass) { this.mass = mass; }/getter long getMass() { return mass; }/Notice there is no setter! }

Slide 18

The End

SPONSORS