C # Basic Knowledge: C # class and structure (3)

Source: Internet
Author: User

1. What are the features of static classes and static members? Implementation code?
Static class and static member are the classes or Members defined using the static keyword. All static class members must be static members. Otherwise, an error is reported. Static classes and members are unique. If it is a static class, it cannot be instantiated, and there is only one load in the memory; if it is a static variable, method, this class can be instantiated, no matter how many times, there is always only one static variable or method.
As follows:
(1) Static members
 
 
Public class StatTicMember
{
Public static string testA = string. Empty;
}
 
Class Program
{
Static void Main (string [] args)
{
// StaticConstruct strc = new StaticConstruct ();
 
// StaticConstruct strcValue = new StaticConstruct (string. Empty );
 
StatTicMember sMember1 = new StatTicMember ();
 
StatTicMember. testA = @ "static member ";
 
Console. WriteLine (StatTicMember. testA );
 
StatTicMember sMember2 = new StatTicMember ();
 
Console. WriteLine (StatTicMember. testA );
 
Console. ReadLine ();
}
}
Public class StatTicMember
{
Public static string testA = string. Empty;
}

Class Program
{
Static void Main (string [] args)
{
// StaticConstruct strc = new StaticConstruct ();
 
// StaticConstruct strcValue = new StaticConstruct (string. Empty );
 
StatTicMember sMember1 = new StatTicMember ();
 
StatTicMember. testA = @ "static member ";
 
Console. WriteLine (StatTicMember. testA );
 
StatTicMember sMember2 = new StatTicMember ();
 
Console. WriteLine (StatTicMember. testA );
 
Console. ReadLine ();
}
}
Result:

 
Static member features:
A. It must be referenced by the class name and cannot be referenced by class objects;
B. No matter how many times the class is instantiated, there is only one region in the memory;
C. If variables or methods outside the method are referenced in static methods, they must also be static, such
 
 
Public class StatTicMember
{
Public static string testA = string. Empty;
 
Public string testB = string. Empty;
 
Public static void Method ()
{
TestA = @ "my"; // correct
 
// TestB = @ "my"; // Error
}
}
Public class StatTicMember
{
Public static string testA = string. Empty;
 
Public string testB = string. Empty;
 
Public static void Method ()
{
TestA = @ "my"; // correct
 
// TestB = @ "my"; // Error
}
}
(2) Static class
 
 
Public static class StaticClass
{
Public static string testA = string. Empty;
 
Public static void StaticMethod ()
{
Console. WriteLine (@ "static method ");
}
}
 
Class Program
{
Static void Main (string [] args)
{
StaticClass. testA = @ "static class ";
 
Console. WriteLine (StaticClass. testA );
 
StaticClass. StaticMethod ();
 
Console. ReadLine ();
}
}
Public static class StaticClass
{
Public static string testA = string. Empty;
 
Public static void StaticMethod ()
{
Console. WriteLine (@ "static method ");
}
}
 
Class Program
{
Static void Main (string [] args)
{
StaticClass. testA = @ "static class ";
 
Console. WriteLine (StaticClass. testA );
 
StaticClass. StaticMethod ();
 
Console. ReadLine ();
}
}
Result:

 
Static features:
A. The members must also be static;
B. Do not instantiate the instance. Reference internal members to use the class name directly;
C. It is a sealed class. (Note: The sealed class cannot be used as a base class, not an abstract class, or cannot be derived .)
D. constructor cannot be included.
When using static classes and members, static classes cannot be used in a flood, because once loaded, there is a zone in the memory, no matter whether you use it or not, it is all there. Memory usage. It can be used in the following scenarios:
A. global variables are a variable used throughout the project, and the value is not easily changed, even if all modules need to be changed, they must be reflected.
B. Do not operate on instance data, and do not associate methods with specific classes in the Code, such as some methods in the Math class.
2. What are the features of the sealing class? Implementation code? Why use a seal class?
A sealed class is a type modified with the sealed keyword. It aims to prevent derivation, that is, this type cannot be inherited.
Features:
It cannot be used as a base class, cannot be abstract, and the calling of the sealing class is faster.
 
 
Public sealed class SealedClass
{
Public string testA = string. Empty;
}
Public sealed class SealedClass
{
Public string testA = string. Empty;
}
3. What is an abstract class? Features? Implementation code? What is the difference between an interface and an abstract class?
Abstract classes are classes modified with abstract keywords. It is used to derive multiple classes and share the common methods and attributes of the base class.
 
 
Public abstract class AbstractClass
{
Public abstract void CommonMethod ();
}
 
Public class ChildClass1: AbstractClass
{
 
Public override void CommonMethod ()
{
Console. WriteLine (@ "Implementing Public method 1 ");
}
}
 
Public class ChildClass2: AbstractClass
{
 
Public override void CommonMethod ()
{
Console. WriteLine (@ "Implementing Public method 2 ");
}
}
 
Class Program
{
Static void Main (string [] args)
{
ChildClass1 chc1 = new ChildClass1 ();
 
Chc1.CommonMethod ();
 
ChildClass2 chc2 = new ChildClass2 ();
 
Chc2.CommonMethod ();
 
Console. ReadLine ();
}
}
Public abstract class AbstractClass
{
Public abstract void CommonMethod ();
}
 
Public class ChildClass1: AbstractClass
{
 
Public override void CommonMethod ()
{
Console. WriteLine (@ "Implementing Public method 1 ");
}
}
 
Public class ChildClass2: AbstractClass
{
 
Public override void CommonMethod ()
{
Console. WriteLine (@ "Implementing Public method 2 ");
}
}
 
Class Program
{
Static void Main (string [] args)
{
ChildClass1 chc1 = new ChildClass1 ();
 
Chc1.CommonMethod ();
 
ChildClass2 chc2 = new ChildClass2 ();
 
Chc2.CommonMethod ();
 
Console. ReadLine ();
}
}
Result:
 

 
Differences between abstract classes and interfaces:
A. Classes are abstract objects. abstract classes can be interpreted as classes as objects. abstract classes are called abstract classes. The interface is just a behavior specification or provision. Microsoft's custom interface always carries the able field to prove that it represents a category "I can do it... ". abstract classes are more defined in a series of closely related classes, while interfaces are mostly classes with loose relationships but all implement certain functions;
B. The interface basically does not have any specific characteristics of inheritance. It only promises the methods that can be called;
C. A class can implement several interfaces at a time, but only one parent class can be extended;
D. interfaces can be used to support callback, but inheritance does not;
E. abstract classes cannot be sealed;
F. The specific methods implemented by abstract classes are virtual by default, but the interface methods in the class implementing interfaces are non-virtual by default. Of course, they can also be declared as virtual;
G and (Interface) are similar to non-abstract classes. abstract classes must provide their own implementations for all the members of interfaces listed in the base class list of this class. However, the abstract class is allowed to map interface methods to abstract methods;
H. abstract classes implement a principle in oop that separates mutable from immutable. Abstract classes and interfaces are defined as immutable classes and can be implemented by subclasses;
I. A good interface definition should be specific and functional, rather than multi-functional, otherwise it will cause interface pollution. If a class only implements a function of this interface and has to implement other methods in the interface, it is called interface pollution;
J. If an abstract class implements an interface, you can map the methods in the interface to an abstract class instead of implementing the methods in the abstract class.
 

Author: Poplar

Related Article

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.