奥萨瓦·本·萨卡

导航

C#初学第十三天

1. 引用类型都在托管空间,
值类型都在栈空间里,都是非托管区
任何类型复制的时候,或者是方法参数传递的时候,都将变量复制了一份
2. 结构里面的构造函数只用来初始化,
3. 每产生一个实例,就找到所有实例,并将所有的都+1,
静态成员,有static 修饰,当编译时只产生一个,创建对象时不产生(将静态成员看作全局成员)
静态成员调用直接是类名.成员名
4. 继承
父类 子类
父类提供子类应该具备的东西,子类包含父类相同的东西,同时还具备自己的东西
继承的语法
[访问修饰符]class类名:父类的类名
{
//成员
}
除了构造方法,所有的都继承下来
5. public internal protected private
6. 继承就是为了减少代码的而重复采用的方法
构造方法的调用
new 子类的时候,实际上先调用了父类的构造方法
在调用构造方法时默认的是调用无参数的构造方法
显示的指定父类的构造方法
base(参数列表)
7. 在写类的时候,如果显示的继承关系,系统默认你的类继承与object类
所有类都是直接或剪剪的继承了object类
object类是根类
8. 子类可以赋值给父类(子类可以隐式的转换成父类)
父类变量=子类变量;
父类在执行方法的时候,只能访问到父类的方法,子类的方法无法访问
父类不能隐士的转换为子类,不是所有情况的父类可以显示的转换为子类
父类=子类
子类=(子类类型)父类
9. is和as
实例名is 类型名
如果实例是该类型,则返回true
如果实例无法转换为该类型,则返回false
该类型的变量=实例名as类型名
如果实例可以转化为该类型,那么返回一个转化好的对象
如果转化不了则返回null
10. 集合
Add(),AddRange()
Revome(),RemoveAt()
Insert()
Clear()
Count属性
sort

 

//class Person
//{
// private string name;
// public String Name
// {
// get { return name; }
// }
// private int age;
// public int Age
// {
// get { return age; }
// }
// private char sex;
// public char Sex
// {
// get { return sex; }
// }
// public Person(string name, int age, char sex)
// {
// this.name = name;
// this.age = age;
// this.sex = sex;
// }
// public void Psay()
// {
// Console.WriteLine("我是皇帝我怕谁");
// }

//}
//class Teacher : Person
//{
// private string work;
// public Teacher(string name, int age, char sex, string work)
// : base(name, age, sex)
// {
// this.work = work;
// }
// public void Tsay()
// {
// Console.WriteLine("我叫{0},我的年龄是{1},我是{2}老师,我是教{3}的老师", Name, Age, Sex, work);
// }
//}
//class Student : Person
//{
// private string study;
// public Student(string name, int age, char sex, string study)
// : base(name, age, sex)
// {
// this.study = study;
// }
// public void Ssay()
// {
// Console.WriteLine("我叫{0},我今年{1}岁了,我是{2}同学,我是学{3}的学生", Name, Age, Sex, study);
// }
//}
class Program
{
//public static void Fun(Person p)
//{
// if (p is Teacher)
// {
// ((Teacher)p).Tsay();
// }
//else if (p is Person)
//{
// ((Person)p).Psay();
//}
//else if(p is Student)
//{
// ((Student)p).Ssay();
//}
//else
//{
// p.Psay();
//}
//else
//{
// Console.WriteLine("传进来的不是可用类型");
//}
//}
static void Main(string[] args)
{
//Teacher th = new Teacher("王刚", 45, '男', "计算机");
////th.Tsay();
//Student sh = new Student("小强", 18, '男', "C#");
////sh.Ssay();
////Console.ReadKey();
//Person ps = new Person("皇帝",43,'男');
////Person p = th;
////Teacher t = (Teacher)p;

////Person pp = sh;
////Student s = (Student)pp;
//Fun(th);
//Fun(sh);
//Fun(ps);

//Console.ReadKey();
//int num = 0;
//Console.WriteLine("输入一个数字");
//try
//{
// num = Convert.ToInt32(Console.ReadLine());
//}
//catch (FormatException ex)
//{

// Console.WriteLine(ex.Message);
//}
//catch(OverflowException ex)
//{
// Console.WriteLine(ex.Message);
//}
//Console.ReadKey();
//ArrayList arr = new ArrayList();
//string str = "";
//while (true)
//{
// Console.WriteLine("输入你想输入的");
// str = Console.ReadLine();
// if (str=="q")
// {
// break;
// }
// arr.Add(str);
//}
//arr.Insert(1, "f");
//for (int i = 0; i < arr.Count; i++)
//{
// Console.WriteLine(arr[i]);
//}
//arr.Remove("f");
//arr.RemoveAt(3);
//arr.Clear();
//arr.Sort();
//arr.Reverse();
//object o = arr[2];
//arr.AddRange(arr);
//Console.ReadKey();
//Hashtable hb = new Hashtable();
//hb.Add("ha","哈哈");
//hb.Add("123","呵呵");
//hb.Remove("ha");
//bool isre=hb.ContainsKey("hda");
//bool isf=hb.ContainsValue("哈哈");
//object b = hb["ha"];
//hb.Keys.Count();
//Console.ReadKey();
//List<string> l = new List<string>();
//Console.WriteLine("输入");

//for (int i = 0; i < l.Count; i++)
//{
// l.Add(Console.ReadLine());
//}
//Dictionary<string, string> d = new Dictionary<string, string>();
//for (int i = 0; i < d.Count; i++)
//{
// d.Add(i.ToString(),Console.ReadLine());
//}
//d.Keys.Count();

 


}
}

posted on 2011-11-23 23:55  奥萨瓦·本·萨卡  阅读(213)  评论(0编辑  收藏  举报