C # Learning Basic Concepts 25 Ask 11-15_c# tutorials

Source: Internet
Author: User
11. Can I use abstract functions to override virtual functions in a base class?

For:

OK


You need to use new to modify the modifier explicitly declaration to indicate that the implementation of the function in the base class is hidden

Or add the override modifier to indicate that the abstract overrides the implementation of the function in the base class

Example:

Class BaseClass
{
public virtual void F ()
{
Console.WriteLine ("Baseclass.f");
}
}
Abstract class Deriveclass1:baseclass
{
public abstract new void F ();
}

Thanks to Watson Hua (http://huazhihao.cnblogs.com/) 's guidance
He reminded me that I can also abstract a virtual method that overrides a base class in this way
Abstract class Deriveclass2:baseclass
{
Public abstract override void F ();
}

12. Can the sealed class have virtual functions?

For:

Can, the virtual function in the base class converts the implicit to a non-virtual function, but the sealed class itself cannot add a new virtual function

Example:

Class BaseClass
{
public virtual void F ()
{
Console.WriteLine ("Baseclass.f");
}
}
Sealed class Deriveclass:baseclass
{
The virtual function f in the base class is implicitly converted to a non-virtual function

Cannot declare new virtual function g in sealed class
public virtual void G ()
//{
Console.WriteLine ("DERIVECLASS.G");
//}
}

13. What is a property accessor?

For:

Property accessor, including a get accessor and a set accessor for a read-write operation for a field, respectively

The main purpose of the design is to implement the encapsulation idea in object-oriented (OO). According to the idea, the field is best set to private, a sophisticated class is best not to directly set the field to the public to provide direct access to the client caller

Also note that the property itself is not necessarily associated with the field



Can 14.abstract be used with virtual? Can I use it with override?

For:

The abstract modifier cannot be used with static, virtual modifiers

The abstract modifier can be used with override, see 11th

Example:


Using System;
Using System.Collections.Generic;
Using System.Text;

Namespace Example14
{
Class BaseClass
{
public virtual void F ()
{
Console.WriteLine ("Baseclass.f");
}
}
Abstract class Deriveclass1:baseclass
{
Here, abstract can be used with override.
Public abstract override void F ();
}
Class Program
{
static void Main (string[] args)
{
}
}
}

15. What members can the interface contain?

For:

Interfaces can contain properties, methods, index indicators, and events, but they cannot contain constants, fields, operators, constructors, and destructors, and they cannot contain any static members

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.