1.get访问器与方法体类似,它必须返回属性类型的值,而set访问器类似于放回类型为void的方法,它使用成为value的隐式参数,此参数的类型时属性的类型。
2.结构的特点:
结构是值类型向方法传递结构时,结构时通过值的方式传递的,而不是作为引用传递的。
结构的实例化可以不同new运算符结构可以声明构造函数,但他们必须带有参数
一个结构不能从另一个结构继承
结构可以实现接口
在结构中初始化实例字段是错误的
3.结构的使用和类的使用区别
using System;
class MyClass
{
public int id;
public mystr str; //作为成员变量
}
public struct mystr //结构体
{
public int id;
public mystr(int idd=0)
{
id = idd;
}
}
class Test
{
public static void change(MyClass my1)
{
my1.id = 10;
}
public static void chan(mystr my2)
{
my2.id = 10;
}
public static void Main(string[] ags)
{
MyClass my = new MyClass();
Console.WriteLine(my.id);
change(my); //这个是引用类型
Console.WriteLine(my.id);
//mystr str=new mystr(11);
mystr str; //定义结构体
str.id = 0; //注意因为是值类型,作为局部变量所以需要初始化才能使用
Console.WriteLine(str.id);
chan(str); //结构体传递的是值
Console.WriteLine(str.id);
Console.WriteLine(my.str.id); //注意这个没有手动初始化
}
}
4.c#中的多态
using System;
class A
{
public int a;
public int b;
public virtual int Add()
{
return a+b;
}
}
class B : A
{
public override int Add() //重写基类方法
{
Console.WriteLine(base.Add()); //调用基类方法
int x = 5;
int y = 7;
return x + y;
}
}
class C : A
{
public override int Add()
{
int x = 4;
int y = 7;
return x + y;
}
}
class D: A
{
public override int Add()
{
Console.WriteLine(base.Add());//子类中调用基类方法
int x = 3;
int y = 7;
return x + y;
}
}
class Test
{
public void show(A a) //通过基类对象来调用子类方法(多态)
{
Console.WriteLine(a.Add());
}
public static void Main(string[] args)
{
B b = new B(); //创建对象
C c = new C();
D d = new D();
Test t = new Test();
t.show(b);
t.show(c);
t.show(d);
}
}
5.c#中的抽象类
C#和C++一样,如果一个类派生于一个抽象类但是并没有为基类的所有抽象方法提供实现代码,那么这个派生类也被认为是一个抽象类。C#要求这种类型的类也必须在类的头部显示地使用abstract关键字进行声明。
using System;
abstract class A //抽象类
{
public int a;
public int b;
public abstract int Add(); //抽象方法
}
class B : A
{
public override int Add() //重写基类方法
{
int x = 5;
int y = 7;
return x + y;
}
}
class Test
{
public void show(A a) //通过基类对象来调用子类方法(多态)
{
Console.WriteLine(a.Add());
}
public static void Main(string[] args)
{
B b = new B();
Test t = new Test();
t.show(b);
}
}
2.结构的特点:
结构是值类型向方法传递结构时,结构时通过值的方式传递的,而不是作为引用传递的。
结构的实例化可以不同new运算符结构可以声明构造函数,但他们必须带有参数
一个结构不能从另一个结构继承
结构可以实现接口
在结构中初始化实例字段是错误的
3.结构的使用和类的使用区别
using System;
class MyClass
{
public int id;
public mystr str; //作为成员变量
}
public struct mystr //结构体
{
public int id;
public mystr(int idd=0)
{
id = idd;
}
}
class Test
{
public static void change(MyClass my1)
{
my1.id = 10;
}
public static void chan(mystr my2)
{
my2.id = 10;
}
public static void Main(string[] ags)
{
MyClass my = new MyClass();
Console.WriteLine(my.id);
change(my); //这个是引用类型
Console.WriteLine(my.id);
//mystr str=new mystr(11);
mystr str; //定义结构体
str.id = 0; //注意因为是值类型,作为局部变量所以需要初始化才能使用
Console.WriteLine(str.id);
chan(str); //结构体传递的是值
Console.WriteLine(str.id);
Console.WriteLine(my.str.id); //注意这个没有手动初始化
}
}
4.c#中的多态
using System;
class A
{
public int a;
public int b;
public virtual int Add()
{
return a+b;
}
}
class B : A
{
public override int Add() //重写基类方法
{
Console.WriteLine(base.Add()); //调用基类方法
int x = 5;
int y = 7;
return x + y;
}
}
class C : A
{
public override int Add()
{
int x = 4;
int y = 7;
return x + y;
}
}
class D: A
{
public override int Add()
{
Console.WriteLine(base.Add());//子类中调用基类方法
int x = 3;
int y = 7;
return x + y;
}
}
class Test
{
public void show(A a) //通过基类对象来调用子类方法(多态)
{
Console.WriteLine(a.Add());
}
public static void Main(string[] args)
{
B b = new B(); //创建对象
C c = new C();
D d = new D();
Test t = new Test();
t.show(b);
t.show(c);
t.show(d);
}
}
5.c#中的抽象类
C#和C++一样,如果一个类派生于一个抽象类但是并没有为基类的所有抽象方法提供实现代码,那么这个派生类也被认为是一个抽象类。C#要求这种类型的类也必须在类的头部显示地使用abstract关键字进行声明。
using System;
abstract class A //抽象类
{
public int a;
public int b;
public abstract int Add(); //抽象方法
}
class B : A
{
public override int Add() //重写基类方法
{
int x = 5;
int y = 7;
return x + y;
}
}
class Test
{
public void show(A a) //通过基类对象来调用子类方法(多态)
{
Console.WriteLine(a.Add());
}
public static void Main(string[] args)
{
B b = new B();
Test t = new Test();
t.show(b);
}
}