// I say all motor vehicles should look like this:interface MotorVehicle{void run();
int getFuel();}
// My team mate complies and writes vehicle looking that wayclass Car implements MotorVehicle{
int fuel;
void run(){print("Wrroooooooom");}
int getFuel(){return this.fuel;}}
// I say all motor vehicles should look like this:abstract class MotorVehicle{
int fuel;
// They ALL have fuel, so lets implement this for everybody.int getFuel(){return this.fuel;}
// That can be very different, force them to provide their// own implementation.abstract void run();}
// My teammate complies and writes vehicle looking that wayclass Car extends MotorVehicle{void run(){print("Wrroooooooom");}}
class Box implements Package, Property {@OverrideString address() {return "5th Street, New York, NY";}@OverrideHuman owner() {// this method is part of another contract}}
abstract class GpsBox implements Package {@Overridepublic abstract String address();protected Coordinates whereAmI() {// connect to GPS and return my current position}}
class Circle {
protected $radius;
public function __construct($radius)
{$this->radius = $radius}
public function area(){return 3.14159 * pow(2,$this->radius); // simply pie.r2 (square);}
}
//Our area calculator class would look like
class Areacalculator {
$protected $circle;
public function __construct(Circle $circle){$this->circle = $circle;}
public function areaCalculate(){return $circle->area(); //returns the circle area now}
}
我们只会做
$areacalculator = new Areacalculator(new Circle(7));
Interface Shape {
public function area(); //Defining contract for the classes
}
Class Square implements Shape {
$protected length;
public function __construct($length) {//settter for length like we did on circle class}
public function area(){//return l square for area of square}
Class Rectangle implements Shape {
$protected length;$protected breath;
public function __construct($length,$breath) {//settter for length, breath like we did on circle,square class}
public function area(){//return l*b for area of rectangle}
}
现在为面积计算器
class Areacalculator {
$protected $shape;
public function __construct(Shape $shape){$this->shape = $shape;}
public function areaCalculate(){return $shape->area(); //returns the circle area now}
}
$areacalculator = new Areacalculator(new Square(1));$areacalculator->areaCalculate();
$areacalculator = new Areacalculator(new Rectangle(1,2));$areacalculator->;areaCalculate();
这不是更灵活吗?如果我们在没有接口的情况下编码,我们会检查每个形状的实例的冗余代码。
什么时候使用抽象?
Abstract Animal {
public function breathe(){
//all animals breathe inhaling o2 and exhaling co2
}
public function hungry() {
//every animals do feel hungry
}
abstract function communicate();// different communication style some bark, some meow, human talks etc
}
向API添加方法很容易,API的所有存量用户仍然可以编译。向SPI添加方法很难,因为每个服务提供者(具体实现)都必须实现新方法。如果使用接口来定义SPI,则提供者必须在SPI契约更改时发布新版本。如果使用抽象类,新方法可以根据现有抽象方法定义,或者作为空的throw not implemented exception存根定义,这至少可以允许旧版本的服务实现仍然编译和运行。
一个推论:反过来经常是错误的:当使用事情时,总是尝试使用你实际需要的最通用的类/接口。换句话说,不要将你的变量声明为ArrayList theList = new ArrayList(),除非你实际上非常依赖它是一个阵列列表,并且没有其他类型的列表适合你。相反,使用List theList = new ArrayList,甚至Collection theCollection = new ArrayList,如果它是一个列表,而不是任何其他类型的集合实际上并不重要。
public abstract class DesireCar{
//It is an abstract method that defines the prototype.public abstract void Color();
// It is a default implementation of a Wheel method as all the desire cars have the same no. of wheels.// and hence no need to define this in all the sub classes in this way it saves the code duplicasy
public void Wheel() {
Console.WriteLine("Car has four wheel");}}
**Here is the sub classes:**
public class DesireCar1 : DesireCar{public override void Color(){Console.WriteLine("This is a red color Desire car");}}
public class DesireCar2 : DesireCar{public override void Color(){Console.WriteLine("This is a red white Desire car");}}
界面示例:
public interface IShape{// Defines the prototype(template)void Draw();}
// All the sub classes follow the same template but implementation can be different.
public class Circle : IShape{public void Draw(){Console.WriteLine("This is a Circle");}}
public class Rectangle : IShape{public void Draw(){Console.WriteLine("This is a Rectangle");}}
abstract class Animals{// They all love to eat. So let's implement them for everybodyvoid eat(){System.out.println("Eating...");}// The make different sounds. They will provide their own implementation.abstract void sound();}
class Dog extends Animals{void sound(){System.out.println("Woof Woof");}}
class Cat extends Animals{void sound(){System.out.println("Meoww");}}
以下是Java中的接口实现:
interface Shape{void display();double area();}
class Rectangle implements Shape{int length, width;Rectangle(int length, int width){this.length = length;this.width = width;}@Overridepublic void display(){System.out.println("****\n* *\n* *\n****");}@Overridepublic double area(){return (double)(length*width);}}
class Circle implements Shape{double pi = 3.14;int radius;Circle(int radius){this.radius = radius;}@Overridepublic void display(){System.out.println("O"); // :P}@Overridepublic double area(){return (double)((pi*radius*radius)/2);}}
<?phpclass X implements Y { } // this is saying that "X" agrees to speak language "Y" with your code.
class X extends Y { } // this is saying that "X" is going to complete the partial class "Y".?>