开始学习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?

  

posted @ 2018-02-19 15:47  Mrfri  阅读(1256)  评论(0编辑  收藏  举报