java知识点整理(持续更新)

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");
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值