C# 复习笔记3

面向对象编程:类和对象(二)

  • 以对象为成员
  • 静态成员:描述类的整体特征的量可以用静态变量实现。静态变量在内存中只有一份,为类的所有对象共享。
  • C# 对象初始化
class test:IEquatable
    {
        public int aa { get; set; }
        public string bb { get; set; }
        public bool cc { get; set; }

        public string dd;

        public test(string dd) {
            this.dd = dd;
        }

        public bool Equals(test other)
        {
            return (this.aa == other.aa && this.bb == other.bb && this.cc == other.cc && this.dd == other.dd);
        }
    }
    class Program
    {
        static void Main(string[] args)
        {
            test a = new test("22") { aa = 1, bb = "11", cc = true, dd = "22" };
            test b = new test("22") { aa = 1, bb = "11", cc = true, dd = "224" };

            Console.WriteLine(a.Equals(b));

            ReadLine();
        }
    }
  • 常量成员
  • Circle.PI
    
  • readonly常量:对于const常量,所有对象都有相同的值,但对于readonly常量,每个对象可以有不同的值。我们把readonly常量初始化代码放在构造函数里,这样每一个对象创建时,就会有独立的值。
  •  public readonly int roomNumber;
    
  • 重载(Overload)
  • this关键字:四种用法
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
 
namespace AppConsole
{
 
    /// 
    /// 普通用户
    /// 
    public class User
    {
        /// 
        /// 全局变量
        /// 
        Dictionary dictInfo = null;
 
        /// 
        /// 构造器
        /// 
        public User()
        {
            dictInfo = new Dictionary();
        }
 
        /// 
        /// 构造函数重载
        /// 
        /// 
        /// 
        public User(int UserID, string UserName)
        {
            this.UserName = UserName;
            this.ID = UserID;
        }
 
        /// 
        /// this,第【1】种用法,索引器
        /// 
        /// 
        /// 
        public object this[string name]
        {
            get { return dictInfo[name]; }
            set { dictInfo[name] = value; }
        }
 
 
        /// 
        /// 编号
        /// 
        public int ID { get; set; }
 
 
        /// 
        /// 用户名
        /// 
        public string UserName { get; set; }
 
        /// 
        /// this第【2】种用法,当做参数传递
        /// 
        public void Said()
        {
            new VIP().Say(this);
        }
 
    }
 
    /// 
    /// 会员
    /// 
    public class VIP : User
    {
 
 
        /// 
        /// 积分
        /// 
        public int integral { get; set; }
 
        /// 
        /// 构造函数
        /// 
        public VIP()
        {
            ID = 520;
            integral = 1000;
        }
 
        /// 
        /// this第【3】种用法,通过this()调用无参构造函数
        /// 
        /// 
        public VIP(string UserName)
            : this()
        {
            this.UserName = UserName;
        }
 
        /// 
        /// 构造函数重载
        /// 
        /// 
        /// 
        public VIP(int UserID, string UserName)
            : base(UserID, UserName)
        {
 
        }
 
        /// 
        ///Say方法
        /// 
        /// 
        public void Say([LCQAttribute] User user)
        {
            Console.WriteLine(string.Format("嗨,大家好!我的编号是{0},大家可以叫我{1}!", user.ID, user.UserName));
        }
 
    }
 
    /// 
    /// 静态类,来扩展User类
    /// 
    public static class Helper
    {
 
        /// 
        /// 第【4】种用法: this扩展User类
        /// 
        /// 
        public static void Sing(this User user)
        {
            Console.WriteLine(string.Format("嗨,大家好!我的编号是{0},大家可以叫我{1}!", user.ID, user.UserName));
        }
    }
 
    /// 
    /// 特性类:指定特性仅适用于方法和方法的参数
    /// 
    [System.AttributeUsage(AttributeTargets.Method | AttributeTargets.Parameter)]
    public class LCQAttribute : System.Attribute
    {
 
    }
}
  • 索引:索引可以让我们像数组那样访问类的数据成员。
  • 引用符

以对象为元素的数组

  • 声明以对象为元素的数组的方式和普通数组相同
  •  Cat[] cats = new Cat[5];
    
  • 但是这只是声明了一组“引用符”而已,并没有真正创建对象,下面的语句才具体创建对象,并让每个引用符指向一个对象。
for (int i = 0; i < cats.Length; i++)
{
	cat[i] = new Cat();
}

面向对象编程:继承

  • 继承(Inheritance)是软件重用的一种形式,采用这种形式,可以在现有类的基础上添加新的功能,从而创造出新的类。软件重用鼓励人们重用久经考验和调试的高质量软件,不但节省开发时间,而且提高软件质量。
  • protected成员:使得类的成员既保持封装性又可以在派生类中使用,那么可以把它定义为protected成员。
  • 虚方法的重写:在基类中,用关键字virtual声明虚方法;在派生类中,用关键字override重写基类的虚方法
  • 普通方法的隐藏:要想在派生类中修改基类的普通方法,需要用new关键字隐藏基类中的方法。
  • base关键字:如何在派生类中调用被重写或隐藏的基类方法呢?我们知道,每个对象都可以用this关键字引用自身的成员,同样,也可以用base关键字引用基类的成员。
  • 抽象类和抽象方法:
  • 密封类和密封方法:不能诶继承的类,sealed关键字声明
  • 派生类的构造函数

面向对象编程:多态性

  • 接口:接口用关键字interface定义,接口的名称习惯上以字母I开头。一般情况下,接口中只能包含成员的声明,不能有任何实现代码。接口的成员总是公有的,不需要也不能添加public修饰符,也不能声明为虚方法或静态方法。

你可能感兴趣的:(C#,复习)