Java注释@Override

简介:

@Override指定方法覆载。它可以强制一个子类必须覆盖父类的方法。

package ch14;

/**
 * Created by Jiqing on 2016/12/27.
 */
public class Fruit {
    public void info() {
        System.out.println("水果的info方法");
    }

    public void test() {
        System.out.println("水果的test方法");
    }
}
class Apple extends Fruit {
    // 使用@Override指定下面的方法必须重写父类方法,告诉编译器,父类要包含一个被该方法重写的方法,否则会编译出错
    @Override
    public void info() {
        System.out.println("苹果的info方法");
    }

    public static void main(String[] info) {
        Apple apple = new Apple();
        apple.info();
    }
}

结果:苹果的info方法

@Deprecated标示已过时

当其他程序使用已过时的类、方法时,编译器将会给出警告

package ch14;

/**
 * Created by Jiqing on 2016/12/27.
 */
class Apple {
    // 定义info方法已过时
    @Deprecated
    public void info() {
        System.out.println("方法已过时");
    }
}

public class DeprecatedTest {
    public static void main(String[] args) {
        // 使用info方法将被警告
        new Apple().info();
    }
}

422101-20161228001427804-1628688749.png

方法论:不必全部掌握,了解一些常用的即可。


本文转自TBHacker博客园博客,原文链接:http://www.cnblogs.com/jiqing9006/p/6227983.html,如需转载请自行联系原作者

相关文章
|
4天前
|
Java 数据安全/隐私保护
java中public、private、protected作用范围
该内容是关于Java中访问修饰符的范围总结:`public`(全局访问)、`protected`(同包及子类访问)、默认(同包访问)、`private`(仅本类访问)。
32 6
|
2天前
使用EventBus 3.0 报 Subscriber class com.example.test.MainActivity and its super classes have no public methods with the @Subscribe annotation
使用EventBus 3.0 报 Subscriber class com.example.test.MainActivity and its super classes have no public methods with the @Subscribe annotation
12 5
this和super用法的区别与细节(java继承中this和super的比较)
this和super用法的区别与细节(java继承中this和super的比较)
|
Android开发 开发者
【解决问题的思路】its super classes have no public methods with the @Subscribe annotation
【解决问题的思路】its super classes have no public methods with the @Subscribe annotation
874 0
|
JavaScript
TypeScript类与继承和修饰符public 、private 、protected 的详细讲解 (上)
TypeScript类与继承和修饰符public 、private 、protected 的详细讲解
TypeScript类与继承和修饰符public 、private 、protected 的详细讲解 (上)
|
JavaScript
TypeScript类与继承和修饰符public 、private 、protected 的详细讲解 (下)
TypeScript类与继承和修饰符public 、private 、protected 的详细讲解
|
编译器
public <T> T method(T t)方法详解
public <T> T method(T t)方法详解
248 0
public <T> T method(T t)方法详解