Reja: Visibility (kirishni boshqarish sinf a'zolari)
Download 28.82 Kb.
|
1 2
Bog'liqVisibility
- Bu sahifa navigatsiya:
- Sinf, paket va pastki sinfga korinadi. Sinf sinfiga misolni korib chiqaylik
Reja: Visibility (kirishni boshqarish sinf a'zolari) Paket ko'rinishi Umumiylik Visibility (kirishni boshqarish sinf a'zolari) Shaxsiy ko'rinish xususiy ko'rinish o'zgaruvchiga faqat uning sinfi tomonidan kirish imkonini beradi. Ular ko'pincha jamoatchilik bilan birgalikda ishlatiladi oluvchilar va o'rnatuvchilar. class SomeClass { private int variable; public int getVariable() { return variable; } public void setVariable(int variable) { this.variable = variable; }} public class SomeOtherClass { public static void main(String[] args) { SomeClass sc = new SomeClass(); // These statement won't compile because SomeClass#variable is private: sc.variable = 7; System.out.println(sc.variable); // Instead, you should use the public getter and setter: sc.setVariable(7); System.out.println(sc.getVariable()); }} Sinf, paket va pastki sinfga ko'rinadi. Sinf sinfiga misolni ko'rib chiqaylik public class Test{ public int number = 2; public Test(){ }} Endi sinfning namunasini yaratishga harakat qilaylik. Ushbu misolda biz raqamga kirishimiz mumkin, chunki u ommaviydir. public class Other{ public static void main(String[] args){ Test t = new Test(); System.out.println(t.number); } Hech qanday modifikatorsiz paket ko'rinishi standart hisoblanadi. Java hujjatlaridan, "[paketning ko'rinishi] ko'rsatadi sinf bilan bir xil paketdagi sinflar (ularning ota-onasidan qat'iy nazar) a'zoga kirish huquqiga egami." javax.swing dan bu misol, package javax.swing; public abstract class JComponent extends Container … { static boolean DEBUG_GRAPHICS_LOADED;} DebugGraphics is in the same package, so DEBUG_GRAPHICS_LOADED is accessible. package javax.swing; public class DebugGraphics extends Graphics { static { JComponent.DEBUG_GRAPHICS_LOADED = true; }} Himoyalangan ko'rinish sabablari bu a'zoning har qanday quyi sinflari bilan birga paketiga ko'rinib turishini bildiradi. package com.stackexchange.docs; public class MyClass{ protected int variable; //This is the variable that we are trying to access public MyClass(){ variable = 2;};} Endi biz bu sinfni kengaytiramiz va uning himoyalangan aʼzolaridan biriga kirishga harakat qilamiz. package some.other.pack; import com.stackexchange.docs.MyClass; public class SubClass extends MyClass{ public SubClass(){ super(); System.out.println(super.variable); }} Himoyalangan a'zoga, agar siz unga o'sha joydan kirayotgan bo'lsangiz, uni kengaytirmasdan ham kirishingiz mumkin bo'ladi paket. E'tibor bering, bu modifikator faqat sinf a'zolarida ishlaydi, sinfning o'zida emas Download 28.82 Kb. Do'stlaringiz bilan baham: |
1 2
Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©fayllar.org 2024
ma'muriyatiga murojaat qiling
ma'muriyatiga murojaat qiling