C # study notes (3) By Heiyeluren learning "Visual C # entry classic" learning notes
Class definition and inheritance
Define a class using Class keywords: class class1 {// members}
The default defined class is only available to other classes and functions in this project, and we can also define our classes using explicitly: Internal class class1 {// members}
Joining us to let other projects can access our classes, we are defined as public, other projects can also visit: public class class1 {// members}
At the same time, we can also define the class as an abstract class, can only inherit, can not instantiate: public abstract class class1 {// members}
We can also define classes to instantiate and cannot inherit:
Public Sealed Class Class1 {// Members}
A class inherits the abstraction class, then all abstract classes will inherit, in addition, the corresponding access method must be in line with the rules, a public base class, derived class can be public,
Can be private, defined legal: public class mybase {// membrate}
Internal class myclass: mybase {// members}
But if it is a private base class, then the school can only be private, and the definition is illegal: internal class mybase {// members}
Public class myclass: mybase {// membrate}
If there is no base class, then inherit is the base class system.Object, which is a very core base class in C #, basically all the roots of all the base classes are it,.
At the same time, we can also inherit the interface, implement the members of the interface: public class myclass: iMyinterface {// members}
If a class inherits the member of the interface, the member of all interfaces must be implemented in the class. If you don't want to implement the members of the interface, you can use "empty", just let the letter.
There is no code.
If we want to inherit the base class and interface at the same time, the base class must be in front of the interface, definition is illegal: public class myclass: iMyinterface, mybase {// membrate}
The correct specified interface and base class is as follows: public class myclass: mybase, iMyinterface {// membrate}
Of course, we can also specify multiple interfaces at the same time: public class myclass: mybase, iMyinterface, IMYSECONDINTERFACE {// Members}
2. Definition and inheritage of the interface
Declare an interface and declare a class, using interface as keywords:
Interface ImyInterface {// Interface Members}
The same interface also has a private and public access:
Public interface Imyinterface {// interface members}
But Abstract and Sealed cannot be used, because the interface is used to inherit, but it cannot be instantiated.
The interface can also inherit, and the interface is not like class, only one base class, the interface inheritance can have multiple interfaces:
Public Interface ImyInterface: iMyBaseInterface, IMYBASEINTERFACE2 {// interface Inheritance and System.Object are similar, that is, the interface is used to determine the use of that interface, the interface cannot be instantiated.
3. Implementation examples of classes and interfaces
// Define Name Namespace TestApp {// Defines an Abstract Based class PUBLIC Abstract Class MyBase {} // Inherited Abstract Class INTERNAL CLASS MyClass: mybase {}
// Define Interface 1 Public Interface IMYBASEINTERFACE {}
/ / Define Interface 2 INTERNAL INTERFACE IMYBASEINTERFACE2 {}
/ / At the same time inheritably interface 1 and interface 2 INTERNAL INTERFACEFACE: IMYBASEINTERFACE, IMYBASEINTERFACE2 {}
// Inherited base class and interface 1 INTERNAL Sealed class mycomplexclass: myclass, iMyinterface {}
/ / Define the class class class1 {// static MIAN method, generally the application's portal static void main (String [] args) {mycomplexclass myobj = new mycomplexclass (); console.writeline (Myobj.toString () }}}
Basically, it's so much, huh, huh ~~~~ Next, let's talk again! !
Writetime: 2004-12-29 21:00