| |

VerySource

 Forgot password?
 Register
Search
View: 730|Reply: 6

A simple question about classes

[Copy link]

2

Threads

5

Posts

4.00

Credits

Newbie

Rank: 1

Credits
4.00

 China

Post time: 2020-3-12 19:30:02
| Show all posts |Read mode
class c
{
    private c ()
    {
    }
}

What is this private c inside and what does it do?
Reply

Use magic Report

0

Threads

64

Posts

45.00

Credits

Newbie

Rank: 1

Credits
45.00

 China

Post time: 2020-6-7 01:30:01
| Show all posts
The constructor of the class can initialize the data when instantiating the class
Reply

Use magic Report

0

Threads

8

Posts

8.00

Credits

Newbie

Rank: 1

Credits
8.00

 China

Post time: 2020-6-7 13:00:01
| Show all posts
How can it be private? How can it be constructed if it cannot be accessed?
Reply

Use magic Report

0

Threads

52

Posts

34.00

Credits

Newbie

Rank: 1

Credits
34.00

 China

Post time: 2020-6-7 20:00:02
| Show all posts
When a member of a class does not need or should not be instantiated, defining a private constructor can prevent it from being incorrectly instantiated
Reply

Use magic Report

0

Threads

52

Posts

34.00

Credits

Newbie

Rank: 1

Credits
34.00

 China

Post time: 2020-6-8 09:00:01
| Show all posts
The private constructor is a special instance constructor. It is usually used in a class that contains only static members. If a class has one or more private constructors and no public constructor, no other class (except nested classes) is allowed to create instances of that class. E.g:

class NLog

{

    // Private Constructor:

    private NLog() {} public static double e = System.Math.E; //2.71828...

}

Declaring an empty constructor prevents automatic generation of the default constructor. Note that if you do not use an access modifier on the constructor, it is still a private constructor by default. However, the private modifier is usually explicitly used to clearly indicate that the class cannot be instantiated.

When there is no instance field or instance method (such as the Math class) or when a method is called to obtain an instance of the class, a private constructor can be used to prevent the creation of an instance of the class. If all methods in the class are static, consider making the entire class static. For more information, see Static Classes and Static Class Members.

Examples

The following is an example of a class that uses a private constructor.

public class Counter

{

    private Counter() {}

    public static int currentCount;

    public static int IncrementCount()

    {

        return ++currentCount;

    }

}

class TestCounter

{

    static void Main()

    {

        // If you uncomment the following statement, it will generate

        // an error because the constructor is inaccessible:

        // Counter aCounter = new Counter(); // Error Counter.currentCount = 100;

        Counter.IncrementCount();

        System.Console.WriteLine("New count: {0}", Counter.currentCount);

    }

}

Output

New count: 101

Note that if you uncomment the following statement in this example, it will generate an error because the constructor is not accessible due to its protection level:

// Counter aCounter = new Counter(); // Error
Reply

Use magic Report

0

Threads

27

Posts

21.00

Credits

Newbie

Rank: 1

Credits
21.00

 China

Post time: 2020-6-8 13:00:02
| Show all posts
Simply put, you don't want users to initialize this class.
Reply

Use magic Report

2

Threads

5

Posts

4.00

Credits

Newbie

Rank: 1

Credits
4.00

 United States

 Author| Post time: 2020-6-8 23:15:01
| Show all posts
Thank you, especiallypsycholmu.
This problem is basically understood!^-^
Reply

Use magic Report

You have to log in before you can reply Login | Register

Points Rules

Contact us|Archive|Mobile|CopyRight © 2008-2023|verysource.com ( 京ICP备17048824号-1 )

Quick Reply To Top Return to the list