mybatis源码分析-注解-1

package com.ilucky.mybatis.source.annotation.test1;

/**
 * @author IluckySi
 * @since 20151022
 * 父类是People, 子类是Adult和Child, 在子类继承父类的方法上可以添加Override注解, 这样有利于编译器帮助检查错误. 
 * 在父类的方法上有一个获取身高的方法getHeight(), 这个方法对于Child子类来说有点不太合适, 因为小孩的身高一直是变化的,
 * 所以可以在Child子类获取身高的方法上添加注解@Deprecated, 标记此方法已经过时, 不建议使用.
 * 这时如果在其他的地方使用Child的getHeight方法时, 编译器就会通过删除线的方式提示不建议使用此方法, 但是也不会报错, 只是警告,
 * 如果强制使用, 对于有强迫症的人来说,可以通过@SuppressWarnings("deprecation")注解忽略警告.
 */
public class MainTest {

    @SuppressWarnings("deprecation")
    public static void main(String[] args) {
        People child1 = new Child("child1", "123456", 115);
        child1.getUsername();
        child1.getPassword();
        child1.getHeight();
        Child child2 = new Child("child2", "123456", 115);
        child2.getUsername();
        child2.getPassword();
        child2.getHeight();  // 在此处会有删除线
        People adult1 = new Adult("adult1", "123456", 175);
        adult1.getUsername();
        adult1.getPassword();
        adult1.getHeight();
        Adult adult2= new Adult("adult2", "123456", 175);
        adult2.getUsername();
        adult2.getPassword();
        adult2.getHeight();
    }
}
package com.ilucky.mybatis.source.annotation.test1;

/**
 * @author IluckySi
 * @since 20151022
 */
public interface People {

    /**
     * 获取用户名
     * @return String
     */
    public String getUsername();

    /**
     * 获取密码
     * @return String
     */
    public String getPassword();

    /**
     * 获取身高
     * @return int
     */
    public int getHeight();
}

/**
 * @author IluckySi
 * @since 20151022
 */
public class Child implements People {

    private String username;
    private String password;
    private int height;

    public Child() {
    }
    public Child(String username, String password, int height) {
        this.username = username;
        this.password = password;
        this.height = height;
    }

    @Override
    public String getUsername() {
        return username;
    }
    public void setUsername(String username) {
        this.username = username;
    }
    @Override
    public String getPassword() {
        return password;
    }
    public void setPassword(String password) {
        this.password = password;
    }
    @Override
    @Deprecated
    public int getHeight() {
        return height;
    }
    @Deprecated
    public void setHeight(int height) {
        this.height = height;
    }
}

package com.ilucky.mybatis.source.annotation.test1;

/**
 * @author IluckySi
 * @since 20151022
 */
public class Adult implements People {

    private String username;
    private String password;
    private int height;

    public Adult() {
    }
    public Adult(String username, String password, int height) {
        this.username = username;
        this.password = password;
        this.height = height;
    }

    @Override
    public String getUsername() {
        return username;
    }
    public void setUsername(String username) {
        this.username = username;
    }
    @Override
    public String getPassword() {
        return password;
    }
    public void setPassword(String password) {
        this.password = password;
    }
    @Override
    public int getHeight() {
        return height;
    }
    public void setHeight(int height) {
        this.height = height;
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值