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 }
  • 相关阅读:
    IP通信基础学习第八周
    IP通信基础学习第七周(下)
    IP通信基础学习第七周(上)
    IP通信基础学习第六周(下)
    mysql 笔记
    一个不错的MYSQL数据库备份类,PHP版,一个文件,精简版
    Linux下apache日志(按日期存放)分析与状态查看方法
    呵呵
    docker-compose常用命令
    mysql 数据备份
  • 原文地址:https://www.cnblogs.com/Aries-rong/p/6525625.html
Copyright © 2011-2022 走看看