• 注释、注解、序列化


    一、注释
    总共三种注释,前两种编译器直接跳过,从来不阅读,第三种编译器是可以看懂的,当你使用javadoc这样的命令时会用到,用来生成api时用的
    1、 //
    2、 /* */
    3、说明注释,它以 /** 开始,以 */结束

    二、注解
    1、注解与类、接口、枚举是在同一个层次,可以成为java 的一个类型。
    2、注解是一种元数据,它是一种描述数据的数据,其作用在于提供程序本身以外的一些数据信息,
    也就是说他不会属于程序代码本身,不参与逻辑运算,故而不会对原程序代码的操作产生直接的影响。

    @Override
    public String toString() {
        return"This is String Representation of current object.";
    }

    注解有什么好处?
    1、@Override告诉编译器这是一个重写方法(描述方法的元数据),
    2、父类中不存在该方法,编译器便会报错。
    3、如果拼写错误,例如将toString()写成了toStrring(),也不用@Override注解,程序依然能编译运行,但这不是我的期望结果。

    参考来源

    大致分为三类:自定义注解、JDK内置注解、还有第三方框架提供的注解。

    public class test {
    //    自定义注解:
    //    返回值类型只能是(基本类型、Class、String、enum)
    //    如果只有1个参数且名为value,使用时可以不用name=value的格式
    //    有设置默认值可以不写,比如Annotation2的age
        @Annotation1("")
        @Annotation2(value = "",employees = {"张三"})
        @Annotation3()
        public void testAnnotation(){};
    }
    
    // 四个元注解:
    // 元注解是注解的注解
    // Target表示注解使用范围,Retention表示在什么地方还有效
    // Documented表示生成javadoc文档,Inherited表示子类可以继承父类的注解
    @Target({ElementType.TYPE,ElementType.METHOD})
    @Retention(RetentionPolicy.RUNTIME)
    @Documented
    @Inherited
    @interface Annotation1{
        String value();
    }
    
    @Target(ElementType.METHOD)
    @interface Annotation2{
        String value();
        String[] employees();
        int age() default 12;
    }
    
    @interface Annotation3{
    }

     内置注解
    1、 @Overide 重写
    2、@Deprecated 不推荐使用,但是可以使用
    3、@SuppressWarnings 抑制编译时的警告信息
      抑制单类型的警告 @SuppressWarnings("unchecked")
      抑制多类型的警告 @SuppressWarnings(value={"unchecked", "rawtypes"})
      抑制所有类型的警告:@SuppressWarnings("all")

    三、序列化

    对象

    public class 序列化 implements java.io.Serializable {
        public String name;
        public String address;
        public transient int SSN;
        public int number;
    
        public void mailCheck() { System.out.println("Mailing a check to " + name + " " + address);}
    }

    序列化

    import java.io.FileInputStream;
    import java.io.ObjectInputStream;
    import java.io.IOException;
    
    public class 反序列化 {
        public static void main(String [] args)
        {
            Employee e = null;
            try
            {
                // 创建输入的文件对象
                FileInputStream fileIn = new FileInputStream("./src/序列化、网络编程、多线程、邮件/employee.ser");
                // 创建输入流对象
                ObjectInputStream in = new ObjectInputStream(fileIn);
                e = (Employee) in.readObject();
                in.close();
                fileIn.close();
            }catch(IOException i)
            {
                i.printStackTrace();
                return;
            }catch(ClassNotFoundException c)
            {
                System.out.println("Employee class not found");
                c.printStackTrace();
                return;
            }
            System.out.println("Deserialized Employee...");
            System.out.println("Name: " + e.name);
            System.out.println("Address: " + e.address);
            System.out.println("SSN: " + e.SSN);
            System.out.println("Number: " + e.number);
        }
    }

    反序列化

    import java.io.FileInputStream;
    import java.io.ObjectInputStream;
    import java.io.IOException;
    
    public class 反序列化 {
        public static void main(String [] args)
        {
            Employee e = null;
            try
            {
                // 创建输入的文件对象
                FileInputStream fileIn = new FileInputStream("./src/序列化、网络编程、多线程、邮件/employee.ser");
                // 创建输入流对象
                ObjectInputStream in = new ObjectInputStream(fileIn);
                e = (Employee) in.readObject();
                in.close();
                fileIn.close();
            }catch(IOException i)
            {
                i.printStackTrace();
                return;
            }catch(ClassNotFoundException c)
            {
                System.out.println("Employee class not found");
                c.printStackTrace();
                return;
            }
            System.out.println("Deserialized Employee...");
            System.out.println("Name: " + e.name);
            System.out.println("Address: " + e.address);
            System.out.println("SSN: " + e.SSN);
            System.out.println("Number: " + e.number);
        }
    }

     

    测试进阶轨迹
  • 相关阅读:
    java微信开发
    LeetCode题目解答
    LeetCode 题目总结/分类
    http://baldtheme.com/theme/cleanizr/html/ 类似于这样的
    发现UC/OS-III源码有一处不明白!会不会是BUG.高手过来看看!
    hibernate hbm2ddl auto 不能创建表的问题
    Can jxta be used to develop online card game (p2p style)?
    父子对等组之间的关系
    WScript中调用js方法
    7-wonders-in-java
  • 原文地址:https://www.cnblogs.com/yinwenbin/p/15126686.html
Copyright © 2020-2023  润新知