回顾 枚举类型
遍历枚举类型中的名字
遍历枚举类型中的名字对应的值
结构体
多种数据类型结合 某一类型 比如人的信息
委托
相当于定 义一个没有函数体的函数和目标函数有相同的返回值和参数使用委托指向目标函数
using Consoleapp1011pm;
using System;
using System.Xml.Linq;
namespace Consoleapp1011pm
{
/*class Animal//定义了一个动物类
{
//构造函数 初始化对象 当我们new创建一个对象的时候就会执行构造函数 进行初始化
public Animal(string name1,int age1,int num2)
{
this.name = name1;//this指向的是当前创建的对象
this.age = age1;
this.num1 = num2;
}
//字段
public string name;
public int age;
public int num1 = 1;
//属性
public int num
{
get //get表示属性被访问的时候会执行改代码
{
Console.WriteLine("我是get,只要属性被读取就会执行");
return num1;//同时返回一个值
}
set //set表示当设置num值的时候会执行 value表示新的值
{
Console.WriteLine("新的值还没给num赋值",value);//响应式数据
}
}
//方法
public void Show()
{
Console.WriteLine("我叫{0},会打篮球,今年{1}", name, age);
}
}
class Programe
{
static void Main(string[] args)
{
Animal dog;//声明一个dog对象
Animal cat;//声明一个cat对象
dog = new Animal("小怪", 8, 123);
cat = new Animal("小小怪", 5, 1234);
dog.Show();
cat.Show();*/
/*dog = new Animal();//创建一个dog对象
dog.age = 13;
dog.name = "大名";
Console.WriteLine(dog.age);
dog.Show();
dog.num = 10;//执行set块
Console.WriteLine(dog.num);//执行get块*/
}
/*enum State
{
在线, 下线, 忙碌, 隐身
}
struct Student
{
public string name;
public int id;
}
delegate int GetNum(int num1, int num2);
static int max(int num1, int num2)
{
return 1;
}
static void Main(string[] args)
{
//枚举类型
State state1;
state1 = State.忙碌;
//遍历枚举类型中的名字
foreach (string name in Enum.GetNames(typeof(State)))
{
Console.WriteLine(name);
}
//遍历枚举类型中的名字对应的值
foreach (string num in Enum.GetValues(typeof(State)))
{
Console.WriteLine(num);
}
//结构体 多种数据类型结合 某一类型 比如人的信息
Student student1;
student1.name = "小明";
student1.id = 12;
//委托 相当于定 义一个没有函数体的函数和目标函数有相同的返回值和参数使用委托指向目标函数
GetNum getNum;
getNum = max;
getNum(10, 11);
*/
//练习题1
//编写一个程序,定义结构(有姓名,年龄,手机号码三个字段),再定义一一个-维数组,把结构作为数组元索类型,存入数据,然后依次输出。
/*struct Information
{
public string name;
public int id;
public int num;
}
static void Main(string[] args)
{
Information infor1;
infor1.name = "小明";
infor1.id = 12;
infor1.num =12;
Information infor2;
infor2.name = "小明";
infor2.id = 12;
infor2.num = 12;
Information infor3;
infor3.name = "小明";
infor3.id = 12;
infor3.num = 12;
Information[] infor = new Information[3] { infor1, infor2, infor3 };
foreach(Information i in infor)
{
Console.WriteLine(i.name);
Console.WriteLine(i.id);
Console.WriteLine(i.num);
}*/
}
}
定义一个车辆( Vehicle )类,具有Run、Stop等方法,具有Speed(速度)、MaxSpeed (最大速度)、Weight (重量)等域(也叫做字段)。
使用这个类声明一个变量(对象)
引用
using _Car;
namespace YourCar {
class Programe
{
static void Main(string[] args)
{
Vechicle car;
car = new Vechicle(10, 100, 50);
car.Run();
}
}
}
namespace _Car;
//创建一个基类
public class Enemy
{
public Enemy(int life)
{
this.life = life;
}
public int life = 1;
public virtual void move()//规定虚方法
{
Console.WriteLine("怪物移动" + life);
}
}
//定义一个车辆( Vehicle )类,具有Run、Stop等方法,具有Speed(速度)、MaxSpeed (最大速度)、Weight (重量)等域(也叫做字段)。
//使用这个类声明一个变量(对象)
public class Vechicle
{
//构造方法
public Vechicle(int Speed, int MaxSpeed, int Weight)
{
this.Speed = Speed;
this.MaxSpeed = MaxSpeed;
this.Weight = Weight;
}
public int Speed;
public int MaxSpeed;
public int Weight;
public void Run()
{
Console.WriteLine("当前以{0}速度往前行驶,行使的最大速度是{1}", Speed, MaxSpeed);
}
class Programe
{
static void Main(string[] args)
{
/*//匿名类型 var
int b = 1;
string c = "ad";
var a = c;//匿名类型的类型由赋值的值的类型决定*/
}
}
}
public class Enemy
{
public Enemy(int life)
{
this.life = life;
}
public int life = 1;
public virtual void move()//规定虚方法
{
Console.WriteLine("怪物移动" + life);
}
}
//创建一个派生类(子类)
class Boss : Enemy
{
//虛方法(重写父类中的方法) 机制 调用move方法的时候如果子类中有move方法了就调用子类中的move方法
public override void move()//override重载
{
Console.WriteLine(1);
}
internal class Program
{
static void Main(string[] args)
{
Enemy enemy = new Enemy();
enemy.move();
Boss boss = new Boss();
boss.move();
}
}
}
class MyMath
{
public double PI = Math.PI;
public void C(double r)
{
Console.WriteLine(2 * r * PI);
}
public void S(double r)
{
Console.WriteLine(r * r * PI);
}
public void V(double r)
{
Console.WriteLine(4 * r * r * r * PI/3);
}
static void Main(string[] args)
{
Console.WriteLine("请输入圆的半径:");
double r1 = Convert.ToDouble(Console.ReadLine());
MyMath circle = new MyMath();
Console.WriteLine("圆的周长为:");
circle.C(r1);
Console.WriteLine("圆的面积为:");
circle.S(r1);
Console.WriteLine("圆的体积为:");
circle.V(r1);
}
}