Definition of Class
class Name
{
Class Body
}
Access level of the class
Classes in C + + do not have the so-called access level problem, you can use this class only if you import the header file of the definition class. In C # and Java , not all classes can be used arbitrarily.
In Java , programs are organized as Packages(package), and the package is structured like a folder. If you want to use a class that is defined in another package, in addition to importing the package first, the class you want to use must also be a public class (a class that is defined in the Common keyword). If the class is not decorated with the keyword public when it is defined, the class can only be used in the package that defines it.
In C # , the system follows the organization of "resolution," "Project," and "Source files." The solution is the highest level, and a solution can contain several projects. Then, from the file hierarchy, a "project" can contain several C # source files, a C # source file can contain several namespaces, classes, interfaces, enumerations, and so on. From the level of program elements, a project can contain namespaces, classes, interfaces, and so on.
Like Java , classes inC # are divided into inner classes by access level ("internal" in this case refers to the access level, not the class defined in the Class) and the public class. Inner classes can only be used in this project, and if you want a class to be accessible by a class in another project, you must define it as a public class.
public class is decorated with the common keyword;
Inner Classes can be decorated with the keyword internal , but this is generally not the case, because in C # , a class without the keyword public is an inner class.
General class, abstract class and sealed class
If you use the keyword abstractwhen defining a class, then this class is an abstract class. Abstract classes cannot be instantiated by themselves,
can only be inherited.
If you use the keyword sealedwhen defining a class, then this class is called a "sealed class", just the opposite of the abstract class, which cannot be inherited (similar to the final class in Java ).
If the definition class is not using the keyword abstract or sealed, then this class is a normal class.
Note: The keyword abstract and sealed are mutually exclusive.
All of the above three types can be public or internal .
Inheriting the format of the parent class and implementing the interface
class name : parent class , interface 1, interface 2,...
{}
Like Java ,C # can only inherit one parent class directly, but it is possible to implement multiple interfaces. The inherited parent class differs from Java by using the keyword extends, which implements the interface with the keyword implements. C # uses a similar approach in C + + , that is, using colons.
In the writing format, the list after the colon must be the parent class before, and the interface behind.
Unlike C + +, access level modifiers are not available in the list after the colon.
It is important to note thatC # stipulates that the access level of a subclass cannot be higher than the base class.
If the base class is an abstract class, the derived class must implement all the abstract methods in the base class, otherwise the derived class must be an abstract class.
Of course, subclasses must also implement all the methods of the interfaces they implement.
Definition of the interface
Use the keyword interface to define the interface, and specify that the interface name must begin with an uppercase letter I .
The interface can also be internal or public. However, you cannot use the keyword abstract and sealedon the interface.
Interface can have parent interface
Class 1: Definition in C # (a bit messy)