A member Access and properties
In defining the class we have to consider the access rights of the members, here in detail private to deepen the understanding of private; If the members of a class are private then it is illegal to call the class object in another class to access it. The following examples illustrate:
Class per
{
private string name;
public string address;
public int age;
Public per ()
{
Name = "Upagn";
Address = "Changsha";
}
}
The code in the main () function, as shown in the previous illustration,
Errors occur during compilation:
Error "Ceshi. Per.name "inaccessible because it is protected level Limited
For the purpose of property set and Get,set and get, they are generally used to manipulate variables inside a class. Instead of directly manipulating the variables of the class. There is a big effect: easy to maintain. Look at the following program
|
Variables in the actual program call get and set: per Percy = new (); Console.WriteLine (percy.address); Console.WriteLine (Percy.name); Percy.age = 22; Call set to assign to age Console.WriteLine (Percy.age); Call get gets the value of the current object age |
Two About the implementation of abstract classes and abstract methods
Only some members and abstract methods are defined in the abstract class, and the member is not necessarily abstract, but the method must be abstract, and the subclass should be overwritten with the actual function; abstract class is not instantiated. Also, abstract methods define only one method body, no code that is implemented, and overwrite in subclasses, the left side of the table below is an abstract class and its abstract method, and the right class inherits it and implements the abstract method
Public abstract class Myab { public abstract void Hi (string Name); An abstract method cannot have a body. } |
public class Submyab:myab {public override void Hi (string Name) { Console.WriteLine (name + "override void Hi" (string name); } } |
Class can be instantiated and used in this way: Submyab obj = new Submyab (); obj. Hi (obj.city); The difference between an interface and an abstract class is that all members of the interface are abstract and cannot contain constructors, destructors, and static members and constants. An interface can be inherited by multiple classes, a class can inherit multiple interfaces, and implement methods in an interface, which defines an interface for three different shapes:
Public interface ishape { Double getarea () ;//abstract methods can only be written like this }//an area Retange myre = new retange (2, 8) for an instance object of three classes; console.writeline (Myre.getarea ()); Triangle mytri = new triangle (5, 1, 9); Console.WriteLine (Mytri.getarea ()); Circle mycur = new circle; Console.WriteLine (Mycur.getarea ()); |
Public class circle : ishape { Public double radius; Public circle (Double radius) { this.radius = radius; //constructor } Public double getarea () { return (Math.PI) * this.radius * this.radius } } |
Public class retange : ishape { Public  DOUBLE WITDH; Public double height; Public retange (double width, double height) { This.witdh = width; This.height = height;//Constructor } Public double getarea () { RETURN THIS.HEIGHT * THIS.WITDH; } } Output result: |
public class Triangle:ishape { public double Arc1; public double Arc2; public double Arc3; Public triangle (double arc1,double arc2,double Arc3) { This. Arc1 = Arc1; This. ARC2 = ARC2; This. ARC3 = ARC3; Constructors } Public double Getarea () { return this. ARC1 * this. ARC2 * this. ARC3; } } |
The above code please carefully analyze and typing your own computer implementation, do not directly copy. The following section describes C # string processing.