zoukankan      html  css  js  c++  java
  • c# 抽象类和抽象方法

    参考:http://www.runoob.com/csharp/csharp-polymorphism.html

    https://zhidao.baidu.com/question/587686949.html?from=commentSubmit#answers1471481219

     1 using System;
     2 using System.Collections.Generic;
     3 using System.Linq;
     4 using System.Text;
     5 using System.Threading.Tasks;
     6 
     7 namespace ConsoleApplication2
     8 {
     9     public abstract class People   //声明一个抽象类
    10     {
    11         public People()//这是构造函数
    12         {
    13             Console.WriteLine("我是 人 的类,我这不叫被实例化了");
    14         }
    15 
    16         public abstract void study();  //抽象方法只能定义在抽象类中。
    17 
    18     }
    19 
    20     public class Student : People   //继承抽象类
    21     {
    22         public Student()//这是构造函数
    23         {
    24             Console.WriteLine("我是学生类,我被实例化了");
    25         }
    26 
    27         public override void study()     //重写抽象类的抽象方法
    28         {
    29 
    30             Console.WriteLine("好好学习,天天向上!");
    31 
    32         }
    33     }
    34     public class Teacher : People   //继承抽象类
    35     {
    36         public Teacher()//这是构造函数
    37         {
    38             Console.WriteLine("我是教师类,我被实例化了");
    39         }
    40 
    41         public override void study()     //重写抽象类的抽象方法
    42         {
    43 
    44             Console.WriteLine("学习大牛论文,发文章,拿基金!");
    45 
    46         }
    47     }
    48     class Program
    49     {
    50         static void Main(string[] args)
    51         {
    52             Student t = new Student();//实例化派生类
    53 
    54             People p = t;   //使用派生类对象实例化抽象类
    55 
    56             //以上两句等价于  People p = new Student();//使用派生类对象实例化抽象类;
    57 
    58             p.study(); //使用抽象类对象调用抽象类中的抽象方法study  
    59             People p2 = new Teacher();
    60             p2.study();
    61         }
    62     }
    63 }
    抽象类抽象方法
  • 相关阅读:
    RxJava+okhttp3
    RetrofitOkHttp网络请求
    布局111
    网络请求展示数据
    一级列表展示购物车
    终极MVP二级购物车
    将博客搬至CSDN
    nyoj-开灯问题
    nyoj-数乌龟
    nyoj 正三角形的外接圆面积
  • 原文地址:https://www.cnblogs.com/zhubinglong/p/8108632.html
Copyright © 2011-2022 走看看