当前位置:网站首页>泛型和注解
泛型和注解
2022-07-24 05:15:00 【x0757】
目录
1.什么是泛型?
其实我们再使用集合时就用过泛型List<T> 创建一个List对象List<Student> list=new ArrayList();
<T>它就是泛型。
所谓的泛型就是在类定义时,不为类中属性和方法指定数据类型,而是在类对象创建时为其指定相应的数据类型。
2.为什么使用泛型?
例子: 要求定义一个Point点类,该类中属性有x坐标和y坐标。
要求: x和y的值可以都是整数类型。
x和y的值可以都是小数类型。
x和y的值可以都是字符串类型。
如何定义该类呢? 如何确定属性的类型。----Object类型
public class Point {
//x坐标
private Object x;
//y坐标
private Object y;
//输出坐标的值
public void show(){
System.out.println("x坐标:"+x+";y坐标:"+y);
}
public Point() {
}
public Point(Object x, Object y) {
this.x = x;
this.y = y;
}
public Object getX() {
return x;
}
public void setX(Object x) {
this.x = x;
}
public Object getY() {
return y;
}
public void setY(Object y) {
this.y = y;
}
}测试
public class Test {
public static void main(String[] args) {
Point p1=new Point(10,20);//坐标为整数int--自动装箱->Integer--->Object(向上转型)
p1.show();
Point p2=new Point(25.5,36.6);//坐标为小数
p2.show();
Point p3=new Point("东经180度","北纬25度");//坐标为字符串
p3.show();
}
}但是:如果我们为坐标一个赋值整数,一个赋值为字符串,这时会不会报错。
但是它违背了我们设计的要求,这就是我们提到的数据类型安全问题。如何解决数据类型安全问题?
可以使用泛型来解决。
public class Point<T> {
//x坐标----
private T x;
//y坐标
private T y;
//输出坐标的值
public void show(){
System.out.println("x坐标:"+x+";y坐标:"+y);
}
public Point() {
}
public Point(T x, T y) {
this.x = x;
this.y = y;
}
public T getX() {
return x;
}
public void setX(T x) {
this.x = x;
}
public T getY() {
return y;
}
public void setY(T y) {
this.y = y;
}
}
class Test{
public static void main(String[] args) {
Point<Integer> p1=new Point<Integer>(10,20);//坐标为整数int--自动装箱->Integer--->Object(向上转型)
p1.show();
Point<Double> p2=new Point<Double>(25.5,36.6);//坐标为小数
p2.show();
Point<String> p3=new Point<String>("东经180度","北纬25度");//坐标为字符串
p3.show();
}
}
注意: 上面的泛型类型必须都是引用类型。不能是基本类型。
使用泛型就保证了数据类型安全问题。
3.如何定义泛型
泛型可以定义在类上,接口上,方法上。 泛型类,泛型接口以及泛型方法。
泛型可以解决数据类型的安全性问题,其主要原理是在类声明时通过一个==标识表示类中某个属性的数据类型或者是某个方法的返回值及参数类型==。这样在类声明或者实例化时只要指定好需要的类型即可。
格式:
public class 类名<泛型标志,泛型标志....>{
//类成员
}
public class Testfx {
public static void main(String[] args) {
Info<String> i1= new Info<>();
i1.setVar("吃饭");
i1.show();
Info<Integer> i2= new Info<>();
i2.setVar(10);
i2.show();
//如果没有指定泛型类型默认为Object,
Info i3 = new Info<>();
i3.setVar("hello");
i3.show();
//如果想使用真正的类型接受,那么必须进行强制
String var = (String) i3.getVar();
}
}
//T标志可以任意起名.----> 那么在创建对象时,必须为每个泛型指定数据类型。
class Info<T>{
public T var;
public void show(){
System.out.println(var);
}
public T getVar() {
return var;
}
public void setVar(T var) {
this.var = var;
}
}4. 通配符
在开发中对象的==引用传递==是最常见的,但是如果在泛型类的操作中,在进行==引用传递时泛型类型必须匹配才可以传递,否则是无法传递的==。如果想传递,可以定义泛型为?通配符。
public class Testtbf {
public static void main(String[] args) {
Info<Integer> i1 = new Info<>();
i1.setVar(100);
fun(i1);
Info<Double> i2= new Info<>();
i2.setVar(1.2);
fun(i2);
Info<String> i3 = new Info<>();
i3.setVar("小潘潘,你又调皮了");
fun(i3);
}
public static void fun(Info<?> info){
info.show();
}
}
class Info<T>{
private T var;
public void show(){
System.out.println("show===="+var);
}
public Info(T var) {
this.var = var;
}
public Info() {
}
public T getVar() {
return var;
}
public void setVar(T var) {
this.var = var;
}
}5. 受限泛型
在引用传递中,在泛型操作中也可以设置一个泛型对象的==范围上限==和==范围下限==。范围上限使用extends关键字声明,表示参数化的类型可能是所指定的类型或者是此类型的子类,而范围下限使用super进行声明,表示参数化的类型可能是所指定的类型或者此类型的父类型。
语法:
[设置上限]
声明对象: 类名称<? extends 类> 对象名称;
定义类: [访问权限] 类名称<泛型标识 extends 类>{}[设置下限]
声明对象: 类名称<? super 类> 对象名称;
定义类: [访问权限] 类名称<泛型标识 super 类>{}
public class TestSxfx {
public static void main(String[] args) {
Info<Number> i = new Info<>();
i.setVar(12);
fun01(i);
fun02(i);
Info<Integer> i2 = new Info<>();
i2.setVar(23);
fun01(i2);
Info<Object> i3 = new Info<>();
i3.setVar("小潘潘");
fun02(i3);
}
//上限
public static void fun01(Info<? extends Number> info){
info.show();
}
//下限
public static void fun02(Info<? super Number> info){
info.show();
}
}
class Info<T>{
private T var ;
public void show(){
System.out.println(var);
}
public Info() {
}
public Info(T var) {
this.var = var;
}
public T getVar() {
return var;
}
public void setVar(T var) {
this.var = var;
}
}6. 泛型接口
上面那些例子都是使用泛型类。而在jdk1.5以后,泛型也可以定义在接口上了,定义接口的泛型和定义泛型类语法相似。
语法:
public interface 接口名<泛型标志,泛型标志....>{
//静态常量
//抽象方法。
}
public class TestInterface {
public static void main(String[] args) {
Upan u= new Upan();
u.eat();
Mouse m = new Mouse();
m.eat();
}
}
interface USB<T>{
T eat();
}
class Upan implements USB<String>{
@Override
public String eat() {
System.out.println("U");
return null;
}
}
class Mouse<T> implements USB<T>{
@Override
public T eat() {
System.out.println("M");
return null;
}
}7. 泛型方法
前面学习的所有泛型操作都是将整个类进行泛型化,但同样也可以在类中定义泛型化的方法。泛型方法的定义与其所在的类是否是泛型类是没有任何关系的,所在的类可以是泛型类,也可以不是泛型类。
【泛型方法的简单定义】
[访问权限] ==<泛型标识>== 泛型标识 方法名称(泛型标识 参数名称)
public class TestClass {
public static void main(String[] args) {
String hello = People.fun("hello");
Double fun = People.fun(10.1);
Integer fun1 = People.fun(20);
}
}
class People{
//泛型方法: static静态成员,随着类的加载而被加载到JVM内存中。常量池
public static <T> T fun(T t){
return t;
}
}8 注解
注释: java不会编译注释的内容,注释给程序员看的。
注解: 它是程序看,当程序看到这个注解时,就应该解析它。
譬如: @Controller @Override
注解的分类:
1. 预定义注解
2. 自定义注解
3. 元注解
8.1 预定义注解
预定义注解就是JDK自带的一些注解,该注解被JVM而解析。
1. @Override: 重写得注解。符合重写得规则。
2. @Deprecated: 表示已过时。
3. @SuppressWarnings: 表示抑制警告。
4. @FunctionInterface: 表示函8.2数式接口。表示该接口中有且仅有一个抽象方法。
8.2 自定义注解
语法:
public @interface 注解名{
//注解属性
数据类型 属性名() default 默认值;
}
使用自定义注解:
类 方法 属性 加@注解名
数据类型: 基本类型,字符串类型,枚举类型【常量】,注解类型,数组类型【必须是上面这些类型的数组】
public class Test {
public static void main(String[] args) {
Info i=new Info();
i.name="笑笑";
i.show();
}
}
//定义好注解了
@interface My{
//注解属性
}
//使用注解
@My
class Info{
@My
public String name;
@My
public void show(){
System.out.println("show================="+name);
}
}
注意: 使用注解和不使用注解没有区别?
注解本身没有任何意义,它只有被解析了,才会赋予真正的意义。
我们后会使用反射来对象注解进行解析。
像:@Override 它被JVM解析,从而使其具有相应的意义。
@Controller @RequestMapping 它被Spring框架解析,所以具有相应的意义。
8.3 元注解
定义在注解上的注解称为元注解。
@Controller它只能加在类上 @Override它只能加在方法上。
原因它使用了元注解可以设置注解使用的位置。
1. @Target(value=可以取下面这些内容): 作用限制注解使用得位置。
/** 表示可以作用在类,接口,枚举 */
TYPE,/** 属性 */
FIELD,/** 普通方法上 */
METHOD,/** 方法参数 */
PARAMETER,/** 构造方法上 */
CONSTRUCTOR,/** 局部变量 */
LOCAL_VARIABLE2. @Retention: 注解什么时候生效。默认时源码 java经历了那些阶段。
源码阶段-->字节码阶段--->运行阶段
/**
* 源码时生效
*/
SOURCE,/**
* 字节码时生效
*/
CLASS,/**
* 运行时生效。
* 在JVM内存中还有该注解。
都会被设置为运行时有效
*/
RUNTIME
3. @Documented 当生产API文档时该注解还存在。4. @Inherited 是否运行被子类继承。
public class Testzj {
public static void main(String[] args) {
Info i = new Info();
i.setName("笑笑");
i.show();
}
}
//表示该注解可以使用的位置
@Target(value ={ElementType.TYPE,ElementType.METHOD})
//表示注解什么时候生效--source--class[默认字节码有效]---runtime[反射时验证]
@Retention(value= RetentionPolicy.RUNTIME)
//是否在生成api文档时存在该注解
@Documented
//子类是否能继承该注解,如果注解在定义时使用了下面这个元注解则能被子类继承。
@Inherited
@interface My{
String value();
int age() default 15;
String[] hobby() default {"看书"};
}
@My(value = "hello")
class Info{
private String name;
@My(value = "world")
public void show(){
System.out.println("show==="+name);
}
public Info() {
}
public Info(String name) {
this.name = name;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public String toString() {
return "Info{" +
"name='" + name + '\'' +
'}';
}
}边栏推荐
- Web3 product manager's Guide: how to face the encryption world
- Heavy! The 2022 China open source development blue book was officially released
- Learning pyramid context encoder network for high quality image painting paper notes
- PostgreSQL: run PostgreSQL + pgadmin 4 in docker
- Pointer learning diary (V) classic abstract data types and standard function libraries
- 【NumPy】
- Pointer learning diary (III)
- [postgraduate entrance examination vocabulary training camp] day 10 - capital, expand, force, adapt, depand
- Hcip day 3 - mGRE experiment
- PPPoE网关模拟环境搭建
猜你喜欢

1. Pedestrian recognition based on incremental occlusion generation and confrontation suppression

The fourth job: about the usage of cat, grep, cut, sort, uniq, VIM, TR and other commands

Jiang Xingqun, senior vice president of BOE: aiot technology enables enterprise IOT transformation

Introduction to 51 single chip microcomputer (dedicated to the most understandable article for beginners)

Chapter VI more supervision training

熊市抄底指南

XML schema

Kingbase V8R6集群安装部署案例---脚本在线一键缩容

On the dilemma faced by non transferable reputation points NFT SBTS
![[Huang ah code] Introduction to MySQL - 3. I use select *, and the boss directly rushed me home by train, but I still bought a station ticket](/img/60/23fc79cf0e399265b4bd75159ad4d1.png)
[Huang ah code] Introduction to MySQL - 3. I use select *, and the boss directly rushed me home by train, but I still bought a station ticket
随机推荐
High performance architecture design of wechat circle of friends
jdbc的增删改查
[advanced mathematics] the difference between differentiable and differentiable functions
The opening ceremony of the 2022 Huawei developer competition in China kicked off!
postgresql:在Docker中运行PostgreSQL + pgAdmin 4
Summary of common errors in wechat applet cloud development
Summary of the development process and key and difficult points of the Listening Project
Execution sequence of finally and return
C table data De duplication
Source code compilation!!
网NN计算能主机系统资e提供的NTCP
Ren Xudong, chief open source liaison officer of Huawei: deeply cultivate basic software open source and jointly build the root technology of the digital world
Using a* heuristic search to solve maze routing problem
支撑复杂的模型群监控、实时告警等t4 文件系统。e
Chapter 1 regression, classification & clustering
1. Pedestrian recognition based on incremental occlusion generation and confrontation suppression
Heavy! The 2022 China open source development blue book was officially released
Transpose of array sparse matrix
Illustration and text demonstrate the movable range of the applet movable view
Memorandum 2022