Qhov Sib Txawv Ntawm Chav Kawm thiab Piv Txwv Cov Piv Txwv

Cov txheej txheem:

Qhov Sib Txawv Ntawm Chav Kawm thiab Piv Txwv Cov Piv Txwv
Qhov Sib Txawv Ntawm Chav Kawm thiab Piv Txwv Cov Piv Txwv

Video: Qhov Sib Txawv Ntawm Chav Kawm thiab Piv Txwv Cov Piv Txwv

Video: Qhov Sib Txawv Ntawm Chav Kawm thiab Piv Txwv Cov Piv Txwv
Video: Saib Mis paub Poj niam (hluas nkauj) tus yam ntxwv lub siab 2024, Hlis ntuj nqeg
Anonim

Qhov Tseem Ceeb Qhov Sib Txawv - Chav Kawm vs Instance Variables

Cov lus programming niaj hnub no txhawb nqa Object Oriented Programming. Ib yam khoom muaj cov ntaub ntawv lossis cov yam ntxwv. Ib yam khoom muaj qee yam cwj pwm. Lawv hu ua txoj kev. Ib qho kev pab cuam tuaj yeem ua qauv siv cov khoom. Ib tug software yog ib tug sau ntawm cov kev pab cuam. Yog li ntawd, ib qho software tuaj yeem tsim thiab siv tau los ntawm cov khoom siv. Cov khoom sib cuam tshuam nrog cov txheej txheem. Object-Oriented Programming txhim kho code modularity thiab reusability. Yuav tsum muaj chav kawm tsim khoom. Ib chav kawm yog ib daim phiaj los tsim ib yam khoom. Yog li ntawd, ib qho khoom yog ib qho piv txwv ntawm chav kawm. Hauv programming, cov ntaub ntawv yuav tsum tau khaws cia. Cov ntaub ntawv khaws cia hauv qhov chaw nco. Cov chaw nco no hu ua variables. Ib tus tswv cuab sib txawv yog qhov sib txawv uas cuam tshuam nrog ib qho khoom tshwj xeeb. Nws yog siv tau rau tag nrho nws txoj kev. Muaj ob hom kev sib txawv ntawm cov tswv cuab uas yog cov sib txawv hauv chav kawm thiab cov piv txwv sib txawv. Qhov sib txawv tseem ceeb ntawm cov chav kawm thiab cov piv txwv sib txawv yog tias, yog tias tsuas muaj ib daim qauv ntawm qhov sib txawv sib koom nrog txhua qhov piv txwv ntawm chav kawm, cov kev hloov pauv no yog hu ua class variables thiab yog tias txhua qhov piv txwv ntawm chav kawm muaj nws tus kheej daim qauv ntawm qhov sib txawv, ces. cov variables no hu ua instance variables.

Clas Variables yog dab tsi?

Thaum tsuas muaj ib daim qauv ntawm qhov sib txawv sib qhia nrog txhua qhov piv txwv ntawm chav kawm, cov kev hloov pauv no raug hu ua class variables. Class variables yog variables tshaj tawm nyob rau hauv lub chav kawm sab nraum txhua txoj kev. Cov kev hloov pauv no muaj cov ntsiab lus zoo li qub. Cov kev hloov pauv no cuam tshuam nrog cov chav kawm, tsis yog rau cov khoom.

Qhov sib txawv ntawm Chav Kawm thiab Cov Piv Txwv Variables
Qhov sib txawv ntawm Chav Kawm thiab Cov Piv Txwv Variables

Daim duab 01: Chav Kawm Variables thiab Piv Txwv Cov Variables

Saib cov lej hauv qab no nrog cov chav kawm sib txawv.

public class Employee {

public static int id;

public zoo li qub nyiaj hli ob npaug;

}

public class Test {

public static void main(string args){

Employee e1=tshiab Employee();

Employee e2=tshiab Employee();

}

}

Raws li cov kev pab cuam saum toj no, e1 thiab e2 yog cov neeg ua haujlwm hom khoom. Ob leeg yuav muaj tib daim qauv ntawm kev nco. Yog tias e1.id=1 thiab luam ntawv e2.id kuj tseem yuav muab tus nqi 1. Nws tuaj yeem luam tawm tus lej ID thiab cov nyiaj hli uas siv cov npe neeg ua haujlwm xws li Cov Neeg Ua Haujlwm.id, Employee.salary etc.

