C # learning basic concepts question 25 11-15

Source: Internet
Author: User

11. Can I use Abstract Functions to override virtual functions in the base class?

A:

Yes

The new modifier must be explicitly declared to hide the implementation of the function in the base class.

Or an override modifier is added, indicating that the implementation of the function in the base class is overwritten.

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/) for your guidance
// He reminded me that I can use this method to abstract and override the virtual method of the base class.
Abstract class DeriveClass2: BaseClass
{
Public abstract override void F ();
}

12. Can there be virtual functions in the seal class?

A:

Yes. The virtual functions in the base class are implicitly converted to non-virtual functions, but the new virtual functions cannot be added to the sealed class itself.

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

// The new virtual function G cannot be declared in the seal class.
// Public virtual void G ()
//{
// Console. WriteLine ("DeriveClass. G ");
//}
}

13. What is an attribute accessor?

A:

Property accessors, including get accessors and set accessors, are used to read and write fields respectively.

The purpose of the design is to realize the encapsulation idea in object-oriented (OO. According to this idea, it is best to set the field to private. It is best not to set the field to public for direct access by the client caller in a delicate class.

In addition, note that attributes are not necessarily associated with fields.

14. can abstract be used with virtual? Can it be used with override?

A:

Abstract modifiers cannot be used with static or virtual modifiers.

Abstract modifier can be used with override. For details, refer to 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 together with override
Public abstract override void F ();
}
Class Program
{
Static void Main (string [] args)
{
}
}
}

15. What members can an interface contain?

A:

An interface can contain attributes, methods, index indicators, and events, but cannot contain constants, fields, operators, constructors, and destructor, and 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.