0
点赞
收藏
分享

微信扫一扫

面向对象(六)

1、Annotation注解

注解(Annotation)是从jdk5.0开始引入的,在代码中以“@注解名”的形式存在。例如:

@override

@Deprecated 表示过时了的结构,但是依旧可以使用

@SupperessWarnings(values=“unchecked”) 抑制编译器警告

注解可以修饰的结构:注解可以像访问权限修饰符那样,修饰包、类、构造器、方法、属性等等。

1.1、元注解:

@Target 表示注解可以修饰的结构,表范围

@Retention 表示生命周期

2、单元测试的使用

测试分类:

黑盒测试:不需要写代码,给输入值,看程序能否输出预期值

白盒测试:需要写代码,关注程序具体的执行流程。

2.1、JUnit单元测试

3、包装类和包装类与基本数据类型的转换

3.1、包装类的作用:让基本数据类型也可以像一个类那么调用一些只能由类对象作为参数的方法,即让基本数据类型具有类的特征。

基本数据类型的包装类基本是首字母大写,但有两个特别的:

int Integer

char Character

3.2、包装类与基本数据类型的转换

3.2.1、基本数据类型转换为包装类:

public class Test {
    public static void main(String[] args) {
        int num = 520;
        //已过时的转换方式
        Integer integer = new Integer(num);
        System.out.println(integer.toString());

        //也可以使用字符串转换成Double类型,注意此时的字符串内容必须要为一个数字
        String s1 = "13.14";
        Double dou = new Double(s1);
        System.out.println(dou.toString());

        //推荐使用下面的方法
        Integer integer1 = Integer.valueOf(num);
        System.out.println(integer.toString());

    }
}

3.2.2、包装类转基本数据类型

    public static void test02(){
        int num = 10;
        Integer integer = Integer.valueOf(num);
        //包装类转换为基本数据类型
        int num1 = integer.intValue();
        //输出11
        System.out.println(++num1);
    }

3.3、包装类的自动装箱和自动拆箱

    public static void test03(){
        //自动封箱
        int a = 10;
        Integer integer = a;
        System.out.println(a);

        //自动拆箱
        Integer integer1 = Integer.valueOf(20);
        int b = integer1;
        System.out.println(b);

    }
4、String类和基本数据类型的转换

4.1、基本数据类型转String类型:

int a = 10; String str1 = String.valueOf(a);

int b = 20; String str2 = b + "";

4.2、String类型转基本数据类型:

 public static void test05(){
        String num1 = "123";
        //调用包装类中的方法进行转换
        int num = Integer.parseInt(num1);
        System.out.println(num);
    }

==注意:包装类和Python中的共用变量池相似,即在存入某个范围内的数据时会共用一片内存空间,当超出这个范围时,就不再共用一片内存空间,而是各有一片内存空间!!==

举报

相关推荐

0 条评论