• 博客园logo
  • 会员
  • 众包
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • HarmonyOS
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
激情 希望 斗志昂扬
Records of growth process 专注微软技术
博客园    首页    新随笔    联系   管理    订阅  订阅

C#抽象类

 

 

 1 using System;
 2 
 3 namespace ConsoleApplication1
 4 {
 5 /// <summary>
 6 /// Class1 的摘要说明。
 7 /// </summary>
 8 class Class1
 9 {
10    /// <summary>
11    /// 应用程序的主入口点。
12    /// </summary>
13    [STAThread]
14    static void Main(string[] args)
15    {
16              caculator c=new caculator(6,2);
17     c.getresult();
18    
19    }
20 }
21 abstract class template{
22       protected int a;
23    protected int b;
24    public template(int a,int b){
25       this.a=a;
26       this.b=b;
27    }
28    public void getresult(){   //抽象类内可以有其他方法
29        add(a,b);
30     sub(a,b);
31     mul(a,b);
32              div(a,b);
33    }
34    abstract protected void add(int a,int b);  //抽象方法
35    abstract protected   void sub(int a,int b);
36    abstract protected void mul(int a,int b);
37    abstract protected void div(int a,int b);
38 }
39 class caculator :template{                       //抽象类实现方法只能通过派生类,自己不能生成实例
40    public caculator(int a,int b):base(a,b){
41    }
42    protected override void add(int a,int b){
43        int r=a+b;
44     Console.WriteLine("{0}+{1}={2}",a,b,r);
45 
46    }
47    protected override void sub(int a, int b)  //抽象类的派生类必须实现(重写)所有的抽象方法(其他方法不用重写)
48    {
49     int r=a-b;
50     Console.WriteLine("{0}-{1}={2}",a,b,r);
51    }
52    protected override void mul(int a, int b)
53    {
54     int r=a*b;
55     Console.WriteLine("{0}*{1}={2}",a,b,r);
56    }
57    protected override void div(int a, int b)
58    {
59    
60     if(b==0)
61     {
62      Console.WriteLine("input error");
63     }else{
64      int r;
65         r=a/b;
66      Console.WriteLine("{0}/{1}={2}",a,b,r);
67     }
68    }
69 
70 }
71 }
72 
73 
posted @ 2009-03-13 11:36  贤  阅读(361)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3