zoukankan      html  css  js  c++  java
  • java对象转字节数组,获取泛型类

    对象转字节数组,字节数组在恢复成对象
    Test.java
    class Test {
        public static void main(String args[]) throws IOException, ClassNotFoundException {
            TestObject to=new TestObject();
            to.setAge(12);
            to.setName("lisi");
            ByteArrayOutputStream byt=new ByteArrayOutputStream();
            ObjectOutputStream oos=new ObjectOutputStream(byt);
            oos.writeObject(to);
    
            byte oArr[]=byt.toByteArray();
            ByteArrayInputStream bis=new ByteArrayInputStream(oArr);
            ObjectInputStream obs=new ObjectInputStream(bis);
            TestObject to1= (TestObject) obs.readObject();
    
            System.out.println(to1.getName());
            System.out.println(to1.getAge());
        }
    }
    TestObject.java
    public class TestObject implements Serializable {
        private String name;
        private int age;
    
        public String getName() {
            return name;
        }
    
        public void setName(String name) {
            this.name = name;
        }
    
        public int getAge() {
            return age;
        }
    
        public void setAge(int age) {
            this.age = age;
        }
    }

    获取泛型类

    public class Test<T> {
        private Type entityClass;
        public Son() {
            getSuperclassTypeParameter();
        }
    
        public    void getSuperclassTypeParameter() {
            Type superclass = getClass().getGenericSuperclass();
            if (superclass instanceof Class) {
                throw new RuntimeException("Missing type parameter.");
            } else {
                ParameterizedType parameterized = (ParameterizedType)superclass;
                System.out.println(parameterized);
            }
        }
        public static void main(String args[]) {
            Son<AuthenticatorUser> s=  new Son<AuthenticatorUser>(){};
    
        }
    }


     
  • 相关阅读:
    三分
    知识点整合摘取
    Codeforces Round #533 C. Ayoub and Lost Array
    判断当前VC 是push还是present的
    自动布局
    获取当前的 viewController
    UIImageView 点击放大缩小
    UIButton 在UIScrollView里面 点击效果不明显的问题
    IOS AutoLayout 遍历修改约束
    GIT 命令 操作 记录
  • 原文地址:https://www.cnblogs.com/blueberry006/p/7844576.html
Copyright © 2011-2022 走看看