构造函数
class A
{
public A()
{
class A
{
public A()
{
}
}
与类名相同,无返回值,且在初始化之前调用。
A a=new A();
其中的A()就是构造函数。一般我们不写构造函数系统会自动生成一个无参且为空的构造函数。
A a
开辟了一个栈 里边用于放对象在内存中的地址
new A();
如果直接这样写生成了一个匿名对象,如果加到a 后边则a的栈放的是该内存的地址。
}
与类名相同,无返回值,且在初始化之前调用。
A a=new A();
其中的A()就是构造函数。一般我们不写构造函数系统会自动生成一个无参且为空的构造函数。
A a
开辟了一个栈 里边用于放对象在内存中的地址
new A();
如果直接这样写生成了一个匿名对象,如果加到a 后边则a的栈放的是该内存的地址。
初始化实例:
calss A
{
int age;
public int Age
{
get{return age;}
set{age=value;}
}
string name;
public string Name
{
get{return Name;}
set{Name=value}
}
//A类的构造函数
//this 代表但前对象。在一个类中this.age/this.Age如果没有约束则相同,有约束需用this.Age
public A(int age,string name)
{
this.Age=age;
this.Name=name;
}
}
class program
{
static void main(string args[])
{
//A a=new a();
//a.age=15 报错因为构造函数已经不是系统自带的了,而是我们写的带有参数的哪个,若要这样赋值则需在A中再写一个构造函数,无参且为空
//public A()
//{
//}
//而要初始化实例如:
A a=new A(15,"张三");
生成了一个实例a 且已经初始化
}
}
calss A
{
int age;
public int Age
{
get{return age;}
set{age=value;}
}
string name;
public string Name
{
get{return Name;}
set{Name=value}
}
//A类的构造函数
//this 代表但前对象。在一个类中this.age/this.Age如果没有约束则相同,有约束需用this.Age
public A(int age,string name)
{
this.Age=age;
this.Name=name;
}
}
class program
{
static void main(string args[])
{
//A a=new a();
//a.age=15 报错因为构造函数已经不是系统自带的了,而是我们写的带有参数的哪个,若要这样赋值则需在A中再写一个构造函数,无参且为空
//public A()
//{
//}
//而要初始化实例如:
A a=new A(15,"张三");
生成了一个实例a 且已经初始化
}
}
继承:
子类将获取父类的所有非私有的数据和行为。
子类有两个有效类型
自身的类型
获取父类的类型
(私有字段或方法可以继承但不能访问而已)
子类将获取父类的所有非私有的数据和行为。
子类有两个有效类型
自身的类型
获取父类的类型
(私有字段或方法可以继承但不能访问而已)
构造函数是不能继承的,每个类都有自己的构造函数。初始化子类会先调用父类的构造函数
例:
Class A
{
public A()
{
messageBox.show("A 出生了");
}
}
class B
{
public B()
{
messageBox.show("B 出生了");
}
}
class program
{
static void main(sting args[])
{
B b=new b();
先弹出A出生了,后弹出B出生了
}
}
例:
Class A
{
public A()
{
messageBox.show("A 出生了");
}
}
class B
{
public B()
{
messageBox.show("B 出生了");
}
}
class program
{
static void main(sting args[])
{
B b=new b();
先弹出A出生了,后弹出B出生了
}
}
子类继承父类,但初始化会调用父类无参的构造函数,如果没有则编译报错,除非子类中能自己调用父类的有参的构造函数。
calss A
{
int age;
public int Age
{
get{return age;}
set{age=value;}
}
string name;
public string Name
{
get{return Name;}
set{Name=value}
}
public A(int age,string name)
{
this.Age=age;
this.Name=name;
}
}
class B
{
string hobby;
public string Hobby
{
get{return hobby}
set{hobby=value}
}
//public B()
//{
//}
//报错
public B(int age,string name,string hobby):base(age,name)//继承父类有参的构造函数
{
this.hobby=hobby;
}
}
class program
{
static void main(sting args[])
{
B b=new b(20,"峻桦","运动");
先调用A的构造函数,在调用B的构造函数。
}
}
calss A
{
int age;
public int Age
{
get{return age;}
set{age=value;}
}
string name;
public string Name
{
get{return Name;}
set{Name=value}
}
public A(int age,string name)
{
this.Age=age;
this.Name=name;
}
}
class B
{
string hobby;
public string Hobby
{
get{return hobby}
set{hobby=value}
}
//public B()
//{
//}
//报错
public B(int age,string name,string hobby):base(age,name)//继承父类有参的构造函数
{
this.hobby=hobby;
}
}
class program
{
static void main(sting args[])
{
B b=new b(20,"峻桦","运动");
先调用A的构造函数,在调用B的构造函数。
}
}
转载于:https://blog.51cto.com/xianyun0216/143343