What are the advantages of making the constructor of a class private in JAVA? This is the demand of single mode. If the whole application only needs 1 instances of a class, or only the same instance, it should be set as a single instance. In WEB applications, if it is stateless (that is, a class that does not need to record user status), it can be designed as singleton.
There is no difference between good and bad design patterns, but they are more suitable for use.
Myrectangle Private 1。 Define a rectangular class named MyRectangle. There are four private integer fields in the class MyRectangle.
{
int,bottom
int left,right
Public:
MyRectangle( int t,int b,int l,int r){ = t; bottom = b; Left = l;; Right = r;; }
...
};
What are the benefits of defining private data members in C++ classes? It is equivalent to encapsulation: it avoids any modification, and because it is generally accessed through a function, it can return a value after being processed in a function. For example, time is actually stored in the form of an integer value, but you may only care about seconds, or hours, or hope to return it in the form of hh:mm:ss, so you can only change the function without redesigning variables. There should be other benefits. I suggest you read the expert's blog summary.
Can C++ define private static constant arrays in classes? Of course.
use
Private building
Q: Can C++ define private static constant array functions in a class?
For example:
# include & lt string & gt
Level a
Public:
...
Private:
Static constant int I = 0;;
}
However, it should be noted that the above static constant variables, except for the int type and enumeration type, can be initialized with the int constant expression in the class definition, and the rest must be explicitly initialized in the implementation file, and the const keyword must be declared before initialization.
Therefore, if you define a static constant array, it must look like this:
Yes. H file
# include & lt string & gt
Level a
Public:
...
Private:
Static constant string array [3];
}
Yes. Cpp file.
const string A::array[3] = {"ab "," bc "," db " };
This is because, although const constant must be assigned a value when it is defined, it is preceded by a static keyword, so it has a static nature, that is, it must be explicitly defined outside the class definition (except of course the int type and enumeration mentioned above).
I hope I can help you!
Why constructors in java enum classes must be privately compiled from "logactioncode". java "?
The common final class LogActionCode extends java.lang.Enum{
public static final LogActionCode ADMIN _ log in;
public static final LogActionCode ADMIN _ CREATE _ USER;
public static final LogActionCode ADMIN _ EDIT _ USER;
public static final LogActionCode ADMIN _ DELETE _ USER;
public static final LogActionCode ADMIN _ CREATE _ RATE;
public static final LogActionCode ADMIN _ EDIT _ RATE;
public static final LogActionCode ADMIN _ DELETE _ RATE;
public static final LogActionCode ADMIN _ CREATE _ INTEGRAL;
public static final LogActionCode ADMIN _ EDIT _ INTEGRAL;
public static final LogActionCode ADMIN _ DELETE _ INTEGRAL;
public static final LogActionCode ADMIN _ CREATE _ EXPENSE;
public static final logaction code ADMIN _ EDIT _ EXPENSE;
public static final logaction code ADMIN _ DELETE _ EXPENSE;
public static final LogActionCode USER _ log in; Static {}; Public static logactioncode value of (long);
Public static java.lang.string getactionmsg (java.lang.long);
public Java . lang . long get code();
public Java . lang . string getMsg();
Public static LogActionCode[]values(); ();
Public static logactioncode value of (java.lang.string); }
When an object has multiple types, we usually use Enum. Any type is an instance of an object. All instances of an object of a type will be initialized in a static block without external instantiation of the object (external instantiation is meaningless). Of course, you don't need a public constructor. Of course, attributes also belong to every type instance, and you can define them as public, but according to the idea of object packaging, it is best not to disclose object attributes.
The content comes from: J Art has known the answer for ten years in Baidu.
Enumeration is designed as singleton pattern, that is, enumeration will be instantiated by JVM at load time, and all enumerations defined in enumeration class will be instantiated. In order to ensure the unique instantiation of each enumerated class element, the JVM will not allow external new, so the constructor will be designed as private to prevent users from generating instances to destroy uniqueness.
Compiled from "LogActionCode.java" public final class logactioncode extension java.lang.enum {public static final logactioncode admin _ login; public static final LogActionCode ADMIN _ CREATE _ USER; public static final LogActionCode ADMIN _ EDIT _ USER; public static final LogActionCode ADMIN _ DELETE _ USER; public static final LogActionCode ADMIN _ CREATE _ RATE; public static final LogActionCode ADMIN _ EDIT _ RATE; public static final LogActionCode ADMIN _ DELETE _ RATE; public static final LogActionCode ADMIN _ CREATE _ INTEGRAL; public static final LogActionCode ADMIN _ EDIT _ INTEGRAL; public static final LogActionCode ADMIN _ DELETE _ INTEGRAL; public static final LogActionCode ADMIN _ CREATE _ EXPENSE; public static final logaction code ADMIN _ EDIT _ EXPENSE; public static final logaction code ADMIN _ DELETE _ EXPENSE; public static final LogActionCode USER _ log in; Static {}; Public static logactioncode value of (long); Public static java.lang.string getactionmsg (java.lang.long); public Java . lang . long get code(); public Java . lang . string getMsg(); Public static LogActionCode[]values(); (); Public static logactioncode value of (java.lang.string); We usually use Enum when there are multiple types of an object. Any type is an instance of an object. All instances of an object of a type will be initialized in a static block without external instantiation of the object (external instantiation is meaningless). Of course, you don't need a public constructor. Of course, attributes also belong to every type instance, and you can define them as public, but according to the idea of object packaging, it is best not to disclose object attributes.
How to call a private method in a parent class? Subclasses in java can access the private variables of the parent class by inheriting public methods.
Even if it is not a parent-child relationship, a class can access its private variables through the public methods of another class.
A subclass is a method of accessing a parent class. Remember that it is a method, not a property.
The method of the parent class operates on private properties, regardless of the subclass.