java的類和對象

來源:互聯網
上載者:User

標籤:style   str   UI   建立   author   scanner   div   健康   ann   

建立狗狗類:

 1 /** 2  * 狗狗類 3  * @author Administrator 4  * 5  */ 6 public class Dog { 7     String name="無名氏";  //姓名 8     int health=0;        //健康值 9     int love=0;        //親密度度10     String strain="聰明的拉布拉多犬";   //品種11     12     /**13      * 輸出狗狗資訊14      */15     public void print(){16         System.out.println("寵物自白:\n我的名字叫"+this.name+",健康值為"+this.health+",親密度為"+this.love+"我是一隻"+this.strain);17     }18 }

建立企鵝類:

 1 /** 2  * 企鵝類,使用靜態常量 3  * @author Administrator 4  * 5  */ 6 public class Penguin { 7     String name="無名氏";   //名字 8     int health=0;       //健康值 9     int love=0;        //親密度10     final String SEX_MALE="Q仔";  11     final String SEX_FEMALE="Q妹";12     String sex=SEX_MALE;   //性別13 14     public void print(){15         System.out.println("寵物自白:\n我的名字叫"+this.name+",健康值為"+this.health+",親密度為"+this.love+",我是一隻" +this.sex);16     }17    }

通過測試類別來建立具體的寵物對象資訊:

 1 import java.util.*; 2 public class Test { 3     public static void main(String[] args) { 4         Scanner input=new Scanner(System.in); 5         System.out.println("歡迎您來到寵物店"); 6         System.out.println("請輸入要領養的寵物名字:"); 7         String name=input.next(); 8         System.out.println("請選擇要領養的寵物類型:(1.狗狗  2.企鵝)"); 9         int coolse=input.nextInt();10         switch (coolse) {11         case 1:12             //選擇狗狗13             System.out.println("請選擇狗狗的品種:(1.聰明的拉布拉多犬 2.酷酷的雪納瑞)");14             String strain=null;15             if (input.nextInt()==1) {16                 strain="聰明的拉布拉多犬";17             }18             else{19                 strain="酷酷的雪納瑞";20             }21             //建立狗狗對象並賦值22             Dog dog=new Dog();23             dog.name=name;24             dog.health=100;25             dog.love=100;26             dog.strain=strain;27             dog.print();28             break;29         case 2:30             //選擇企鵝31             System.out.println("請選擇企鵝的性別:(1.Q仔  2.Q妹)");32             String sex=null;33             if (input.nextInt()==1) {34                 sex="Q仔";35             }36             else{37                 sex="Q妹";38             }39             //建立企鵝對象並賦值40             Penguin pg=new Penguin();41             pg.name=name;42             pg.sex=sex;43             pg.health=100;44             pg.love=100;45             pg.print();46             break;47         default:48             System.out.println("請輸入正確的選項");49             break;50         }51     }52 53 }

執行結果如:

 

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.