学习日志-abstract class and interface

学一点记一点,免得忘记..

 

看了很多前辈写的关于abstract class and interface的文章,自己也试着去梳理了一遍。

 

abstract class :

1.抽象类不能实例化。

2.抽象类可以有构造方法,可以在其派生类中调用。

3.抽象类中的抽象方法(没有方法体)在派生类中必须重写,非抽象方法(有方法体)可以不重写。

4.抽象类可以有字段,属性,方法,构造函数等成员。
5.抽象类对于接口只能满足单一继承。

 

 

ExpandedBlockStart.gif 代码
     abstract   class  Myabstract
    {
        
public  Myabstract()
     {
       Console.WriteLine( " Abstract class constructors " ); 
     }
        
public   abstract   void  abstractMethod();
        
public   void  Method()
        {
            Console.WriteLine(
" Myabstract Method " );
        }
    }

 

 

interface:

1.接口不能实例化。

2.接口不可以有构造方法。

3.接口中的方法不需要方法体,在派生类中必须重写方法。

4.接口不可以有字段。
5.接口的方法不需要修饰符,其含义类似public。

6.接口可以满足“多继承”。

 

 

     interface  MyInterface
    {
        
void  interfaceMethod();
    }

 

 

 Class:

 

ExpandedBlockStart.gif 代码
class  MyClass : Myabstract,MyInterface
    {
        
public  MyClass()
            : 
base ()
        {
        }

        
public   override   void  abstractMethod()
        {
            Console.WriteLine(
" MyClass Override Abstract Method " );
        }

        
public   void  interfaceMethod()
        {
            Console.WriteLine(
" MyClass Override Interface Method " );
        }
    }

 

 

Main Method:

 

ExpandedBlockStart.gif 代码
     class  Program
    {
        
static   void  Main( string [] args)
        {
            MyClass myclass 
=   new  MyClass();
            myclass.abstractMethod();
            myclass.interfaceMethod();
            myclass.Method();
        }
    }

 

 

输出:

Abstract Class Constructors

MyClass Override Abstract Method

MyClass Override Interface Method

Myabstract Method

 

 

 

转载于:https://www.cnblogs.com/Tony-Yang/archive/2010/06/20/1761363.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值