c#第十课

109 阅读2分钟

类,对象

包含方法,属性,字段等,增加代码的复用

//声明一个车辆类
class Vehicle {
        public int speed;
        public int weight;
        public int MaxSpeed;
        public Vehicle(int speed1,int weight1,int maxspeed)
        {
            this.speed = speed1;
            this.weight = weight1;
            this.MaxSpeed=maxspeed;
        }
        public void run()
        {
            Console.WriteLine("我时速{0},重达{1}吨,最高时速{2}",speed,weight,MaxSpeed);
        }

        public void Stop()
        {
            Console.WriteLine();
        }
    }


//声明一个动物类
class Animal
{
   //构造函数  当我们new创建一个对象时,会执行构造函数,进行初始化
   public Animal(string name1,int age1,int num12)
   {
       this.name = name1;//this指向当前创建的对象
       this.age = age1;
       this.num1 = num12;
   }

   //字段
   public string name;
   public int age;
   public int num1=1;
   //属性
   public int num
   {
       get
       {
           Console.WriteLine("我是get,只要属性被读取,就会执行");
           return num1;//表示属性被访问的时候会执行代码块,同时返回一个值
       }
       set
       {
           num1 = value;
           Console.WriteLine("新的值还没给num赋值",value);//set表示设置/修改num值的时候会执行,value表示新的值
       }
   }

   //方法
   public void Show()
   {
       Console.WriteLine("我叫{0}会打篮球,今年{1}岁",name,age);
   }
       
    }
    
//在main方法中
  Animal dog =new Animal("小撒",20,120);
  dog.age = 18;
  dog.name = "泰迪";
  dog.num1 = 110;
  Console.WriteLine(dog.age);//18
  Console.WriteLine(dog.num1);//110
  Console.WriteLine(dog.name);//泰迪
  dog.Show();//我叫泰迪会打篮球,今年18岁
  
  
  Vehicle car;
  car = new Vehicle(120,1,130);
  car.speed = 120;
  car.run();//我时速120,重达1吨,最高时速130

继承

可以将公共部分抽离出来,写成一个父类,当需要使用时,就不需要重复写,直接继承父类即可,但一个类只能继承一个父类,并且不能继承父类的构造方法

//基类(父类)
     class Enemy
     {
       *//*  public Enemy(int life){
             this.life = life;
         }*//*
         public int life;
         public virtual void Move()
         {
             Console.WriteLine("怪物移动");
         }
     }

     //派生类(子类)
     class Boss:Enemy
     {
         //虚方法(重写父类中的方法)  机制:当调用Move方法时,如果子类中有,就掉用子类的,没有就调用父类的
         public override void Move()
         {
             Console.WriteLine(1);
         }
     }
     
      Enemy enemy = new Enemy(20);
      enemy.Move();//怪物移动
      Boss boss = new Boss();
      boss.Move();//1

练习

//声明一个MyMath类
public class MyMath
    {
        public MyMath(int r)
        {
            this.r = r;
        }
        public const double PI = 3.14;
        public int r;
        //周长
        public static double C(int r)
        {            
            double c = PI * 2 * r;
            Console.WriteLine("圆的周长为:{0}",c);
            return c;
        }
        //面积
        public static double Area(int r)
        {
            double s = PI * r * r;
            Console.WriteLine("圆的面积为:{0}", s);
            return s;
        }
        //体积
        public double V(int r)
        {
            double v = PI * 4 / 3 * r * r * r;
            Console.WriteLine("圆的周长为:{0}", v);
            return v;
        }
    }
    
     MyMath myMath = new MyMath(3);
     myMath.V(4);//体积
     MyMath.Area(4);//面积
     MyMath.C(2);//周长
     //圆的周长为:267.94666666666666
     //圆的面积为:50.24
     //圆的周长为:12.56