Abstract class defines:
while its function is to generate the subclass specific properties and to a subclass.
abstract modifier classes can be used with the methods, properties, events, and indexers. Use the abstract modifier in a class declaration to indicate that a class is the only other class of the parent class. Contained in the abstract or abstract class members must be achieved from the subclass of abstract class mark.
Characteristics:
1. An abstract class that can not be instantiated;
2. abstract class may contain abstract methods and accessors;
3. not modified with the sealed modifier;
4 derived from the abstract class must include non-abstract class inherits all abstract methods and implement the abstract accessor.
Summary:
~ abstract method is implicit virtual method;
~ only allow the use of abstract classes in the abstract method declarations;
~ as an abstract method declaration provides actual implementation, there is no method body; method declaration simply ends with a semicolon, and after the signature without braces "{}", implemented by a method override, override this method is non-members of the abstract class;
~ or use static virtual modifier in the abstract method declaration is wrong;
~ in addition to the statement and call syntax different, the behavior of abstract properties and methods as abstract;
~ use the abstract modifier on a static property is wrong;
~ In a derived class, by using override modifier including property declarations, can override inherited attributes abstract. Abstract class defines:
while its function is to generate the subclass specific properties and to a subclass.
abstract modifier classes can be used with the methods, properties, events, and indexers. Use the abstract modifier in a class declaration to indicate that a class is the only other class of the parent class. Contained in the abstract or abstract class members must be achieved from the subclass of abstract class mark.
using System;
abstract class A
{
public abstract void F();
protected int _x;
public abstract int X
{
get;
set;
}
}
class B:A
{
public override void F()
{
}
public override int X
{
get{return _x;}
set{_x=value;}
}
}
class Test
{
static void Main()
{
B b=new B();
b.X=10;
Console.Write(b.X);
}
}
Virtual methods defined:
Briefly, the method can be subclassed virtual method is overridden, if subclass overrides a virtual method, then uses the logical overwrite operation when the, if not rewritten, using the parent class virtual logic method;
Virtual keys for modification methods, properties, or events indexer declarations and allows rewriting these objects in a derived class.
using System ;
class A
{
public void F( )
{
Console.WriteLine("A.F") ;
}
public virtual void G( )
{
Console.WriteLine("A.G") ;
}
}
class B: A
{
new public void F( )
{
Console.WriteLine("B.F") ;
}
public override void G( )
{
Console.WriteLine("B.G") ;
}
}
class Test
{
static void Main( )
{
B b = new B( ) ;
A a = b;
a.F( ) ;
b.F( ) ;
a.G( ) ;
b.G( ) ;
}
} 输出是:A.F B.F B.G B.G
Actually, the most important thing is an abstract method can not be instantiated, subclass must be mandatory cover its methods. And virtual methods is to provide a choice, may not cover may cover, inherited virtual method in the base class.
Compare summarized as follows:
The difference between abstract methods and virtual methods:
- the difference between abstract methods and virtual methods are: virtual method has an implementation section, and provides the option to override this method in a derived class, on the contrary, an abstract method does not provide an implementation part, to force derived classes covering methods (or a derived class can not be specific type); ~
abstract method declarations in an abstract class only, not the virtual method;
~ abstract methods must be overridden in a derived class, and it is not necessary virtual;
~ method can not abstract method declarations entity may be a virtual method.