• Java初学者笔记一:元类、获取类型、枚举


    零、绪论:

      2018年新年伊始,学习Java的冲动越来越强烈,毕竟以后无论是做安全开发还是安全研究都必不可少的掌握这门语言,所以在不断完善Python作为脚本语言的主语言的情况下觉得学习Java作为高级语言当中的主语言(无奈C、C++指针太虐人,内存太虐人,还是被JVM屏蔽了的好,虽然后面的也得看)所以觉得写一个学习系列的Java博客,作为学习笔记。希望能非常基础的但也完整的记录自己学习Java的一点一滴,备忘同时提升。本系列的后续篇章不再像WEB安全系列那样每一篇都把绪论作为开头了,先写下来作为开篇吧。由于大学期间学习过Java最基础的,而且具备一点点CC++的阅读代码能力,所以想hello world啊,环境配置就不写了。

    一、Object类:

      在java中Object是所有类的基类,蛮类似python中的新式类的,哈哈。对于Object的定义的参数,你传入什么类型的动心这个参数变量就是什么类型的,这样就解决了一个数据结构中保存不同类型对象的问题。

    例如,我要搞一个数组{"a",1,c} c是一个类的实例对象,那么该如何定义这个数组呢?

    1 Object[] objlist = new Object[10];

    你对Object参数传参后类型就是你传入的类型,如下是我模仿python字典的一些基本操作,用java实现的一个数据结构:

    节点类:

     1 class dictnode{
     2     private Object key;
     3     private Object value;
     4     dictnode(Object keyname,Object valueitem){
     5         this.key = keyname;
     6         this.value = valueitem;
     7     }
     8     protected void finalize() {}
     9     public Object getValue() {
    10         return this.value;
    11     }
    12     public void setValue(Object value) {
    13         this.value = value;
    14     }
    15     public Object getKey() {
    16         return this.key;
    17     }
    18 }

    字典类:

     1 class dict{
     2     private int max;
     3     private int size = 0;
     4     private dictnode[] nodelist;
     5     dict(int max){
     6         this.max = max;
     7         nodelist = new dictnode[max];
     8     }
     9     public boolean set(Object key,Object value) throws Exception {
    10         if(this.size <= this.max-1 ) {
    11             for(int flag=0;flag<this.size;flag++) {
    12                 if (nodelist[flag].getKey() == key) {
    13                     nodelist[flag].setValue(value);
    14                     return true;
    15                 }
    16             }
    17             nodelist[this.size] = new dictnode(key,value);
    18             this.size++;
    19             return false;
    20         }
    21         else {
    22             throw new Exception("字典已经满额"); 
    23         }
    24     }
    25     public Object get(Object key) throws Exception {
    26         for(int flag=0;flag<this.size;flag++) {
    27             if (nodelist[flag].getKey() == key) {
    28                 return nodelist[flag].getValue();
    29             }
    30         }
    31         throw new Exception("未找到该键值");
    32     }
    33     public int getSize() {
    34         return this.size;
    35     }
    36     public void clear() {
    37         for(int flag=0;flag<this.size;flag++) {
    38             this.nodelist[flag] = null;
    39         }
    40         this.size = 0;
    41     }
    42     public boolean hasKeys(Object key) {
    43         for(int flag=0;flag<this.size;flag++) {
    44             if (this.nodelist[flag].getKey() == key) {
    45                 return true;
    46             }
    47         }
    48         return false;
    49     }
    50     public dictnode[] items() {
    51         return this.nodelist;
    52     }
    53     public Object[] keys() {
    54         Object[] keylist= new Object[this.size];
    55         for(int flag=0;flag<this.size;flag++) {
    56             keylist[flag] = this.nodelist[flag].getKey();
    57             //System.out.println(keylist[flag]);
    58         }
    59         return keylist;
    60     }
    61     public Object[] values() {
    62         Object[] valuelist= new Object[this.size];
    63         for(int flag=0;flag<this.size;flag++) {
    64             valuelist[flag] = this.nodelist[flag].getValue();
    65         }
    66         return valuelist;
    67     }
    68 }

    对于Object类型参数,我传入一个{“1”:2},那么节点中的Object参数value的getClass()就变成了你还:

    class java.lang.Integer

    如上,我们可以通过Object数据类型来构造范式的数据结构。

    特殊的类型枚举类

    我们来看一个定义:

    class student{
        public String Name;
        enum gendertype{MALE,FEMALE}
        public gendertype Gender;
        public String StudentID;
        public int Age;
        public String Level;
        private double AveryScore;
        
        public student(String name,int gender,String studentid,int age,String level) {
            this.Name = name;
            if(gender == 0){
                this.Gender = Gender.MALE;
            }
            else {
                this.Gender = Gender.FEMALE;
            }
            this.StudentID = studentid;
            this.Age = age;
            this.Level = level;
            this.AveryScore = 0.00;
        }
        public double getStudentAveryScore() {
            return this.AveryScore;
        }
        public void setStudentAveryScore(double averscore) {
            this.AveryScore = averscore;
        }
        public void show() {
            String infostr = "学号:"+this.StudentID+"姓名:"+this.Name+" 性别:"+this.Gender+" 年龄:"+String.valueOf(this.Age)+" 年级:"+this.Level;
            System.out.println(infostr);
        }
    }

    我们来看枚举类的定义方法:

     1 enum gendertype{MALE,FEMALE} 
     2 public gendertype Gender;
     3 
     4 //调用的时候
     5 if(gender == 0){
     6     this.Gender = Gender.MALE;
     7 }
     8 else {
     9     this.Gender = Gender.FEMALE;
    10 }
  • 相关阅读:
    系统架构师基础到企业应用架构表现层
    网站性能优化之应用程序缓存中篇
    系统架构师基础到企业应用架构企业应用架构
    系统架构师基础到企业应用架构服务层
    http的请求和响应过程管道
    反射获取信息图(转)
    白话学习MVC(二)页面周期一
    Asp.Net请求原理and页面生命周期(转)
    前后台互访
    HttpApplication事件&ASP.NET页面周期
  • 原文地址:https://www.cnblogs.com/KevinGeorge/p/8253784.html
Copyright © 2020-2023  润新知