Abstract classes can provide implementation at some degree.
Below is an example. The abstract class provides a constructor
which is common to both subclasses, but doesn't care about 'area'
method, which should be overridden.
Note that, as haXe doesn't have an abstract keyword, the method
must be complete. That means it must return a value if the function
demands it.
class AbstractPolygon {
var width:Float;
var height:Float;
private function new(width:Float, height:Float) {
this.width = width;
this.height = height;
}
public function area():Float return 0; // abstract method
}
class Triangle extends AbstractPolygon {
public function new(width:Float, height:Float) {
super(width, height);
}
public override function area():Float {
return width * height / 2;
}
}
class Rectangle extends AbstractPolygon {
public function new(width:Float, height:Float) {
super(width, height);
}
public override function area():Float {
return width * height;
}
}
class Main {
static function main() {
var p1:AbstractPolygon = new Triangle(2, 5);
var p2:AbstractPolygon = new Rectangle(2, 5);
trace(p1.area());
trace(p2.area());
}
}
The example was taken from C++ tutorial on cplusplus.com as I'm learning
that now =P
Ian L.
Post by Achmad AuliaWell it's been a while since I use my C++ skill (I originally C++
programmer), but IIRC Abstract Base Classes (ABC) is basicly a class, with
all the method set to pure virtual, which is basically the same as
interface. And usually used the same way, to establish "contract" between
different part of the code, and programmer.
Post by Ian Liu RodriguesI meant you *can't instantiate it*
*;)
*
Classes with private constructors are abstract, since you can instantiate
Post by Ian Liu Rodriguesan object, forcing you to extend it to make it useful.
Ian L.
It is different with actionscript 3, haxe allow you to define
Post by ä¿¡ææ¶é¸¦ç«é¿variables in interface , I think the developers maybe thought we don't
need Abstract Classes with this feature ? But in fact , we sometimes
need Abstract Classes , and Consts etc ...
btw: There is no User Define Abstract Classes too in Actionscript 3 .
Post by Pimm HogelingAchmad's reply was epic. We had a good laugh.
It is a choice.
Post by Achmad AuliaYou mean "interface" ?
Post by Renaud BardetHi list,
There is no abstract classes nor pure virtual function (virtual = 0
in
Post by Pimm HogelingPost by Achmad AuliaPost by Renaud BardetC++)
in HaXe,
is it a choice or a constraint ?
Cheers,
--
Renaud Bardet - Développeur
Succubus Interactive
--
--
haXe - an open source web programming language
http://haxe.org
--
haXe - an open source web programming language
http://haxe.org
--
Best regards
Yin Fei
Icebirds.net
--
haXe - an open source web programming language
http://haxe.org
--
haXe - an open source web programming language
http://haxe.org
--
haXe - an open source web programming language
http://haxe.org