自定义的linux版本号,201013:Linux查看版本-自定义注解反射调用

一. Linux查看版本命令

1. 查看Linux内核版本命令

cat /proc/version

uname -a

2. 查看Linux系统版本的命令

lsb_release -a

注意:使用该命令即可列出所有版本信息,这个命令适用于所有的Linux发行版,包括RedHat、SUSE、Debian等发行版。

cat /etc/redhat-release

注意:该方法只适合Redhat系的Linux

cat /etc/issue

注意:该命令也适用于所有的Linux发行版

3. Linux 下面解压.tar.gz 和.gz文件解压的方式

两种解压方式

tar.gz 使用tar命令进行解压

tar -zxvf java.tar.gz

解压到指定的文件夹

tar -zxvf java.tar.gz -C /usr/java

gz文件的解压 gzip 命令

gzip -d java.gz

也可使用zcat 命令,然后将标准输出 保存文件

zcat java.gz > java.java

4. Linux下运行.sh文件的两种方法

直接./加上文件名.sh,如运行hello.sh为./hello.sh【hello.sh必须有x权限】

直接sh 加上文件名.sh,如运行hello.sh为sh hello.sh【hello.sh可以没有x权限】

方法一:绝对路径执行.sh文件

./home/test/shell/hello.sh

sh /home/test/shell/hello.sh

方法二:当前目录执行.sh文件

cd到.sh文件所在目录

给.sh文件添加x执行权限

chmod u+x hello.sh

./执行.sh文件

./hello.sh

sh 执行.sh文件

sh hello.sh

注意事项:用“./”加文件名.sh执行时,必须给.sh文件加x执行权限

二. 自定义注解

1. 新建一个annotation,名字为:MyAnnotation.java

package com.dragon.test.annotation;

import java.lang.annotation.ElementType;

import java.lang.annotation.Retention;

import java.lang.annotation.RetentionPolicy;

import java.lang.annotation.Target;

/**

* Created by gmq on 2015/9/10.

*/

@Target(ElementType.METHOD)

@Retention(RetentionPolicy.RUNTIME)

public @interface MyAnnotation

{

String hello () default "hello";

String world();

}

2. 建立一个MyTest.java 来使用上面的annotation

package com.dragon.test.annotation;

/**

* Created by gmq on 2015/9/10.

*/

public class MyTest

{

@MyAnnotation(hello = "Hello,Beijing",world = "Hello,world")

public void output() {

System.out.println("method output is running ");

}

}

3. 用反射机制来调用注解中的内容

package com.dragon.test.annotation;

import java.lang.annotation.Annotation;

import java.lang.reflect.Method;

/**

* 用反射机制来调用注解中的内容

* Created by gmq on 2015/9/10.

*/

public class MyReflection

{

public static void main(String[] args) throws Exception

{

// 获得要调用的类

Class myTestClass = MyTest.class;

// 获得要调用的方法,output是要调用的方法名字,new Class[]{}为所需要的参数。空则不是这种

Method method = myTestClass.getMethod("output", new Class[]{});

// 是否有类型为MyAnnotation的注解

if (method.isAnnotationPresent(MyAnnotation.class))

{

// 获得注解

MyAnnotation annotation = method.getAnnotation(MyAnnotation.class);

// 调用注解的内容

System.out.println(annotation.hello());

System.out.println(annotation.world());

}

System.out.println("----------------------------------");

// 获得所有注解。必须是runtime类型的

Annotation[] annotations = method.getAnnotations();

for (Annotation annotation : annotations)

{

// 遍历所有注解的名字

System.out.println(annotation.annotationType().getName());

}

}

}

4. 输出:

Hello,Beijing

Hello,world

----------------------------------

com.dragon.test.annotation.MyAnnotation

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值