c# 第三课 类
类: 其中的成员变量和成员函数有public,private,protected,internal,protected internal五种
Access-modifiers;
public : the type or member can be accessed by any other code in the same assembly or another assembly that reference it.
private : the type or member can only be accessed by code in the same class or struct.
protected : the type or member can only be accessed by code in the same or struct, or in a derived class.
internal : the type or member can be accessed by any code in the same assembly,but not from another assembly.
protected internal : the type or member can be accessed by any code in the same assembly, or by any derived class in another assembly.
深拷贝和浅拷贝:
对于类来说,浅拷贝只是拷一个指针。拷贝与被拷贝的内容占用同一片内存区域,删除一个变量时,另一个变量也为空。深拷贝则是重新开辟一块内存区域。两片内存区域存相同的内容。删除其中一个变量时,对另一个变量没有影响。
this 关键字:refers to the current instance of an object. used in 5 ways.
1 public void SomeMethod(int hour)
{
this.hour = hour;
}
2 class myClass
{
public void Foo(otherClass otherObject)
{
otherObject.bar(this);
}
}
3 with indexers
4 to call oneoverloaded constructor from another
class myClass
{
public myClass(int i)
public myClass():this(42)
}
5 to explicitly invoke methods and members of a class, as a form of documentation:
public void Method(int y)
{
int x = 0;
x = 7;
y = 8;
this.z = 5;
this.Draw();
}
静态类不能实例化
静态类是密封的,不能从它派生类型
静态类不能包含非静态成员,也不能有构造方法。
面向对象的三个特点:封装,多态,继承。