abstract_class2.vala added
This commit is contained in:
parent
83d28daa30
commit
c5cef9fe2b
|
@ -0,0 +1,37 @@
|
|||
#include <iostream>
|
||||
|
||||
using namespace std;
|
||||
|
||||
class Figure {
|
||||
public:
|
||||
void draw () {
|
||||
cout << "Figure:draw () called" << endl;
|
||||
}
|
||||
};
|
||||
|
||||
class Circle : public Figure {
|
||||
public:
|
||||
void draw () {
|
||||
cout << "Circle:draw () called" << endl;
|
||||
}
|
||||
};
|
||||
|
||||
class ColorCircle : public Circle {
|
||||
public:
|
||||
void draw () {
|
||||
cout << "ColorCircle:draw () called" << endl;
|
||||
}
|
||||
};
|
||||
|
||||
int main () {
|
||||
|
||||
Figure *fig = new ColorCircle ();
|
||||
|
||||
fig->draw ();
|
||||
|
||||
Circle fig2 = *(Circle*)fig;
|
||||
|
||||
fig2.draw ();
|
||||
|
||||
return 0;
|
||||
}
|
|
@ -0,0 +1,36 @@
|
|||
public interface Drawable : Object {
|
||||
|
||||
public abstract void draw ();
|
||||
}
|
||||
|
||||
public abstract class Figure : Object, Drawable {
|
||||
public virtual void draw () {
|
||||
stdout.printf ("Figure:draw() called\n");
|
||||
}
|
||||
}
|
||||
|
||||
public class Circle : Figure {
|
||||
public override void draw () {
|
||||
stdout.printf ("Circle:draw() called\n");
|
||||
}
|
||||
}
|
||||
|
||||
public class ColorCircle : Circle {
|
||||
public override void draw () {
|
||||
stdout.printf ("ColorCircle:draw() called\n");
|
||||
}
|
||||
}
|
||||
|
||||
void main () {
|
||||
var cc = new ColorCircle ();
|
||||
cc.draw ();
|
||||
|
||||
Circle c = cc;
|
||||
c.draw ();
|
||||
|
||||
Figure f = c;
|
||||
f.draw ();
|
||||
|
||||
Drawable d = f;
|
||||
d.draw ();
|
||||
}
|
Loading…
Reference in New Issue