下面是我学mooc时,所学到一些笔记
继承的相关ppt :
单根继承
https://www.cnblogs.com/xiaoxiaoyihan/p/5014682.html
https://www.cnblogs.com/chenssy/p/3354884.html
https://www.cnblogs.com/zhisuoyu/p/5270926.html(继承和组合)
super的用法:编译器默认加的 super()
抽象类:
abstract 类没办法new
有抽象方法 一定要定义一个抽象类
接口
被继承的类可以是抽象类,也可以是普通类
接口可以同时继承多个接口
public interface Animal {
public void eat();
public void move();
}
-------------------------------
public class Cat implements Animal
{
public void eat() {
System.out.println("Cat: I can eat");
}
public void move(){
System.out.println("Cat: I can move");
}
}
-------------------------------
public interface CatFamily extends Animal, ClimbTree{
//包含以下三个方法
//eat()
//move()
//climb()
}
-------------------------------
public interface ClimbTree {
public void climb();
}
-------------------------------
public abstract class LandAnimal implements Animal {
public abstract void eat() ;
public void move() {
System.out.println("I can walk by feet");
}
}
-------------------------------
public class Rabbit extends LandAnimal implements ClimbTree {
public void climb() {
System.out.println("Rabbit: I can climb");
}
public void eat() {
System.out.println("Rabbit: I can eat");
}
}
-------------------------------
//继承自Shape抽象类
public class Rectangle extends Shape{
int width; //宽
int length; //长
public Rectangle(int length, int width) {
this.length = length;
this.width = width;
}
public void calArea() {
System.out.println(this.length * this.width);
}
public static void main(String[] args) {
Rectangle rect = new Rectangle(10,5);
rect.calArea();
}
}
-------------------------------
public abstract class Shape {
//面积
int area;
//计算面积方法
public abstract void calArea();
}
-------------------------------
public class Tiger implements CatFamily {
//必须实现CatFamily中的三个方法
public void eat() {
System.out.println("Tiger: I can eat");
}
public void move() {
System.out.println("Tiger: I can move");
}
public void climb() {
System.out.println("Tiger: I can climb");
}
}