C# basic knowledge compilation: C# classes and structures (3)

黄舟
Release: 2017-02-10 15:25:03
Original
1533 people have browsed it

1. What are the functional features of static classes and static members? Implement code?
Static classes and static members refer to classes or members defined using the static keyword. All members of a static class must be static members, otherwise an error will be reported. One of the characteristics of static classes and members is that they are unique. If it is a static class, it cannot be instantiated, and there is only one loaded in the memory; if it is a static variable or method, if this class can be instantiated, no matter how many times it is instantiated, there will always be only one static variable or method.
are 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 = @"静态成员";

            Console.WriteLine(StatTicMember.testA);

            StatTicMember sMember2 = new StatTicMember();

            Console.WriteLine(StatTicMember.testA);

            Console.ReadLine();
        }
    }
Copy after login

Result:

Characteristics of static members:
a. Must be referenced through the class name, Cannot be referenced by objects of the class;
b. No matter how many times the class is instantiated, there is only the same area in the memory;
c. If variables or methods outside the method are referenced in a static method, they must also be Static, such as

    public class StatTicMember
    {
        public static string testA = string.Empty;

        public  string testB = string.Empty;

        public static void Method()
        {
            testA = @"my";//正确

            //testB = @"my";//错误
        }
    }
Copy after login

(2), static class

  public static class StaticClass
    {
        public static string testA = string.Empty;

        public static void StaticMethod()
        {
            Console.WriteLine(@"静态方法");
        }
    }

    class Program
    {
        static void Main(string[] args)
        {
            StaticClass.testA = @"静态类";

            Console.WriteLine(StaticClass.testA);

            StaticClass.StaticMethod();

            Console.ReadLine();
        }
    }
Copy after login

Result:

Static class characteristics:
a. Members must also be static;
b. It cannot be instantiated. Use the class name directly to reference internal members;
c. It is a sealed class; (Note: Sealed class means that this class cannot be used as a base class, and cannot be an abstract class, that is, it cannot Derived. )
d. Cannot contain constructors.
When using static classes and members, static classes cannot be used extensively, because once it is loaded, there is an area in the memory, and it is there whether you use it or not. takes up memory. It can be used in the following situations:
a. Global variable, a variable used in the entire project, and the value cannot be changed easily, even if all modules are changed, they must respond.
b. Methods that do not operate on instance data and are not associated with specific classes in the code, such as some methods in the Math class.
2. Sealed functional features? Implement code? Why use sealed classes?
A sealed class refers to a class modified with the sealed keyword. Its purpose is to prevent derivation, that is, this class cannot be inherited.
Features:
Cannot be used as a base class, cannot be abstracted, and the call to a sealed class is faster.

    public sealed class SealedClass
    {
        public  string testA = string.Empty;
    }
Copy after login

3. What is an abstract class? Features? Implement code? What is the difference between interface and abstract class?
Abstract class refers to a class modified with the abstract keyword. Its function is to derive multiple classes and share the public methods and properties of the base class.

   public abstract class AbstractClass
    {
        public abstract void CommonMethod();
    }

    public class ChildClass1 : AbstractClass
    {

        public override void CommonMethod()
        {
            Console.WriteLine(@"实现公用方法1");
        }
    }

    public class ChildClass2 : AbstractClass
    {

        public override void CommonMethod()
        {
            Console.WriteLine(@"实现公用方法2");
        }
    }

    class Program
    {
        static void Main(string[] args)
        {
            ChildClass1 chc1 = new ChildClass1();

            chc1.CommonMethod();

            ChildClass2 chc2 = new ChildClass2();

            chc2.CommonMethod();

            Console.ReadLine();
        }
    }
Copy after login

Result:


The difference between abstract class and interface:
a. A class is an abstraction of an object. Abstract classes can be understood as treating classes as Objects are abstracted into classes called abstract classes. The interface is just a specification or regulation of behavior. Microsoft's custom interface is always followed by an able field, which proves that it expresses a class "I can do...". Abstract classes are more defined in a series of closely related Between classes, most interfaces are loosely related but all implement a certain function;
b. The interface basically does not have any specific characteristics of inheritance, it only promises methods that can be called;
c , A class can implement several interfaces at a time, but can only extend one parent class;
d. Interfaces can be used to support callbacks, but inheritance does not have this feature;
e. Abstract classes cannot be sealed;
                                                                              ##                                               # Similar to classes, an abstract class must also provide its own implementations for all members of the interfaces listed in the class's base class list. However, abstract classes are allowed to map interface methods to abstract methods;
h. Abstract classes implement a principle in oop, which separates the mutable and the immutable. Abstract classes and interfaces are defined as immutable, and the mutable ones are left to subclasses for implementation;
i. A good interface definition should have specific functionality, not multi-function, otherwise the interface will become pollute. If a class only implements one function of the interface and has to implement other methods in the interface, it is called interface pollution;
j. If the abstract class implements the interface, the methods in the interface can be mapped to the abstract class As an abstract method in the interface, it does not need to be implemented, but the methods in the interface are implemented in the subclass of the abstract class.

The above is the above content. For more related content, please pay attention to the PHP Chinese website (www.php.cn)! For more related content, please pay attention to the PHP Chinese website (www.php.cn)!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!