Personal understanding of C# related knowledge points (to be added in succession)

Object:

  1. Definition: The mapping and embodiment of an entity in the real world in computer logic. For example, a book, a person, or a car are all objects.
  2. Objects have properties and methods (which can be understood as behaviors and functions). For example, the color of a car is an attribute, and a car's ability to start and whistle are its methods.

kind:

  1. Definition: A collection of objects with the same data structure and the same operations

  2. Classes are object templates, and objects are the concretization of classes.

  3. It can be understood as a class of objects in nature. For example, cars are a class, and this car is an object of the class; birds are a class, and sparrows are an object of the bird.

  4. public Public class, indicating that the outside world can access it without restrictions
    private Private class, the class can only be accessed by members of this class
    internal Internal class, only this program can access this class
    protected Protected class, can only be accessed by members of this class or derived classes
    abstract Abstract classes cannot be instantiated and can only be used as base classes.
    sealed Sealed class, cannot be a base class

Class members:

  1. Constants, methods, indexers, properties, events, fields, operators, constructors, destructors, static destructors

  2. public Allow access from outside the class with minimal restrictions
    private Do not allow external and derived classes to access
    protected External access is not allowed, but access by derived classes is allowed
    internal Only allow access to classes within the same namespace
    readonly Read only
  3. If default modifier is used, defaults to private

Static members:

  1. Modify with static
  2. Belongs to the class and is used through "class name.static member name"

Instance members:

  1. Not modified with static
  2. Owned by objects belonging to the class, used through "object name.member name"

Static method:

  1. It belongs to the class and can be used through "class name. method name"; it cannot be used through "object name. method name".
  2. Static methods are modified with static
  3. Static methods are always in memory and can be called very quickly
  4. Static methods can only access static members of the class

Instance methods (non-static methods):

  1. Owned by the object of the class, used through "object name.method name"
  2. Those without static modification are instance methods.
  3. Can access all members of the class
    class Person
    {
    
    
        public string name; //公有成员名字
        public static int no;   //静态成员编号
        int age;        //私有成员年龄,缺省为private
        protected string sex;   //保护成员性别
        public static void A()
        {
    
    
            Console.WriteLine("这是一个静态方法");
            //name = "Tom"; //错误,不可以访问实例字段
            no = 9; //正确,可以访问静态字段
        }
        public void B()
        {
    
    
            name = "Tom";
            no = 10;//正确,可以访问静态字段
            Console.WriteLine("这是一个实例方法");
        }
    }
    class A
    {
    
          
        static void Main(string[] args)
        {
    
    
            Person Jack = new Person(); //实例化Person类,即创建“Jack”对象
            Person.A(); //使用类的静态方法
            Jack.B();   //使用类的实例方法
            //Jack.A(); 错误,对象无法访问类的静态方法
            Console.WriteLine(Person.no);
        }
    }

Sealing type:

  1. Do not allow other classes to inherit
  2. modified with sealed
  3. Usually there are some classes that have fixed functions and are used to complete certain standard functions.

Sealing method:

  1. modified with sealed
  2. Sealed methods prevent overriding of the method in derived classes, that is, sealed methods cannot be overridden
  3. Sealed classes and sealed methods can be understood as a template or a standard.

Field

  1. Definition: variables defined directly in the class

  2. 声明格式:
    [字段修饰符] 字段类型 字段名列表;
    public string name;
    int age;
    protected string sex;
    public static int no;
    
  3. As above, add static to modify it into a static field. Static fields can only be accessed through **"class name.field name"**

Attributes

  1. 定义示例:
    class Person
    {
          
          
        private string name;	//私有字段name
        public string Name		//共有属性Name
        {
          
          
            get{
          
          return name;}
            set{
          
          name=value;}
        }
    }
    
  2. Get must use return, set must use value

  3. Use public attributes to assign values ​​to class members from outside the class

  4. The difference between attributes and fields

    • Properties can be read-only or write-only, fields cannot
    • get can return some calculated or processed data
  5. When to use attributes? When to use fields?

    • To record the state of things inside the class, use fields (internal fields)
    • To announce the status of things to the outside world, use attributes (external attributes)

Method inheritance, addition, overloading, hiding, and rewriting

  1. Inheritance: The methods of the parent class are automatically inherited by the subclass
  2. Add: Add new methods to subclasses
  3. Overloading: The method name is the same as the parent class, but the signature (parameters) is different
  4. Hidden: The method name is the same as the parent class, and the front (parameters) are also the same
  5. Rewriting: If the method of the parent class is modified with abstract or virtual, then the subclass defines a method with the same name and signature as the parent class, and the subclass method is modified with override before it.
method hiding
public class BaseClass{
    
    
    public void DoWork() {
    
    	Console.WriteLine("父类方法");	}
}
public class DerivedClass:BaseClass{
    
    
    public new void DoWork() {
    
    	Console.WriteLine("子类方法");	}
}
class Program{
    
    
    DerivedClass dc=new DerivedClass();
    dc.Dowork();	//调用子类的DoWork方法
}
//隐藏方法即在子类中创建同名方法,需要使用new 关键字
//那么是否还能调用父类的方法呢?当然能,需要使用base关键字
public class DerivedClass:BaseClass{
    
    
    public new void DoWork() {
    
    	Console.WriteLine("子类方法");	}
    public void A(){
    
    
        this.DoWork();//调用子类方法
   		base.Dowork();//调用父类方法
    }  
}
Virtual methods and their overrides
class A {
    
    
    public virtual void VirtualMethod() {
    
     Console.WriteLine("基类虚方法"); }
}
class B : A {
    
    
    public override void VirtualMethod(){
    
     Console.WriteLine("子类重写基类虚方法"); }
}
Abstract class:
  1. The function of an abstract class in C# is to provide a public interface for its derived classes
  2. Abstract classes can only be used as base classes for other classes and cannot create objects.
  3. An abstract method must belong to an abstract class , and there may not necessarily be an abstract method in an abstract class.
  4. If a class inherits an abstract class, then all abstract methods in the abstract class must be overloaded in the subclass! ! ! ! ! ***
Abstract method:
  1. No specific implementation code is provided and can only be defined in abstract classes .Just define it, don’t write anything in it
  2. Subclasses (derived classes) are required to override the abstract methods of the base class to implement specific functions. Override is used to indicate rewriting.
abstract class Vehicle{
    
     //注意抽象类,抽象方法一定要在抽象类中!!!!
    public abstract void Whistle(); //声明抽象方法,只能这样写,大括号都不要写!!!
}
class Car : Vehicle{
    
    
    public override void Whistle(){
    
    	Console.WriteLine("抽象方法实现"); }
}

Guess you like

Origin blog.csdn.net/u014295602/article/details/115309700