zoukankan      html  css  js  c++  java
  • C#学习笔记-继承基本知识

     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();        //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 }
  • 相关阅读:
    Thymeleaf模板引擎绕过浏览器缓存加载静态资源js,css文件
    LCMapString/LCMapStringEx实现简体字、繁体字的转换。
    java8 LocalDateTime转unix时间戳(带毫秒,不带毫秒)
    关于全角半角
    c++builder 读写文件类
    Unresolved external 'AlphaBlend' referenced from
    GridhEH 选择勾CheckBox
    Variant
    TRegEx 正则表达式
    c++ 字符检测 TCharacter
  • 原文地址:https://www.cnblogs.com/Aries-rong/p/6525625.html
Copyright © 2011-2022 走看看