11. Can abstract functions be used to rewrite virtual functions in base classes?
answer:
Can
It needs to be declared explicitly using the new modifier, indicating that the implementation of the function in the base class is hidden.
Or add the override modifier to indicate that the abstract rewrites the implementation of the function in the base class.
Example:
class BaseClass
{
public virtual void F()
{
("");
}
}
abstract class DeriveClass1 : BaseClass
{
public abstract new void F();
}
//Thanks to watson hua(/) for his advice
//He reminded me that I can also use this method to abstractly rewrite the virtual method of the base class.
abstract class DeriveClass2 : BaseClass
{
public abstract override void F();
}
12. Can sealed classes have virtual functions?
answer:
Yes, virtual functions in the base class will be implicitly converted into non-virtual functions, but the sealed class itself cannot add new virtual functions.
Example:
class BaseClass
{
public virtual void F()
{
("");
}
}
sealed class DeriveClass : BaseClass
{
//The virtual function F in the base class is implicitly converted into a non-virtual function
//The new virtual function G cannot be declared in the sealed class
//public virtual void G()
//{
// ("");
//}
}
13.What is a property accessor?
answer:
Property Accessor, including the get accessor and set accessor for field read and write operations respectively
Its design purpose is mainly to realize the encapsulation idea in object-oriented (OO). According to this idea, it is best to set the field to private, and it is best not to set the field to public directly for the client to access it directly for the caller.
Also note that the properties themselves are not necessarily related to the fields
Can it be used with virtual? Can it be used with override?
answer:
abstract modifier cannot be used with static and virtual modifiers
The abstract modifier can be used with override, see point 11
Example:
using System;
using ;
using ;
namespace Example14
{
class BaseClass
{
public virtual void 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 an interface contain?
answer:
Interfaces can contain properties, methods, index indicators, and events, but cannot contain constants, domains, operators, constructors, and destructors, and cannot contain any static members.
answer:
Can
It needs to be declared explicitly using the new modifier, indicating that the implementation of the function in the base class is hidden.
Or add the override modifier to indicate that the abstract rewrites the implementation of the function in the base class.
Example:
class BaseClass
{
public virtual void F()
{
("");
}
}
abstract class DeriveClass1 : BaseClass
{
public abstract new void F();
}
//Thanks to watson hua(/) for his advice
//He reminded me that I can also use this method to abstractly rewrite the virtual method of the base class.
abstract class DeriveClass2 : BaseClass
{
public abstract override void F();
}
12. Can sealed classes have virtual functions?
answer:
Yes, virtual functions in the base class will be implicitly converted into non-virtual functions, but the sealed class itself cannot add new virtual functions.
Example:
class BaseClass
{
public virtual void F()
{
("");
}
}
sealed class DeriveClass : BaseClass
{
//The virtual function F in the base class is implicitly converted into a non-virtual function
//The new virtual function G cannot be declared in the sealed class
//public virtual void G()
//{
// ("");
//}
}
13.What is a property accessor?
answer:
Property Accessor, including the get accessor and set accessor for field read and write operations respectively
Its design purpose is mainly to realize the encapsulation idea in object-oriented (OO). According to this idea, it is best to set the field to private, and it is best not to set the field to public directly for the client to access it directly for the caller.
Also note that the properties themselves are not necessarily related to the fields
Can it be used with virtual? Can it be used with override?
answer:
abstract modifier cannot be used with static and virtual modifiers
The abstract modifier can be used with override, see point 11
Example:
using System;
using ;
using ;
namespace Example14
{
class BaseClass
{
public virtual void 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 an interface contain?
answer:
Interfaces can contain properties, methods, index indicators, and events, but cannot contain constants, domains, operators, constructors, and destructors, and cannot contain any static members.