First, the concept of the class:
1. According to the actual situation, we define ourselves as a complex type.
2. Three main characteristics of the class: encapsulation, inheritance, polymorphism.
3. Definition of class: keyword is class
[Public/private]class name
{
Definition of a member variable; (definition of a field)
[public/private] Variable type variable name;
The definition of a property;
Public Property Type property name
{
get{}
set{}
}
Definition of a member function; (definition of a method)
[Public/private] return type function name (formal parameter list)
{
}
}
4. Use of classes:
The instantiation of the class-generates the object.
Class nameReference name= new class name ();
Call the members of the object:
Reference name. Member Variable Name
Reference name. property name
Reference name. Member Method Name ()
Second, Access decoration:
Public-the curly braces inside and outside of the class can be accessed.
Private-the curly braces inside the class can be accessed. (Default type)
Third, the definition and use of attributes.
Function: Used to assign and value a private member variable safely.
Defined:
Public Property Type property name
{
Get//Read part.
{
return member variable name.
}
Set//Settings section
{
member Variable name = value;
}
}
public int Height
{
Get
{
return _height;
}
Set
{
if (value>0 && value<300)
{
_height = value;
}
Else
{
Console.WriteLine ("Error");
}
}
}
Third, the use of methods:
1. The definition of a method is the definition of a function.If the method is private, it is not called by the outside world, we often call it the tool function.
2. Overloads of the method. Method names are the same, parameters are different (type, number)。
Method naming and memory are convenient.
Improve code reuse and reduce duplication.
Four, the constructor function.
The function that the object is called automatically when it is instantiated. You cannot explicitly invoke the constructor of the current object using code.
Defined:
class{
[Public/private] class name ()
{
}
}
Characteristics:
1. When an object is instantiated, it is automatically invoked.
2. It is the first member function to be executed. The new class name (), which is actually a call to the constructor.
3. Constructors can be overloaded.
How do I call other constructors in the same class?
Public Feixingqi (string name)
{
name = name;
}
Public Feixingqi (String name,int count,string type):This (name)This is where the public Feixingqi (string name) constructor is called above
{
_chibang = new Chibang ();
_chibang._type = Type;
_chibang._count = Count;
}
This represents the current object. This () invokes the other constructors of the current object. This.xxxx calls the other members of the current object.
Call the This () of the other constructor, not in the {} of the constructor. Written after the declaration of the constructor function.
4. If the declaration constructor is not displayed, the system automatically adds a default constructor. Once the constructor is explicitly declared, the default constructor is no longer automatically generated by the system.
Use of the site environment:
Common use in constructors to perform some initialization work on objects (assigning an initial value to a member variable)。
Constructors are typically used to initialize private data fields of a class.
Practice:
Namespace ConsoleApplication28
{
Class Program
{
Class Cat
{
private string Name;
public string _name
{
get {return Name;}
set {Name = value;}
}
Public Cat (string name)
{
name = name;
}
public void Cry ()
{
Console.WriteLine (name+ ": Meow");
}
}
Class Mouse
{
public void Runaway ()
{
Console.WriteLine ("The Cat is coming, quick to withdraw");
}
}
Class Humen
{
private string Name;
public string _name
{
get {return Name;}
set {Name = value;}
}
public void Awake ()
{
Console.WriteLine (name+ ": Stupid cat, stop barking");
}
}
Class House
{
Private Cat _cat;
Private Mouse _mouse;
Private Humen _xiaoxing=new humen ();
Public House (string catname,string humenname)
{
_mouse = new Mouse ();
_cat = new Cat (catname);
_xiaoxing._name = Humenname;
}
public void Start ()
{
_cat.cry ();
_mouse.runaway ();
_xiaoxing.awake ();
}
}
static void Main (string[] args)
{
House H = new House ("Black Cat Sheriff", "Harry");
H.start ();
Console.ReadLine ();
}
}
}
Five, static members.
Object-oriented (2)