Java—abstract类与继承

抽象类

  • abstract类不能使用new建立对象,只能作为父类
  • 抽象方法中没有方法体,包含抽象方法的类一定要为抽象类
  • abstract方法必须在子类中重写
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
abstract class shape {
protected String name;
double s, v;

public abstract String getName();
}

class ball extends shape {
private double r;

public String getName() {
return ball.super.name;
}

ball(double r, String name) {
this.r = r;
this.name = name;
s = 4 * Math.PI * Math.pow(r, 2);
v = 4 * Math.PI * Math.pow(r, 3) / 3;
}

public double getVolume() {
return v;
}
}

public class Main {
public static void main(String[] args) {
ball c = new ball(2, "MyBall");
System.out.println(c.getName() + "\t" + c.getVolume());
}
}

多态

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
public class Test {
public static void main(String[] args) {
show(new Cat()); // 以 Cat 对象调用 show 方法
show(new Dog()); // 以 Dog 对象调用 show 方法

Animal a = new Cat(); // 向上转型
a.eat(); // 调用的是 Cat 的 eat
Cat c = (Cat)a; // 向下转型
c.work(); // 调用的是 Cat 的 work
}

public static void show(Animal a) {
a.eat();
// 类型判断
if (a instanceof Cat) { // 猫做的事情
Cat c = (Cat)a;
c.work();
} else if (a instanceof Dog) { // 狗做的事情
Dog c = (Dog)a;
c.work();
}
}
}

abstract class Animal {
abstract void eat();
}

class Cat extends Animal {
public void eat() {
System.out.println("吃鱼");
}
public void work() {
System.out.println("抓老鼠");
}
}

class Dog extends Animal {
public void eat() {
System.out.println("吃骨头");
}
public void work() {
System.out.println("看家");
}
}