-
Notifications
You must be signed in to change notification settings - Fork 19
/
Inheritance 603.cs
executable file
·82 lines (66 loc) · 1.9 KB
/
Inheritance 603.cs
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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
//Rextester.Program.Main is the entry point for your code. Don't change it.
//Compiler version 4.0.30319.17929 for Microsoft (R) .NET Framework 4.5
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;
namespace Rextester
{
//Inheritance
//class inherits methods and data members from the class above
//abstract class
//cannot instantiated
//can contain abstract methods but its not required
//can contain non abstract metheods
public abstract class Character
{
public string name_;
public int speed_;
public int health_;
public int test_ = 2;
public abstract void print(); //MUST BE modified in inheriting classes
public int testFunction()
{
test_= test_*2;
return test_;
}
//virtual class
//can be changed in classes that derive from the base class BUT NOT required
public virtual void Swing()
{
Console.WriteLine("SWING!");
}
}
public class SwordsMan : Character
{
public SwordsMan()
{
name_ = "Erik";
speed_ = 10;
health_ = 100;
}
//implement abstract class we must use override
public override void print()
{
Console.WriteLine("My name is " + name_);
Console.WriteLine("and I am " + speed_ + " fast");
Console.WriteLine("my health is " + health_);
}
//virtual funtions can be overriden as well
public override void Swing()
{
Console.WriteLine("Im not going to swing!");
}
}
public class Program
{
public static void Main(string[] args)
{
SwordsMan Erik = new SwordsMan();
Erik.print();
Erik.testFunction();
Erik.Swing();
Console.WriteLine(Erik.test_);
}
}
}