首页 > 代码库 > 扣响C#之门笔记-第八章

扣响C#之门笔记-第八章

8.1 以对象为成员

1.类的成员不光是可以使int,double等基本类型,也可以是其他类的对象;

 class Program    {        static void Main(string[] args)        {            date d = new date(1992, 5, 18, new Time(12, 20, 5));        }    }    class date    {        int year;        int month;        int day;        Time  t;  //(1)含有其他对象成员的类        public date(int year, int month, int day, Time t)        {            this.year = year;            this.month = month;            this.day = day;            this.t = t;        }    }    class Time     {        int houer;        int minute;        int second;        public Time(int houer, int minute,  int second )        {            this.houer = houer;            this.minute = minute;            this.second = second;                    }    }

8.2 静态常量

(1)静态变量就是用satic 关键字修饰的变量

(2)静态变量只能使用类名引用不能使用对象调用

    class Program    {        static void Main(string[] args)        {            S s = new S();            //s.number=5;// error (2)静态变量只能使用类名引用不能使用对象调用            s.setSnumber();            Console.WriteLine(S.number);            S.number = 4;            Console.WriteLine(S.number);            Console.ReadKey();                }    }    class S    {        public static int number;  //(1)静态变量就是用satic 关键字修饰的变量        public S()        {            number = 0;        }        public void setSnumber()        {            number = 5;        }    }

(3)静态方法就是用satic 关键字修饰的方法

(4)静态方法只能使用类名引用,不能使用对象引用

    class Program    {        static void Main(string[] args)        {            A.print();            A a = new A();            //a.print(); error (2)静态方法只能使用类名引用,不能使用对象引用        }    }    class A    {        public static void print()  //(1)静态方法就是用satic 关键字修饰的方法        {            Console.WriteLine("print");            Console.ReadKey();        }       }

8.3常量

8.3.1 const常量

(1)const 常量定义方法:访问权限+ const +类型+常量名=初始值;

(2)const 常量必须在定义时候同时赋值;

(3)const 常量是隐式静态的(不能用static关键字修饰),因此只能使用类名引用

    class Program    {        static void Main(string[] args)        {            Console.WriteLine(circle.PI);//(3)const 常量是隐式静态的(不能用static关键字修饰),因此只能使用类名引用            Console.ReadKey();        }    }    class circle    {       // public const double PI ;  (2)const 常量必须在定义时候同时赋值;       // PI=3.14;        public const double PI = 5;//(1)const 常量定义方法:访问权限+ const +类型+常量名=初始值;    }

8.3.2 readonly常量

(1)readonly 常量定义方法:同const

(2)readonly 常量是非静态常量,每个对象可以有不同的值,可以在构造函数中初始化(也可在定义时候赋值);

(3)由于readonly 常量是非静态的,和普通变量一样,使用对象引用,不能使用类名引用

    class Program    {        static void Main(string[] args)        {            hotel h1 = new hotel(20);            hotel h2 = new hotel(30);            Console.WriteLine(h1.number);//(3)由于readonly 常量是非静态的,和普通变量一样,使用对象引用,不能使用类名引用            Console.WriteLine(h2.number);            //Console.WriteLine(hotel.number); //error            Console.ReadKey();         }    }}class hotel{    public readonly int number=0;//(1)readonly 常量定义方法    public hotel(int number)//(2)readonly 常量是非静态常量,每个对象可以有不同的值,可以在构造函数中初始化(也可在定义时候赋值);    {        this.number = number;    }

8.4 重载

(1)函数名相同,参数个数或者参数类型不同 构成重载

(2)函数重载调用的原则是“最佳匹配”,系统或调用参数最匹配的那个函数

class Program    {        static void Main(string[] args)        {            //(2)函数重载调用的原则是“最佳匹配”,系统或调用参数最匹配的那个函数            Console.WriteLine(calculate.add(1,1));            Console.WriteLine(calculate.add(1.1, 1));            Console.WriteLine(calculate.add(1, 1,1));            Console.ReadKey();        }    }    class calculate    {     public static int add(int a, int b)    {        return a+b;    }    public static double add(double a, double b)//(1)函数名相同,参数个数或者参数类型不同 构成重载    {        return a + b;    }    public static int add(int a, int b,int c)    {        return a + b+c;    }        }

8.5 待续

 

扣响C#之门笔记-第八章