VB.net 2010 视频教程 VB.net 2010 视频教程 python基础视频教程
SQL Server 2008 视频教程 c#入门经典教程 Visual Basic从门到精通视频教程
当前位置:
首页 > Python基础教程 >
  • C#教程之C#学习笔记-继承基本知识

namespace Inheritance
 2 {
 3     class Program
 4     {
 5         static void Main(string[] args)
 6         {
 7             Dog dog = new Dog();
 8             dog.Age = 10;
 9             dog.Bite();
10             dog.GetAge();
11             dog.BiteMan();
12             Dog oldDog = new Dog(10);
13 
14 
15             Animal dog2 = new Dog();
16             dog2.Bite();
17             dog2.BiteMan();
18             ((Dog)(dog2)).BiteMan();        //casts
19             Console.ReadLine();
20         }
21     }
22 
23     public class Animal
24     {
25         public int Age
26         {
27             get;
28             set;
29         }
30         public Animal()     //constructor
31         {
32             Console.WriteLine("Animal constructure");
33         }
34 
35         public Animal(int age)
36         {
37             Console.WriteLine("Old Animal");
38             this.Age = age;
39         }
40 
41         public virtual void Bite()      //virtual can be override when it be inherited
42         {
43             Console.WriteLine( "Animal bite");
44         }
45         public virtual void GetAge()
46         {
47             Console.WriteLine(Age);
48         }
49 
50         public void BiteMan()       //can't be overrided without virtual
51         {
52             Console.WriteLine("Animal bite man");
53         }
54     }
55     public sealed class Dog : Animal        //sealed:can't be inherited
56     {
57         public Dog()        //constructor
58         {
59             Console.WriteLine("Dog constructure");
60         }
61         public Dog(int age):base(age)       //call specific function     
62         {
63             Console.WriteLine("Old Dog");
64             this.Age = age;
65         }
66         public override void Bite()     //override the parent class method 
67         {
68             Console.WriteLine("Dog bite");
69         }
70 
71         public new void BiteMan()       //new:new method which is as same as the parent class method,not override
72         {
73             Console.WriteLine("Dog bite man");
74         }
75     }
76 }

相关教程