zoukankan      html  css  js  c++  java
  • C#

     1 using System;
     2 using System.Collections.Generic;
     3 using System.Linq;
     4 using System.Text;
     5 
     6 namespace 类_继承
     7 {
     8     // 基类 : 动物
     9     public abstract class Animal
    10     {
    11         private int _age;
    12         public int Age
    13         {
    14             get { return this._age; }
    15             set
    16             {
    17                 if (value < 0 || value > 10)
    18                 {
    19                     throw (new ArgumentOutOfRangeException("AgeIntPropery", value, "年龄必须在0-10岁之间"));
    20                 }
    21                 this._age = value;
    22             }
    23         }
    24 
    25         // virtual修饰基类的Print()方法, 在子类中 override重写 实现子类实际需求
    26         public virtual void Print()
    27         {
    28         }
    29 
    30     }
    31 
    32     // 子类 : 马  (继承基类 : Animal) 
    33     public class Horse : Animal
    34     {
    35         // 重写基类的Print()方法;
    36         public override void Print()
    37         {
    38             Console.WriteLine("马的年龄为 : {0} 岁", this.Age);
    39         }
    40     }
    41 
    42     // 子类 : 羊  (继承基类 : Animal) 
    43     public class Sheep : Animal
    44     {
    45         // 重写基类的Print()方法;
    46         public override void Print()
    47         {
    48             Console.WriteLine("羊的年龄为 : {0} 岁", this.Age);
    49         }
    50     }
    51 
    52     class Program
    53     {
    54         static void Main(string[] args)
    55         {
    56             Horse horse = new Horse();
    57             horse.Age = 2;
    58             horse.Print();
    59 
    60             Sheep sheep = new Sheep();
    61             sheep.Age = 9;
    62             sheep.Print();
    63 
    64             Console.ReadLine();
    65         }
    66     }
    67 }

  • 相关阅读:
    排序算法
    顺序表与链表
    二叉树
    查找算法
    15 Django 离线脚本
    poj 1330 Nearest Common Ancestors(LCA 基于二分搜索+st&rmq的LCA)
    hdu 6158 The Designer( 反演圆)
    圆的反演性质
    HDU 6153 A Secret(扩展kmp)
    kmp&扩展kmp
  • 原文地址:https://www.cnblogs.com/DuanLaoYe/p/5355379.html
Copyright © 2011-2022 走看看