(Memories from last season:
1. The polymorphism of the target
2. Generic classes and interfaces
Top knowledge points this season:
Practical use of generic classes and interfaces
1. The subclass target can be considered as the instantiation of the parent class target, and the methods to be called in the future are the methods that have been overridden in the subclass.
2. You can use this feature to observe the use of general classes, because there are many general methods in general classes.
general class
abstractclassA
{
publicabstractvoidfun();
};
classBextendsA
{
publicvoidfun()
{
System.out.println(B==gt;HelloWorld);
}
};
claSSCextendsA
{
publicvoidfun()
{
System.out.println(C==gt;HelloWorld);
}
};
publicclassDemo01
{
publicstaticvoidmain(Stringargs)
{
Aa=newB();
a.fun();
}
};
abstractclassA
{
publicabstractvoidfun();
};
classBextendsA
{
publicvoidfun()
{
System.out.println(B==gt;HelloWorld);
}
};
claSSCextendsA
{
publicvoidfun()
{
System.out.println(C==gt;HelloWorld);
}
};
publicclassDemo01
{
publicstaticvoidmain(Stringargs)
{
Aa=newC();
a.fun();
}
};
Generic classes are instantiable and instantiated through the polymorphism of the target
What is the primary function of the general class? The general class is similar to a template operation ==> JAVAWEBServlet program, which provides a template.
Turn the above practical example into a program
abstractclassErr
{
publicvoidprintInfo()
{
System.out.println(name: this.getName());
System.out.println(class: this.getCls());
System.out.println(reason: this.getCau());
}
//Get the name, it is done by the detailed subclass
publicabstractStringgetName();
//Get the class, which is done by the detailed subclass
publicabstractStringgetCls();
// get the reason
publicabstractStringgetCau();
}
classZhangSanextendsErr
{
publicStringgetName()
{
return Zhang San;
}
publicStringgetCls()
{
return the fifth class;
}
publicStringgetCau()
{
return was caught by the teacher because he ate tools in class, so he had to fill out a disciplinary card. ;
}
}
classLiSiextendsErr
{
publicStringgetName()
{
return Li Si;
}
publicStringgetCls()
{
return the fifth class;
}
publicStringgetCau()
{
return was caught by the teacher because he slept in class, so he had to fill out a disciplinary card. ;
}
}
publicclassDemo02
{
publicstaticvoidmain(Stringargs)
{
Erre=newZhangSan();
e.printInfo();
}
}
Let's change it to Li Si and see the effect~
abstractclassErr
{
publicvoidprintInfo()
{
System.out.println(name: this.getName());
System.out.println(class: this.getCls());
System.out.println(reason: this.getCau());
}
//Get the name, it is done by the detailed subclass
publicabstractStringgetName();
//Get the class, which is done by the detailed subclass
publicabstractStringgetCls();
// get the reason
publicabstractStringgetCau();
}
classZhangSanextendsErr
{
publicStringgetName()
{
return Zhang San;
}
publicStringgetCls()
{
return the fifth class;
}
publicStringgetCau()
{
return was caught by the teacher because he ate tools in class, so he had to fill out a disciplinary card. ;
}
}
classLiSiextendsErr
{
publicStringgetName()
{
return Li Si;
}
publicStringgetCls()
{
return the fifth class;
}
publicStringgetCau()
{
return was caught by the teacher because he slept in class, so he had to fill out a disciplinary card. ;
}
}
publicclassDemo02
{
publicstaticvoidmain(Stringargs)
{
Erre=newLiSi();
e.printInfo();
}
}
Calling the template is successful~
(Template Design) Scenario:
Assuming that Person is divided into Worker and Student, the attributes of workers include: name, age, and salary, and the attributes of students include: name, age, and achievements, so now everyone can speak, but workers and students must speak differently. At this moment, the way of speaking among people must be fixed, it must be a common way, only the content of the speech is different.
abstractclassPerson
{
privateStringname;
privateintage;
publicPerson(Stringname,intage)
{
this.name=name;
this.age=age;
}
publicStringgetName()
{
returnthis.name;
}
publicintgetAge()
{
returnthis.age;
}
publicvoidtalk()
{
//This is to say, the content is in this method
System.out.println(this.getContent());
}
publicabstractStringgetContent();
}
classStudentextendsPerson
{
privatefloatscore;
publicStudent(Stringname,intage,floatscore)
{
super(name, age);
this.score=score;
}
publicStringgetContent()
This document includes the following attachments:
[Zero Basic Learning JAVA] JavaSE Target-Oriented Part-17. Target-Oriented Advanced (05).doc)