|
Ўзгарувчи ва методларга мурожаат
|
bet | 7/11 | Sana | 25.05.2024 | Hajmi | 4,84 Mb. | | #253860 |
Bog'liq Ma\'ruza-6Ўзгарувчи ва методларга мурожаат: /* Объект яратиш*/ - ObjectReference = new Constructor();
/* объект орқали ўзгарувчини чақириш */ - ObjectReference.variableName;
/* объект орқали методни чақириш */ - ObjectReference.MethodName();
Мисол:
public class Puppy{
int puppyAge;
public Puppy(String name){
System.out.println("Passed Name is :" + name );
}
public void setAge( int age ){
puppyAge = age;
}
public int getAge( ){
System.out.println("Puppy's age is :" + puppyAge );
return puppyAge;
}
public static void main(String []args){
Puppy myPuppy = new Puppy( "tommy" );
myPuppy.setAge( 2 );
myPuppy.getAge( );
System.out.println("Variable Value :" + myPuppy.puppyAge );
}
}
Натижа:
Passed Name is :tommy
Puppy's age is :2
Variable Value :2
Encapsulation
class Encapsulation {
private int summa = 500;
protected int smax = 45500;
public int getSumma() {
return this.summa;
}
}
public class EncapsulationDemo {
public static void main(String []argc) {
Encapsulation d = new Encapsulation();
System.out.println(d.getSumma());
}
}
Encapsulation
Encapsulation – бу синф ўзгарувчиларининг (майдонлари)
ҳимояланганлиги
Inheritance - Ворислик (inheritance) - бошқа бир синфдан ворис (насл) олиш.
- Java да насл олиш extends калит сўзидан фойдаланилади.
- Мисол:
- class Android extends MobilePhone { … }
class MobilePhone {
public void switch() {
System.out.println(“Phone is switching");
}
}
class Android extends MobilePhone {
public void switchOS() {
System.out.println(“Switching Android 4.4");
}
}
public class Demo {
public static void main(String []args) {
Android kitkat = new Android();
kitkat.switch();
kitkat.switchOS();
}
}
|
| |