關於JAVA中形參的詳解

來源:互聯網
上載者:User

標籤:

在JAVA中形式參數主要有基本類型,類名,抽象類別名,介面名。下面我們來各自講述。

1.基本類型 這個太簡單不做簡述。

2.類名

  類名:需要該類的對象

 1 class Student { 2     public void study() { 3         System.out.println("Good Good Study,Day Day Up"); 4     } 5 } 6  7 class StudentDemo { 8     public void method(Student s) { //ss; ss = new Student();  Student s = new Student(); 9         s.study();10     }11 }12 13 class StudentTest {14     public static void main(String[] args) {15         //需求:我要測試Student類的study()方法16         Student s = new Student();17         s.study();18         System.out.println("----------------");19         20         //需求2:我要測試StudentDemo類中的method()方法21         StudentDemo sd = new StudentDemo();22         Student ss = new Student();23         sd.method(ss);24         System.out.println("----------------");25         26         //匿名對象用法27         new StudentDemo().method(new Student());28     }29 }

  3.抽象類別名:需要該類的子類對象

abstract class Person {    public abstract void study();}class PersonDemo {    public void method(Person p) {//p; p = new Student();  Person p = new Student(); //多態        p.study();    }}//定義一個具體的學生類class Student extends Person {    public void study() {        System.out.println("Good Good Study,Day Day Up");    }}class PersonTest {    public static void main(String[] args) {        //目前是沒有辦法的使用的        //因為抽象類別沒有對應的具體類        //那麼,我們就應該先定義一個具體類        //需求:我要使用PersonDemo類中的method()方法        PersonDemo pd = new PersonDemo();        Person p = new Student();        pd.method(p);    }}

4.介面名:需要該介面的實作類別對象

和抽象類別名很相似

 1 //定義一個愛好的介面 2 interface Love { 3     public abstract void love(); 4 } 5  6 class LoveDemo { 7     public void method(Love l) { //l; l = new Teacher();  Love l = new Teacher(); 多態 8         l.love(); 9     }10 }11 12 //定義具體類實現介面13 class Teacher implements Love {14     public void love() {15         System.out.println("老師愛學生,愛Java,愛林青霞");16     }17 }18 19 class TeacherTest {20     public static void main(String[] args) {21         //需求:我要測試LoveDemo類中的love()方法22         LoveDemo ld = new LoveDemo();23         Love l = new Teacher();24         ld.method(l);25     }26 }

 

關於JAVA中形參的詳解

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.