Qhov Tseem Ceeb Qhov Sib Txawv - Static Binding vs Dynamic Binding
Programming lus xws li Java thiab C txhawb nqa Object Oriented Programming (OOP). Nws tso cai tsim software siv cov khoom. Muaj ntau ntau yam khoom hauv software system lossis ib qho program. Cov khoom no muaj cov cwj pwm thiab cov txheej txheem. Cov cwj pwm piav qhia cov yam ntxwv. Cov txheej txheem piav qhia cov kev ua uas tuaj yeem ua los ntawm cov khoom. Cov ntaub ntawv yog dhau los ntawm cov khoom siv txoj kev. Cov nqi yuav tsum tau xa los ntawm txoj kev hu nrog cov tsis muaj. Txoj kev siv tiag tiag yog nyob rau hauv txoj kev txhais. Muaj kev sib txuas ntawm txoj kev hu thiab txoj kev txhais. Nws yog lub npe hu ua kev khi. Muaj ob hom kev khi. Lawv yog static binding thiab dynamic binding. Qhov sib txawv tseem ceeb ntawm kev sib khi zoo li qub thiab kev sib khi zoo li qub yog tias, hauv kev sib khi zoo li qub, qhov kev sib khi tau raug daws thaum lub sijhawm sib sau ua ke thaum lub sijhawm sib txuas dynamic tau daws thaum lub sijhawm khiav, uas yog lub sijhawm ua tiav. Kab lus no tham txog qhov sib txawv ntawm ob txoj kev sib khi no.
Dab tsi yog kev khi ruaj khov?
Kev khi yog qhov sib txuas ntawm txoj kev hu thiab txoj kev txhais.
Daim duab 01: Static Binding thiab Dynamic Binding
Saib cov program hauv qab no sau hauv Java.
public class A{
public void method1(){
System.out.println(“Method1”);
}
public void method2(){
System.out.println(“Method2”);
}
public static void main(String args){
A obj=new A();
obj.method1();
obj.method2();
}
}
Raws li cov kev pab cuam saum toj no, ib yam khoom ntawm hom A raug tsim. Ces method1 thiab method2 hu ua. Kev txheeb xyuas txoj kev twg yuav tsum hu rau kev ua tiav yog hu ua kev khi. Nqe lus obj.method1() yuav hu method1() thiab obj.method2() yuav hu method2(). Qhov txuas no yog khi.
Nyob rau hauv kev sib khi zoo li qub, kev sib khi yog daws thaum lub sijhawm sau los ntawm tus sau. Nws tseem hu ua thaum ntxov khi. Kev khi tau tshwm sim ua ntej qhov kev zov me nyuam ua tiav. Static binding tshwm sim nyob rau hauv txoj kev overloading. Xa mus rau qhov program hauv qab no sau hauv Java.
public void xam laib{
public void sum(int x, int y){
System.out.println(“Sum is “, x+y);
}
public void sum(ob x, ob y){
System.out.println(“Sum is “, x+y);
}
public static void main(String args){
Calculation cal=new Calculation();
cal.sum(2, 3);
cal.sum(5.1, 6.4);
}
}
Raws li cov kev pabcuam saum toj no, thaum dhau ob tus lej, tus qauv nrog ob tus lej yuav raug hu. Thaum dhau ob qhov tseem ceeb, txoj kev sib raug rau ob qhov txiaj ntsig ob npaug yuav raug hu. Txoj kev khi no tshwm sim thaum lub sijhawm muab tso ua ke. Lub compiler paub tias nws yuav tsum hu rau txoj kev sum nrog ob tus lej tus nqi rau cal.sum(2, 3). Rau cal(5.1, 6.4), nws yuav hu rau txoj kev suav nrog ob tus nqi ob. Tag nrho cov ntaub ntawv xav tau yog paub ua ntej lub sijhawm ua haujlwm, yog li nws ua kom cov haujlwm ua haujlwm tau zoo thiab ua tiav nrawm.
Dynamic Binding yog dab tsi?
Nyob rau hauv Dynamic Binding lub compiler tsis daws qhov khi ntawm lub sijhawm sau. Kev khi tau tshwm sim thaum lub sijhawm ua haujlwm. Nws tseem hu ua lig khi. Dynamic Binding tshwm sim nyob rau hauv txoj kev overriding. Xa mus rau qhov program sau hauv Java.
public class Shape(){
public void draw(){
System.out.println(“Kho duab”);
}
}
public class Circle() extends Shape{
public void draw(){
System.out.println(“Kos lub voj voog”);
}
}
public class Triangle() extends Shape{
public void draw(){
System.out.println("Kaw daim duab peb sab");
}
}
public class Test{
public static void main(String args){
Shape s;
s=New Shape();
s.draw();
s=new Circle();
s.draw();
s=tshiab daim duab peb sab();
s.draw();
}
}
Raws li cov kev pab cuam saum toj no, chav kawm Shape muaj ib txoj kev kos(). Class Circle thiab class Triangle extends Shape class. Class Circle thiab class Triangle tuaj yeem tau txais cov cwj pwm thiab cov txheej txheem ntawm chav kawm Shape. Yog li, chav kawm Shape yog chav kawm super lossis chav kawm niam txiv. Class Circle thiab Class Triangle yog cov chav kawm sub lossis cov chav kawm muab los. Cov chav kawm no kuj muaj kev kos() txoj kev nrog lawv tus kheej siv. Yog li ntawd, txoj kev kos() hauv chav kawm super yog overridden.
Hauv txoj kev tseem ceeb, cov khoom sib txawv raug hu. Muaj ib qho kev siv sib txawv ntawm hom Shape, uas yog s. Tom qab ntawd, s invokes tus qauv raws li cov chav kawm tshwj xeeb. Thaum lub sij hawm sau, lub compiler tsuas yog xa mus rau super class draw method. Thaum qhov kev ua tiav tiag tiag pib, nws yuav ua rau kev ua tiav ntawm ntau txoj kev kos. Ua ntej, s yuav taw tes rau qhov khoom ntawm hom Shape. Yog li ntawd, nws yuav hu rau txoj kev kos hauv Shape class. Tom qab ntawd tus s yuav tau taw qhia cov khoom ntawm hom Circle, thiab nws yuav hu rau txoj kev kos ntawm lub voj voog chav kawm. Thaum kawg, s yuav raug xa mus rau cov khoom ntawm hom Daim duab peb sab, thiab nws yuav hu rau txoj kev kos hauv daim duab peb sab. Txawm hais tias qhov kev siv sib txawv yog hom Shape, qhov kev sib khi tshwm sim nyob ntawm seb hom khoom. Lub tswvyim no hu ua Dynamic Binding. Cov ntaub ntawv yog muab thaum lub sijhawm khiav, yog li qhov kev ua tiav qeeb qeeb piv rau kev sib khi zoo li qub.
Dab tsi yog qhov zoo sib xws ntawm Static Binding thiab Dynamic Binding?
Ob leeg no muaj feem xyuam nrog polymorphism uas tso cai rau ib qho khoom coj los ua ntau txoj hauv kev
Dab tsi yog qhov txawv ntawm Static Binding thiab Dynamic Binding?
Static Binding vs Dynamic Binding |
|
Static Binding yog hom kev khi uas sau tag nrho cov ntaub ntawv xav tau los hu rau lub sijhawm ua haujlwm. | Dynamic Binding yog hom kev khi uas sau tag nrho cov ntaub ntawv xav tau los hu rau lub sijhawm ua haujlwm. |
Lub Sijhawm Sib Tw | |
Static Binding tshwm sim ntawm lub sijhawm sau. | Dynamic binding tshwm sim thaum lub sijhawm khiav. |
Static Binding siv hom ntaub ntawv los khi. | Dynamic Binding siv cov khoom los daws qhov khi. |
Yam khoom tiag tiag | |
Kev khi ruaj khov tsis siv qhov khoom tiag los khi. | Dynamic binding, siv qhov khoom tiag los khi. |
Synonyms | |
Static binding tseem hu ua thaum ntxov khi. | Dynamic binding tseem hu ua lig khi. |
Kev ua tiav | |
Kev ua kom nrawm nrawm nrawm hauv kev khi zoo li qub. | Kev ua kom nrawm nrawm yog qis hauv kev sib khi zoo. |
Example | |
Static binding yog siv nyob rau hauv txoj kev overloading. | Dynamic binding yog siv rau hauv txoj kev overriding. |
Summary – Static Binding vs Dynamic Binding
Muaj qhov txuas ntawm txoj kev hu thiab txoj kev txhais. Nws yog lub npe hu ua kev khi. Muaj ob hom kev khi hu ua static binding thiab dynamic binding. Qhov sib txawv ntawm static binding thiab dynamic binding yog hais tias nyob rau hauv static binding, lub binding yog daws nyob rau hauv lub compile lub sij hawm thaum lub sij hawm dynamic binding yog daws thaum lub sij hawm khiav, uas yog lub sij hawm tiag tiag ntawm kev ua. Raws li cov ntaub ntawv xav tau yog muab ua ntej lub sijhawm ua haujlwm, kev khi zoo li qub tau nrawm hauv kev ua tiav piv rau dynamic binding.
Download the PDF of Static Binding vs Dynamic Binding
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 qhov sib txuas ruaj khov thiab Dynamic Binding