C # Study Notes-inherit basic knowledge,

Source: Internet
Author: User

C # Study Notes-inherit basic knowledge,

 1 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();        //casts19             Console.ReadLine();20         }21     }22 23     public class Animal24     {25         public int Age26         {27             get;28             set;29         }30         public Animal()     //constructor31         {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 inherited42         {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 virtual51         {52             Console.WriteLine("Animal bite man");53         }54     }55     public sealed class Dog : Animal        //sealed:can't be inherited56     {57         public Dog()        //constructor58         {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 override72         {73             Console.WriteLine("Dog bite man");74         }75     }76 }

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.