1. 基本继承示例

继承的关键字为extends

  1. // Ext.java
  2. class Person {
  3. private int age;
  4. public void setAge(int age) {
  5. if (age < 0 || age > 200)
  6. age = 0;
  7. else {
  8. this.age = age;
  9. }
  10. }
  11. public int getAge() {
  12. return age;
  13. }
  14. }
  15. class Student extends Person{
  16. private String school;
  17. public void setSchool(String school) {
  18. this.school = school;
  19. }
  20. public String getSchool() {
  21. return school;
  22. }
  23. public Student(String school) {
  24. this.school = school;
  25. }
  26. }
  27. public class Ext {
  28. public static void main (String args[]) {
  29. Student stu = new Student("ustc");
  30. stu.setAge(10);
  31. System.out.println(stu.getAge());
  32. System.out.println(stu.getSchool());
  33. }
  34. }

编译运行后,结果为:

solen@ubuntu:~/JavaCodes$ java Ext
10 ustc

2. override 重写

重写是子类对父类的允许访问的方法的实现过程进行重新编写, 返回值和形参都不能改变。即外壳不变,核心重写!
重写的好处在于子类可以根据需要,定义特定于自己的行为。 也就是说子类能够根据需要实现父类的方法。
这一点与C++有很大的不同,C++只能对虚函数进行重写,Java则取消了这一限制。
如果某个方法被final关键字修饰,则该方法不允许被override

  1. class Animal{
  2. public void move(){
  3. System.out.println("动物可以移动");
  4. }
  5. }
  6. class Dog extends Animal{
  7. public void move(){
  8. System.out.println("狗可以跑和走");
  9. }
  10. }
  11. public class TestDog{
  12. public static void main(String args[]){
  13. Animal a = new Animal(); // Animal 对象
  14. Animal b = new Dog(); // Dog 对象
  15. a.move();// 执行 Animal 类的方法
  16. b.move();//执行 Dog 类的方法
  17. }
  18. }

3. super关键字

两种使用场景:

  • 子类调用父类被重写的方法时,需要使用super关键字
  • 在子类中指代父类构造器
    1. class Animal{
    2. public void move(){
    3. System.out.println("动物可以移动");
    4. }
    5. }
    6. class Dog extends Animal{
    7. public void move(){
    8. super.move(); // 应用super类的方法
    9. System.out.println("狗可以跑和走");
    10. }
    11. }
    12. public class TestDog{
    13. public static void main(String args[]){
    14. Animal b = new Dog(); // Dog 对象
    15. b.move(); //执行 Dog类的方法
    16. }
    17. }

    4. abstract 抽象类

    abstract 声明的类是抽象类
    作为一个接口。指定继承者必须实现抽象方法。抽象类不允许实例化对象。这一点与C++是一致的 ```java abstract class Father { private int money;
    public int getMoney() {return money; } public void setMoney(int money) {this.money = money; } public abstract void study(); } class Son extends Father{ public void study() {System.out.println(“I am study”); }

} public class Ext6 { public static void main (String args[]) { Son son = new Son(); son.study(); } }

<a name="HDexB"></a>
# 5. interface接口
接口是为了突破,子类只能单继承一个父类的限制,接口只允许存在抽象方法和常量
<a name="upgNm"></a>
### 抽象类和接口的区别

- 1. 抽象类中的方法可以有方法体,就是能实现方法的具体功能,但是接口中的方法不行。
- 2. 抽象类中的成员变量可以是各种类型的,而接口中的成员变量只能是 **public static final** 类型的。
- 3. 接口中不能含有静态代码块以及静态方法(用 static 修饰的方法),而抽象类是可以有静态代码块和静态方法。
- 4. 一个类只能继承一个抽象类,而一个类却可以实现多个接口。

_ JDK 1.8 以后,接口里可以有静态方法和方法体了_
```java
[可见度] interface 接口名称 [extends 其他的接口名] {
        // 声明变量(常量)
        // 抽象方法
}
abstract class Father {
    private int money;    

    public int getMoney() {return money; }
    public void setMoney(int money) {this.money = money; }

    public abstract void study();
}

interface A {
    public static final int i = 10;
    public abstract int getNum();
}

interface B {
    public static String name = "InterfaceB";
    public abstract String getName();
}

class Son extends Father implements A,B{
    public int getNum() {return i;}
    public String getName() {return name;}
    public void study() {System.out.println("I am study"); }

}

public class Ext7 {
    public static void main (String args[]) {
        Son son = new Son();
        System.out.println(son.getName());
        son.study();
    }
}