What is facing the object
In the c# learning process, many people will find that facing objects is very abstract, so I will give some examples for a better understanding.
The objects in the face of objects are generally concrete things.
Facing the object is the result. For example, when I open the door, the process of opening is facing the process. The process can be diverse, such as kicking with feet and opening with hands. But the result is that the door is opened, and object-oriented is the result of the door being opened. Object abstract class
Properties of class
Class attributes can be understood as the characteristics of the object, such as the color of the door, the shape of the door, and so on.
Class method
Class can be understood as the behavior of the object, such as the door can be opened
The following code can help you understand
using System namespace duiXiang { class DX { static void Main(string[] args) { student st=new student(); //create object st.name="Zhang San"; //Declarative function st.age=18; st.behaviour(); Console.ReadKey(); } } class student //Class name { public string name; // Properties of class public int age; pubilc void behaviour() //The method of a class is the behavior of an object { Console.WriteLine("{0}Your age is{1},Behavior is learning",this.name,this.age); } } }
The result is:
Zhang San's age is 18 and his behavior is learning
Three characteristics of facing objects
1. Succession
Inherited properties
1. Singleness: a subclass can only have one parent class.
2. Transitivity: intergenerational transmission can be carried out. For example, the subclass of a subclass can inherit the properties of the subclass or use the properties of the parent class.
Base class (parent class) and derived class (subclass): base and derived classes can inherit (or understand to use) the data and functions of the base class. Derived classes can also have their own data.
Initialization of base class: you should first create a parent class (base class) and then create a child class (derived class). You can initialize the parent class in the member initialization list, that is, in the body of the base class.
When considering inheritance, one thing to note is that the relationship between the two classes should be a "belong" relationship. For example, Employee is a Person and Manager is a Person, so both classes can inherit the Person class. However, the Leg class cannot inherit the Person class because the Leg is not a Person.
using System; namespace InheritanceApplication { class Shape { public void setWidth(int w) { width = w; } public void setHeight(int h) { height = h; } protected int width; protected int height; } // Derived class (subclass) class Rectangle: Shape { public int getArea() { return (width * height); } } class RectangleTester //Base class (parent class) { static void Main(string[] args) { Rectangle Rect = new Rectangle(); Rect.setWidth(5); Rect.setHeight(7); // Plot the area of the object Console.WriteLine("Total area: {0}", Rect.getArea()); Console.ReadKey(); } } }
The result is:
Total area: 35
Note: new keyword 1. Can create object 2. Can hide parent class
The subclass does not inherit the constructor of the parent class. But the subclass calls the parameterless constructor of the parent class by default
Internal conversion: a subclass can be assigned to a parent class
Create a subclass object
child c=new child(); father f=c; or new child();
The parent class can inherit the value of the child class.
If a parent class holds an object of a subclass, it can force the parent class to be a subclass
child cc=(child)f;
cc. Name of subclass;
2. Packaging
You can use public,private,protected,internal,protected internal
And other keywords
public: all objects can be accessed;
private: the object itself can be accessed inside the object;
protected: only this class object and its subclass objects can be accessed
internal: objects of the same assembly can be accessed;
protected internal: access is limited to the current assembly or types derived from the containing class
get and set can encapsulate keywords. get and set correspond to readable and writable keywords respectively.
3. Polymorphism
Polymorphism means that the same event will have different results on different objects. For example, in the face of a thing, different people have different results.
Static polymorphism.
The response of a function occurs at compile time. It can also be understood that at compile time, the connection mechanism between a function and an object is called early binding, also known as static binding.
Function overload.
You can have multiple definitions of the same function name in the same scope. The definitions of functions must be different from each other. They can be different types of parameters in the parameter list or the number of parameters. Cannot overload function declarations with only different return types.
example:
using System; namespace PolymorphismApplication { public class TestData { public int Add(int a, int b, int c) { return a + b + c; } public int Add(int a, int b) { return a + b; } } class Program { static void Main(string[] args) { TestData dataClass = new TestData(); int add1 = dataClass.Add(1, 2); int add2 = dataClass.Add(1, 2, 3); Console.WriteLine("add1 :" + add1); Console.WriteLine("add2 :" + add2); } } }
Dynamic polymorphism
You can use the keyword abstract to create abstract classes that provide the implementation of some classes of the interface. When a derived class inherits from the abstract class, the implementation is complete.
Abstract classes contain abstract methods that can be implemented by derived classes. Derived classes have more professional functions.