0x00:
java中数据类型的整理
//code1
short s1=1;
s1=s1+1;
//code2
short s1= 1;
s1+=1;
在code1的代码中我们发现会报错,ide提醒我们将s1的类型设置为int或者s1=(short)s1+1;这样强制转换。
但是在code2的代码中缺不报错,这是因为类似于这种结构的已经进行了一步强制转化。
0X01:
在java面向对象中static关键字的使用:
1.java对象中static属性的使用
package head_first;
class Book1{
private String title;
private double price;
private static String pub = "sss";
public Book1(String title,double price) {
this.title = title;
this.price = price;
}
public static void setPub(String p) {
pub = p;
}
public String getInfo() {
return "title:"+this.title+"price:"+this.price+"pub:"+this.pub;
}
}
public class Exp394 {
public static void main(String[] args) {
// TODO Auto-generated method stub
Book1.setPub("qqq");
Book1 bk3 = new Book1("java",12.3);
System.out.println(bk3.getInfo());
}
}
运行结果:title:javaprice:12.3pub:qqq
事实证明:在java的面向对象中,我们能在没有实例化对象之前使用static的方法,而只要使用 类.方法 的格式就ok。
2.java中static的内部类的使用
package head_first;
class Outer{
private String msg = "abc";
class Inner{
public void print() {
System.out.println(Outer.this.msg);
}
}
}
public class Exp395 {
public static void main(String[] args) {
// TODO Auto-generated method stub
Outer.Inner in = new Outer().new Inner();
in.print();
}
}
package head_first;
class Outer{
private static String msg = "abc";
class Inner{
public void print() {
System.out.println(Outer.msg);
}
}
}
public class Exp395 {
public static void main(String[] args) {
// TODO Auto-generated method stub
Outer.Inner in = new Outer().new Inner();
in.print();
}
}
package head_first;
class Outer{
private static String msg = "abc";
static class Inner{
public void print() {
System.out.println(Outer.msg);
}
}
}
public class Exp395 {
public static void main(String[] args) {
// TODO Auto-generated method stub
Outer.Inner in = new Outer.Inner();
in.print();
}
}
0X02
不用static声明方法
package head_first;
public class Exp395 {
public static void main(String[] args) {
// TODO Auto-generated method stub
fun();
}
public static void fun() {
System.out.println("x");
}
}
package head_first;
public class Exp395 {
public static void main(String[] args) {
// TODO Auto-generated method stub
new Exp395().fun();
}
public void fun() {
System.out.println("x");
}
}