Internship Training--java Foundation (4)
1 polymorphic
Polymorphism is the ability of the same behavior to have many different manifestations or forms.
Polymorphism is the same interface, using different instances to perform different operations, polymorphism is the embodiment of various forms of objects.
In reality, for example, we press the F1 key for this action:
- If the current pop-up in the Flash interface is the help document as 3;
- If the word help is currently popping up under word;
- Windows Help and support pops up under Windows.
The same event occurs on different objects and produces different results.
The advantages of polymorphism
- 1. Eliminate the coupling between types
- 2. Replaceable
- 3. Extensibility
- 4. Interface
- 5. Flexibility
- 6. Ease of
Three necessary conditions for polymorphic existence
- Inherited
- Rewrite
- Parent class reference to child class object
Like what:
New Child ();
When calling a method using polymorphic mode, first check whether the method is in the parent class, if not, compile the error, and if so, call the subclass's same name method.
The benefits of polymorphism: The program can be well extended, and the objects of all classes can be universally handled.
Here is a demonstration of a polymorphic instance, with a detailed description of the note:
Abstract class Animal { abstractvoid eat ();}
class Cat extends Animal {@Override void eat () { System.out.println ( eat fish " void work () {System.out.println ( "Catch Mouse"
class extends Animal { publicvoid eat () { System.out.println ("Eat Bones"); } Public void Work () { System.out.println ("housekeeping");} }
Public classTest { Public Static voidMain (string[] args) {Show (NewCat ());//call the Show method with a Cat objectShowNewDog ());//call the Show method with a Dog objectAnimal a=NewCat ();//Upward TransformationA.eat ();//The Eat of the Cat is calledCat C = (cat) A;//Down TransformationC.work ();//The catchmouse of the Cat is called } Public Static voidShow (Animal a) {a.eat (); //Type judgment if(AinstanceofCat) {//things that cats doCat C =(Cat) A; C.work (); } Else if(AinstanceofDOG) {//What the dog does.Dog C =(Dog) A; C.work (); } }}
2 QQ Mailbox Send mail
Importjava.security.GeneralSecurityException;Importjava.util.Properties;ImportJavax.mail.Authenticator;ImportJavax.mail.Message;Importjavax.mail.MessagingException;Importjavax.mail.PasswordAuthentication;Importjavax.mail.Session;ImportJavax.mail.Transport;Importjavax.mail.internet.InternetAddress;ImportJavax.mail.internet.MimeMessage;Importcom.sun.mail.util.MailSSLSocketFactory; Public classSendEmail { Public Static voidMain (String [] args)throwsgeneralsecurityexception {//recipient e-mailString to = "[Email protected]"; //Sender e-mailString from = "[Email protected]"; //specifies that the host sending the message is smtp.qq.comString host = "smtp.qq.com";//QQ Mail Server//Get System PropertiesProperties Properties =system.getproperties (); //setting up a mail serverProperties.setproperty ("Mail.smtp.host", host); Properties.put ("Mail.smtp.auth", "true"); Mailsslsocketfactory SF=Newmailsslsocketfactory (); Sf.settrustallhosts (true); Properties.put ("Mail.smtp.ssl.enable", "true"); Properties.put ("Mail.smtp.ssl.socketFactory", SF); //get the default Session objectSession session = Session.getdefaultinstance (properties,NewAuthenticator () { Publicpasswordauthentication getpasswordauthentication () {return NewPasswordauthentication ("[Email protected]", "************");//Sender Mail user name, password, * * * is an authorization password, not a login password } }); Try{ //to create a default MimeMessage objectMimeMessage message =NewMimeMessage (session); //Set from: Header fieldMessage.setfrom (Newinternetaddress (from)); //Set to: Header fieldMessage.addrecipient (Message.RecipientType.TO,Newinternetaddress (to)); //Set Subject: Head header fieldMessage.setsubject ("This is the Subject line!"); //set the message bodyMessage.settext ("This is actual message"); //Send Messagetransport.send (message); System.out.println ("Sent message Successfully....from runoob.com"); }Catch(Messagingexception Mex) {mex.printstacktrace (); } }}
Beginner's Tutorial
Internship Training--java Foundation (4)