Qhov piv txwv yog dab tsi?

Thaum txhua qhov piv txwv ntawm chav kawm muaj nws tus kheej daim qauv ntawm qhov sib txawv, ces cov kev hloov pauv no hu ua piv txwv hloov pauv. Xa mus rau qhov program hauv qab no.

public class Employee {

public int id;

public ob lub nyiaj hli;

}

public class Test{

public static void main(string args){

Employee e1=tshiab Employee();

e1.id=1;

e1.salary=20000;

Employee e2=tshiab Employee();

e2.id=2;

e2. nyiaj hli=25000;

}

}

Nyob rau hauv qhov kev pab cuam loj, e1 thiab e2 yog hais txog cov khoom ntawm cov neeg ua haujlwm. Nws muaj peev xwm muab qhov tseem ceeb rau id thiab nyiaj hli siv tus neeg teb xov tooj xws li e1.id, e1. nyiaj hli thiab lwm yam. Tus id thiab cov nyiaj hli nyob rau hauv chav kawm Employee yog hu ua piv txwv variables. Cov e1 thiab e2 yog cov khoom sib cais. Txhua yam khoom yuav muaj ib daim qauv ntawm cov piv txwv sib txawv. Lub e1 yuav muaj nyias tus lej thiab nyiaj hli thiab e2 yuav muaj tus lej cais thiab nyiaj hli. Yog li, cov piv txwv hloov pauv tau tsim thaum qhov khoom lossis qhov piv txwv raug tsim.

Dab tsi yog qhov Zoo sib xws ntawm Chav Kawm thiab Cov Piv Txwv Cov Piv Txwv?

  • Ob leeg yog hom kev hloov pauv.
  • Ob qhov sib txawv nyob hauv ib chav kawm tab sis sab nraum ib txoj kev.

Dab tsi yog qhov txawv ntawm Chav Kawm thiab Cov Piv Txwv Cov Piv Txwv?

Class Variables vs Instance Variables

Class variables yog variables nyob rau hauv uas tsuas muaj ib daim qauv ntawm cov variable qhia rau tag nrho cov piv txwv ntawm chav kawm. Instance variables yog qhov sib txawv thaum txhua qhov piv txwv ntawm chav kawm muaj nws tus kheej daim qauv ntawm qhov sib txawv.
koom haum
Class variables yog txuam nrog chav kawm. Instance variables yog txuam nrog cov khoom.
Class variables tsim ib daim qauv rau txhua yam khoom. Instance variables tsim nyias daim qauv rau txhua yam khoom.
Cov lus tseem ceeb
Class variables yuav tsum muaj cov ntsiab lus zoo li qub. piv txwv piv txwv tsis tas yuav muaj lo lus tshwj xeeb xws li zoo li qub.

Summary – Class vs Instance Variables

Object-oriented programming yog qhov kev ua haujlwm loj. Nws pab ua qauv software siv cov khoom. Cov khoom raug tsim los ntawm cov chav kawm. Object creation kuj hu ua instantiation. Ib chav kawm muab ib daim phiaj los tsim ib yam khoom. Ib tus tswv cuab sib txawv yog qhov sib txawv uas cuam tshuam nrog ib qho khoom tshwj xeeb. Nws yog siv tau rau tag nrho nws txoj kev. Muaj ob hom kev sib txawv ntawm cov tswv cuab xws li, chav kawm sib txawv thiab cov piv txwv sib txawv. Qhov sib txawv ntawm chav kawm thiab cov piv txwv sib txawv yog tias, yog tias tsuas muaj ib daim qauv ntawm qhov sib txawv sib koom nrog txhua qhov piv txwv ntawm chav kawm, cov kev hloov pauv no yog hu ua chav kawm hloov pauv thiab yog tias txhua qhov piv txwv ntawm chav kawm muaj nws tus kheej daim qauv ntawm qhov sib txawv, ces cov variables hu ua instance variables.

Download the PDF Version of Class vs Instance Variables

Koj tuaj yeem rub tawm PDF version ntawm kab lus no thiab siv nws rau offline hom phiaj raws li daim ntawv ceeb toom. Thov rub tawm PDF version ntawm no: Qhov Sib txawv Ntawm Chav Kawm thiab Cov Piv Txwv Cov Piv Txwv

Pom zoo: