zoukankan      html  css  js  c++  java
  • C#抽象方法与抽象实例--C#基础

    1、抽象方法与抽象类的声明

    1)抽象类和抽象方法声明必须包含abstract

    2)抽象方法的声明没有方法体:public abstract void fly();

    3)抽象类和抽象法前加上public,不加默认会是private。

    4)抽象类的声明就是为了派生和继承,标记为sealed,是不可以继承的。

    5)抽象类不能实例化,也就是不能用new、sealed,必须通过继承由派生类实现其抽象方法

    6)抽象类可以包含非抽象方法

    7)笔记原文:

    Program.cs:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    //抽象类加abstract
    //public abstract Fky(){}//默认是private
    //public abstract void fly();抽象方法声明没有方法体
    //抽象类和抽象方法多的声明必须包含abstract
    //抽象类的声明就是为了派生和继承,如果标记为sealed,不可以继承了
    //抽象类不能实例化,必须通过继承由派生类实现其抽象方法,因此不能用new、sealed
    //抽象类中可以包含非抽象方法
    namespace 抽象类与抽象方法
    {
    class Program
    {
    static void Main(string[] args)
    {

    }
    }
    }

    2、抽象类与抽象实例

    Pow.cs:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;

    namespace 抽象类和抽象法实例
    {
    public abstract class Pow
    {
    public abstract void PowMehod(int x,int y);
    }
    }

    Pow1.cs:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;

    namespace 抽象类和抽象法实例
    {
    class Pow1:Pow
    {
    public override void PowMehod(int x,int y)
    {
    int pow = 1;
    for (int i = 1; i <= y; i++)
    {
    pow *= x;
    }
    Console.WriteLine("求幂的结果是:"+pow);
    }
    }
    }

    Pow2.cs:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;

    namespace 抽象类和抽象法实例
    {
    class Pow2:Pow
    {
    public override void PowMehod(int x, int y)
    {

    Console.WriteLine("求幂的结果:"+System.Math.Pow(x,y));
    }
    }
    }

    Program.cs:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;

    namespace 抽象类和抽象法实例
    {
    class Program
    {
    static void Main(string[] args)
    {
    //Pow pow = new Pow();抽象类不可以实例化
    Pow1 pow1 = new Pow1();
    pow1.PowMehod(2,10);
    Pow2 pow2 = new Pow2();
    pow2.PowMehod(2,10);
    Console.ReadKey();
    //abstract方法:没有自己的实现。
    //virtual方法
    //共同点:都可以通过overrid来实现对原来的方法的重写
    }
    }
    }

  • 相关阅读:
    Python反射机制
    并发和并行的区别
    I/O模型
    python 字符串零散方法记录
    python 关于占位符格式化
    Python 十进制转二进制、八进制、十六进制
    python中str函数isdigit、isdecimal、isnumeric的区别
    文件的修改
    LF 模块三
    stackoverflow os.path.abspath and os.path.realpath
  • 原文地址:https://www.cnblogs.com/qikeyishu/p/7509656.html
Copyright © 2011-2022 走看看