開始學習java 網址http://how2j.cn?p=33637


這個網站還挺好使的

http://how2j.cn?p=33637

Day1

熟悉了一下類,屬性,方法的概念

模擬了一下英雄這個類,包括有護甲,血量,移速這些屬性,然后英雄又有類似於加血加速,超神這種行為,稱作方法

代碼如下

 1 package how2j;
 2 
 3 public class Hero {
 4     String name;//英雄名
 5     float hp;//血量
 6     int armor;//護甲值
 7     int movespeed;//移速
 8     int deathtimes;//死亡次數
 9     int killtimes;//擊殺次數
10     int helptimes;//助攻次數
11     int money;//金錢
12     int budao;//補刀數
13     float gongsu;//攻速
14     String killword;//擊殺台詞
15     String deathword;//死亡台詞
16     
17     int getarmor() {
18         return armor;
19     }//定義方法 獲取護甲值
20     
21      void keng() {
22         System.out.println("坑隊友!");
23     }//定義方法 坑()
24     
25     void addspeed(int speed) {
26         movespeed = movespeed + speed;
27     }//定義方法 加速(加速的值)
28     
29     void legendary() {
30         System.out.println("Legendary!");
31     }//定義方法 超神
32     
33     float getHp() {
34         return hp; 
35     }//定義方法 返回當前生命值
36     void recovery(float blood) {
37        hp = hp + blood; 
38     }//定義方法 加血(增加的血量)
39     
40     public static void main(String[] args) {
41         // TODO Auto-generated method stub
42         Hero dog = new Hero();
43         dog.name = "狗頭";
44         dog.hp = (float) 10.5;
45         dog.armor = 35;
46         dog.movespeed = 345;
47         dog.killtimes = 9;
48         dog.deathtimes = 0;
49         dog.helptimes = 0;
50         dog.money = 4399;
51         dog.budao = 200;
52         dog.gongsu = 0.47f;
53         dog.killword = "lay down baster!";
54         dog.deathword = "I want to live 500 years more...";
55         //定義屬性
56         dog.addspeed(100);
57         dog.keng();
58         //使用  方法
59         System.out.println("hp:" + dog.getHp());
60         dog.recovery(30.44f);
61         System.out.println("hp:" + dog.getHp());
62         System.out.println("speed" +dog.movespeed );
63         ;
64         dog.legendary();
65         System.out.println(dog.killword);
66     }
67 
68 }

 變量的類型轉換

 1 public class HelloWorld {
 2 
 3     public static void main(String[] args) {
 4         // TODO Auto-generated method stub
 5         System.out.println("Hello World!");
 6         byte a = 8;
 7         int i1 = 10;
 8         int i2 = 300;
 9         System.out.println(Integer.toBinaryString(i2));
10         //查看整數對應的二進制的值,先看看i2的二進制
11         
12         a = (byte)i1;
13         System.out.println(a);
14         //低精度向高精度轉換可以
15         
16         a = (byte)i2;
17         System.out.println(a);
18         //高精度向低精度,由於低精度位數不夠,byte只能存八位
19         //先都變成了二進制數值,100101100,九位
20         //所以轉換后只留八位成了00101100,等於十進制的44
21         System.out.println(Integer.toBinaryString(i2));     
22     }
23 
24 }
強制轉換后果

Hello World!
100101100
10
44
100101100

兩個short相加成了什么?

 1 public class HelloWorld {
 2 
 3     public static void main(String[] args) {
 4         // TODO Auto-generated method stub
 5         System.out.println("Hello World!");
 6         short a = 1;
 7         short b = 2;
 8         //short c =  a + b;這樣會報錯,提示不能從int轉成short,說明兩個short相加成了int
 9         short c = (short)(a+b);
10         //需要這樣強轉一下
11         int d = a + b;
12         //或者這樣直接定義int
13         System.out.println(c);
14         System.out.println(d);
15     }
16 
17 }
short + short = int?

  


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM