• /* объект орқали методни чақириш */
  • Encapsulation
  • Inheritance
  • Ўзгарувчи ва методларга мурожаат




    Download 4,84 Mb.
    bet7/11
    Sana25.05.2024
    Hajmi4,84 Mb.
    #253860
    1   2   3   4   5   6   7   8   9   10   11
    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();
    }
    }

    Download 4,84 Mb.
    1   2   3   4   5   6   7   8   9   10   11




    Download 4,84 Mb.

    Bosh sahifa
    Aloqalar

        Bosh sahifa



    Ўзгарувчи ва методларга мурожаат

    Download 4,84 Mb.