Annotation(注解)

1.准确覆写@Override

如果准确控制覆写要求,使用@Override,如果覆写不正确,则它提示错误

package com.wjx.sayHello;

class Persons{
	@Override
	//The method tostring() of type Persons must override or implement a supertype method
	//如果写了@Override没有覆写toString()会提示上述问题	
	public String tostring(){
		return "hello";
	}
}
public class TestDemo {
	public static void main(String[] args){
		System.out.println(new Persons());
	}
}

2 .声明过期处理@Deprecated

一些程序类再新版本不希望继续使用,但是在老版本里面还得继续使用,不能删除,此时我们声明注解@Deprecated来表示,声明过的方法或者类不建议使用,但是使用不会报错。

package com.wjx.sayHello;

class Persons{
	@Deprecated
	public Persons(){}
	public Persons(String name){}
	@Deprecated
	public void fun(){}
}
public class TestDemo {
	public static void main(String[] args){
		Persons p=new Persons();
		Persons p1=new Persons("李四");
		p.fun();
		
	}
}
3. 压制警告@

当调用某些方法,定义方法对象等操作时候,程序出现警告时候的时候,你不想让开发工具提示警告可以使用压制警告来避免提示警告

package com.wjx.sayHello;

class Persons<T>{
	@Deprecated
	public Persons(){}
	public Persons(String name){}
	@Deprecated
	public void fun(){}
}
public class TestDemo {
	@SuppressWarnings({ "rawtypes", "unused" })
	public static void main(String[] args){
		//p使用Object接受对象,由于persons为泛型 没声明对象类型会出现警告
		Persons p=new Persons();
		//p1为使用,会出现警告
		Persons p1=new Persons("李四");
		p.fun();
		
	}
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值