實習培訓——Java基礎(4)

來源:互聯網
上載者:User

標籤:收件者   this   work   main   static   動作   寄件者   發送訊息   子類   

實習培訓——Java基礎(4)

1 多態

多態是同一個行為具有多個不同表現形式或形態的能力。

多態就是同一個介面,使用不同的執行個體而執行不同操作,多態性是對象多種表現形式的體現。

現實中,比如我們按下 F1 鍵這個動作:

  • 如果當前在 Flash 介面下彈出的就是 AS 3 的協助文檔;
  • 如果當前在 Word 下彈出的就是 Word 協助;
  • 在 Windows 下彈出的就是 Windows 協助和支援。

同一個事件發生在不同的對象上會產生不同的結果。

多態的優點
  • 1. 消除類型之間的耦合關係
  • 2. 可替換性
  • 3. 可擴充性
  • 4. 介面性
  • 5. 靈活性
  • 6. 簡化性
多態存在的三個必要條件
  • 繼承
  • 重寫
  • 父類引用指向子類對象

比如:

Parent p = new Child();

當使用多態方式調用方法時,首先檢查父類中是否有該方法,如果沒有,則編譯錯誤;如果有,再去調用子類的同名方法。

多態的好處:可以使程式有良好的擴充,並可以對所有類的對象進行通用處理。

以下是一個多態執行個體的示範,詳細說明請看注釋:

abstract class Animal {    abstract void eat();}
class Cat extends Animal {    @Override    void eat() {        System.out.println("吃魚");    }    void work() {        System.out.println("捉老鼠");    }}
class Dog extends Animal {    public void eat() {        System.out.println("吃骨頭");    }    public void work() {        System.out.println("看家");    }}
public class Test {    public static void main(String[] args) {        show(new Cat()); // 以 Cat 對象調用 show 方法        show(new Dog()); // 以 Dog 對象調用 show 方法        Animal a = new Cat(); // 向上轉型        a.eat(); // 調用的是 Cat 的 eat        Cat c = (Cat) a; // 向下轉型        c.work(); // 調用的是 Cat 的 catchMouse    }    public static void show(Animal a) {        a.eat();        // 類型判斷        if (a instanceof Cat) { // 貓做的事情            Cat c = (Cat) a;            c.work();        } else if (a instanceof Dog) { // 狗做的事情            Dog c = (Dog) a;            c.work();        }    }}

2 QQ郵箱發送郵件

import java.security.GeneralSecurityException;import java.util.Properties;import javax.mail.Authenticator;import javax.mail.Message;import javax.mail.MessagingException;import javax.mail.PasswordAuthentication;import javax.mail.Session;import javax.mail.Transport;import javax.mail.internet.InternetAddress;import javax.mail.internet.MimeMessage;import com.sun.mail.util.MailSSLSocketFactory;public class SendEmail {    public static void main(String [] args) throws GeneralSecurityException     {        // 收件者電子郵箱        String to = "[email protected]";        // 寄件者電子郵箱        String from = "[email protected]";        // 指定發送郵件的主機為 smtp.qq.com        String host = "smtp.qq.com";  //QQ 郵件伺服器        // 擷取系統屬性        Properties properties = System.getProperties();        // 設定郵件伺服器        properties.setProperty("mail.smtp.host", host);        properties.put("mail.smtp.auth", "true");        MailSSLSocketFactory sf = new MailSSLSocketFactory();        sf.setTrustAllHosts(true);        properties.put("mail.smtp.ssl.enable", "true");        properties.put("mail.smtp.ssl.socketFactory", sf);        // 擷取預設session對象        Session session = Session.getDefaultInstance(properties,new Authenticator(){            public PasswordAuthentication getPasswordAuthentication()            {                return new PasswordAuthentication("[email protected]", "************"); //寄件者郵件使用者名、密碼,****是授權密碼,不是登入密碼            }        });        try{            // 建立預設的 MimeMessage 對象            MimeMessage message = new MimeMessage(session);            // Set From: 頭部頭欄位            message.setFrom(new InternetAddress(from));            // Set To: 頭部頭欄位            message.addRecipient(Message.RecipientType.TO, new InternetAddress(to));            // Set Subject: 頭部頭欄位            message.setSubject("This is the Subject Line!");            // 設定訊息體            message.setText("This is actual message");            // 發送訊息            Transport.send(message);            System.out.println("Sent message successfully....from runoob.com");        }catch (MessagingException mex) {            mex.printStackTrace();        }    }}

 

 

 

菜鳥教程

實習培訓——Java基礎(4)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.