枚举:值的集合
结构:变量的集合
接口:方法的集合:只定义,不具体实现,被继承时,需要全部实现其所定义的方法
\\\
枚举:值的集合
namespace caEnum
{
enum Gender
{
男,
女
}
class Program
{
static void Main(string[] args)
{
Gender sex;
string a = Gender.男.ToString();
string s = Console.ReadLine();
//(自定义枚举名)(Enum.Parse(typeof(自定义枚举名),s));
try
{
sex = (Gender)(Enum.Parse(typeof(Gender),s));
Console.WriteLine("你输入的性别为:" + sex);
}
catch
{
Console.WriteLine("你输入的性别有误!");
}
}
}
}
结构:变量的集合
namespace caEnum
{
public struct Person
{
public string name;
public int age;
public Gender sex;
}
public enum Gender
{
男,
女
}
class Program
{
static void Main(string[] args)
{
Person p1 = new Person();
p1.name = "张三";
p1.age = 19;
p1.sex = Gender.男;
Person p2 = new Person();
p2.name = "李四";
p2.age = 18;
p2.sex = Gender.女;
Console.WriteLine("{0},{1},{2}",p1.name,p1.age,p1.sex);
Console.WriteLine("{0},{1},{2}",p2.name,p2.age,p2.sex);
}
}
}
\\\
接口:方法的集合:只定义,不具体实现,被继承时,需要全部实现其所定义的方法
namespace caEnum
{
public interface ISwim
{
void Swim();
}
public interface IFood
{
void Cook();
}
public abstract class Bird
{
public abstract void Fly();
}
//继承自一个抽象类,实现两个接口
public class Duck : Bird, IFood, ISwim
{
public void Swim()
{
Console.WriteLine("是鸭子就会游泳!");
}
public void Cook()
{
Console.WriteLine("鸭子经常被烧烤!");
}
public override void Fly()
{
Console.WriteLine("只有野鸭才会飞!");
}
}
class Program
{
static void Main(string[] args)
{
Duck d = new Duck();
//Duck对象d可以使用3种类型的方法:
//1、自身定义的,2、父类定义的,3、接口定义的
d.Fly();
d.Cook();
d.Swim();
}
}
}