10.java的接口写法,编码和解码

//1.创建接口
public interface USB {
   //接口中没有构造方法,写了就报错
    //成员常量
    public static final int NUM = 10;

    //成员方法
    //JDK7及以前的写法
    public abstract void open();
    public abstract void close();

    //JDK8多了两种写法
    public static void method1(){

    }
    public default void method2(){

    }
    //JDK9多了一种写法
    private void method3(){

    }
}
//测试接口
public class TestDemo2 {
    public static void main(String[] args) {
        System.out.println(USB.NUM);
    }
}
@Test
public void test2() throws UnsupportedEncodingException {
//将字符串进行平台默认的字符集进行编码,UTF-8编码3个字节一个汉字,GBK编码2个字节一个汉字
String s ="中国";
byte[] bys = s.getBytes();                    //使用平台默认的字符集编码
System.out.println(Arrays.toString(bys));                      //输出编码:[-28, -72, -83, -27, -101, -67]
byte[] bys1 = s.getBytes("UTF-8"); //使用UTF-8进行编码
System.out.println(Arrays.toString(bys1));                     //输出编码:[-28, -72, -83, -27, -101, -67]
byte[] bys2 = s.getBytes("GBK");   //使用GBK进行编码
System.out.println(Arrays.toString(bys2));                     //输出编码:[-42, -48, -71, -6]
//下面进行解码,编码和解码方式必须要一致,什么方法编码的就要用什么方式解码
String s1 = new String(bys); //传入平台默认的字符集编码
System.out.println(s1);    //平台默认字符集解码输出:中国
String s2 = new String(bys1,"UTF-8"); //按UTF-8进行解码
System.out.println(s2);  //输出:中国
String s3 = new String(bys2,"GBK");  //按GBK方式解码
System.out.println(s3);  //输出:中国
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

胡启行

您的鼓励将是我前进的动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值