Qhov Tseem Ceeb Qhov txawv - qhov no vs super hauv Java
Cov ntsiab lus 'qhov no' thiab 'super' yog siv hauv Java programming. Cov lus tseem ceeb no tsis tuaj yeem siv los ua qhov hloov pauv lossis lwm lub npe cim. Java txhawb nqa Object Oriented Programming (OOP). Cov kev pab cuam lossis software tuaj yeem ua qauv siv cov khoom. Cov khoom yog insatiate siv cov chav kawm. Ib tug ncej ntawm OOP yog qub txeeg qub teg. Nws muab code reusability. Cov chav kawm uas twb muaj lawm yog superclasses, thiab cov chav kawm muab tau yog subclasses. Cov lus tseem ceeb super tuaj yeem siv los xa cov khoom ntawm superclass. Muaj ntau yam khoom hauv qhov system. Lo lus 'qhov no' yog siv los xa cov khoom tam sim no. Qhov sib txawv tseem ceeb ntawm qhov no thiab super yog 'qhov no' yog qhov sib txawv siv siv los xa cov khoom tam sim no thaum 'super' yog qhov sib txawv siv tau siv los xa cov khoom superclass tam sim no.
Qhov no yog dab tsi hauv Java?
Lo lus tseem ceeb 'qhov no' yog siv los xa cov khoom tam sim no. Xa mus rau Java program.
Daim duab 01: Java program siv lo lus tseem ceeb no
Hauv Java, muaj peb hom kev hloov pauv. Lawv yog cov piv txwv variables, local variables thiab class variables. Raws li cov kev pab cuam saum toj no, cov neeg ua haujlwm hauv chav kawm muaj ob qhov piv txwv sib txawv. Lawv yog id thiab npe. Local variables yog cov variables belongs rau txoj kev. Class variables yog sib koom los ntawm txhua yam khoom. Tus ID thiab lub npe raug xa mus rau tus neeg ua haujlwm tsim qauv. Yog tus programmer sau id=id; nws yuav tsis pib qhov piv txwv piv txwv vim tias Constructor twb muaj id thiab npe. Tsis muaj qhov muaj nuj nqis rau cov piv txwv piv txwv. Yog li, luam tawm lawv yuav tso tawm null. Thaum siv qhov no, nws hais txog cov khoom tam sim no. Yog li ntawd, muab id thiab lub npe rau tus tsim qauv tuaj yeem teeb tsa cov piv txwv sib txawv.
Lo lus tseem ceeb 'qhov no' tuaj yeem siv los hu rau cov qauv hauv chav kawm tam sim no. Xa mus rau qhov muab java program.
public class ThisDemo{
public static void main(String args){
Myclass myClass=new Myclass();
myClass. B();
}
}
class Myclass{
public void A(){
System.out.println(“A”);
}
public void B(){
System.out.prinltn(“B”);
this. A();
}
}
chav kawm Myclass muaj ob txoj hauv kev. Lawv yog txoj kev A thiab B. Thaum tsim ib yam khoom ntawm Myclass thiab invoking tus qauv B yuav luam B, A raws li cov zis. Hauv txoj kev B, tom qab luam ntawv B muaj nqe lus raws li qhov no. A(). Siv qhov no, cov txheej txheem hauv chav kawm tam sim no raug hu.
Nws tseem tuaj yeem siv lo lus tseem ceeb no los hu rau tus tsim chav kawm tam sim no. Xa mus rau qhov kev pab cuam.
public class ThisDemo{
public static void main(String args){
A obj=new A(5);
}
}
class A{
public A(){
System.out.println(“Constructor A”);
}
public A(int x){
this();
System.out.println(“Parameterized Constructor A”);
}
}
Raws li cov kev pab cuam saum toj no, chav kawm A muaj tus tsim qauv ua ntej thiab tus qauv tsim qauv. Thaum tsim ib yam khoom ntawm A, parameterized constructor hu ua. Nyob rau hauv parameterized constructor, muaj ib nqe lus zoo li no(); Nws yuav hu rau tus tsim chav kawm tam sim no uas yog A().
Dab tsi yog qhov super hauv Java?
lo lus tseem ceeb 'super' muaj feem xyuam rau qub txeeg qub teg. Inheritance yog lub ntsiab lus tseem ceeb ntawm Object Oriented Programming. Nws tso cai rau siv cov khoom thiab cov txheej txheem ntawm cov chav kawm uas twb muaj lawm mus rau chav kawm tshiab. Cov chav kawm uas twb muaj lawm yog hu ua niam txiv chav lossis superclass. Cov chav kawm tshiab no hu ua me nyuam chav los yog subclass.
The 'super' yog qhov sib txawv ntawm kev siv uas yog siv los xa cov khoom hauv chav kawm niam txiv tam sim. Lub ntsiab lus tseem ceeb tuaj yeem xa mus rau cov niam txiv tam sim hauv chav kawm piv txwv hloov pauv lossis hu rau niam txiv tam sim hauv chav kawm. Tus super() yog siv los hu tam sim niam txiv chav tsim tsim.
Xav tias muaj ob chav kawm li A thiab B. Chav A yog superclass thiab B yog subclass. Chav A, B ob leeg muaj cov qauv qhia.
public class A{
public void display(){
System.out.println(“A”);
}
}
public class B txuas ntxiv A{
public void display(){
System.out.println(“B”);
}
}
Thaum tsim ib yam khoom ntawm hom B thiab hu rau cov qauv tso saib, nws yuav muab cov zis B. chav kawm A muaj cov qauv tso saib, tab sis nws yog overridden los ntawm subclass B zaub txoj kev. Yog tias tus programmer xav hu rau cov qauv qhia hauv chav kawm A, ces nws tuaj yeem siv lo lus super. Xa mus rau Java program.
Daim duab 02: Java program siv super lo lus tseem ceeb
Raws li cov kev pab cuam saum toj no, chav kawm A muaj tus lej hloov pauv nrog tus nqi 10. Chav B txuas ntxiv A thiab muaj cov npe sib txawv nrog tus nqi 20. Feem ntau, thaum tsim ib yam khoom ntawm hom B thiab hu rau cov qauv qhia. yuav tsum muab tus lej hauv subclass vim tias tus nqi superclass yog overridden los ntawm chav kawm tshiab. Los ntawm kev siv super.num, tus lej superclass tus nqi yog luam tawm.
Tus super() tuaj yeem siv los hu rau tus tsim qauv superclass. Xa mus rau qhov program hauv qab no.
public class Main {
public static void main(String args){
B obj=new B();
}
}
class A{
A(){
System.out.println(“A”);
}
}
class B txuas A{
B(){
super();
System.out.println(“B”);
}
}
Raws li cov kev kawm saum toj no, chav kawm A muaj tus tsim A (). Chav B muaj tus tsim B (). Chav B txuas ntxiv chav kawm A. Thaum tsim ib yam khoom ntawm hom B, nws yuav luam A, B raws li cov zis. Tus B () constructor muaj super (). Yog li ntawd, thawj tus tsim A raug hu thiab tom qab ntawd mus rau B. Txawm hais tias super () tsis sau, los ntawm lub neej ntawd tus niam txiv tsim tus tsim hu ua.
Tus super siv txoj hauv qab no.
Daim duab 03: Java program uas hu rau tus txheej txheem superclass
Raws li cov kev pab cuam saum toj no, chav kawm A muaj cov qauv qhia. Chav B kuj muaj cov txheej txheem tso saib. Chav B txuas ntxiv A. Thaum tsim ib yam khoom ntawm hom B thiab hu rau cov qauv zaub yuav muab tso tawm raws li A thiab B. Nyob rau hauv chav kawm B zaub hom, chav kawm A zaub qauv hu ua siv super.display(). Txoj kev ntawd luam "A" ua ntej. Ces sau “B”.
Dab tsi yog qhov zoo sib xws ntawm qhov no thiab super?
Ob leeg yog lo lus tseem ceeb hauv Java programming
Qhov txawv ntawm qhov no thiab super yog dab tsi?
This vs Super |
|
Tus 'qhov no' yog qhov sib txawv siv siv los xa cov khoom tam sim no. | The 'super' yog qhov sib txawv siv siv los xa cov khoom superclass tam sim ntawd. |
Instance Variable | |
Ib chav piv txwv tam sim no hloov pauv tuaj yeem raug xa mus siv qhov no. | Superclass piv txwv hloov pauv tuaj yeem raug xa mus siv super. |
Class Method | |
Tus txheej txheem chav kawm tam sim no tuaj yeem thov siv qhov no. | Superclass txoj kev tuaj yeem raug hu siv super. |
Tus tsim | |
Tus tsim chav kawm tam sim no tuaj yeem thov siv qhov no (). | Superclass constructor tuaj yeem raug hu siv super(). |
Summary – this vs super in Java
Cov ntsiab lus 'qhov no' thiab 'super' yog siv hauv Java. Cov lus tseem ceeb tsis tuaj yeem siv los ua qhov sib txawv lossis lwm lub npe tus cim. Lawv zoo li qub, tab sis lawv muaj qhov sib txawv. Qhov txawv ntawm qhov no thiab super yog tias super yog qhov sib txawv siv siv los xa cov khoom superclass tam sim ntawd thaum qhov no yog qhov sib txawv ntawm cov khoom siv tam sim no.
Download PDF ntawm no vs super hauv Java
Koj tuaj yeem rub tawm PDF version ntawm kab lus no thiab siv nws rau offline lub hom phiaj raws li daim ntawv ceeb toom. Thov rub tawm PDF version ntawm no: Qhov txawv ntawm qhov no thiab super hauv Java