VB.net 2010 视频教程 VB.net 2010 视频教程 python基础视频教程
SQL Server 2008 视频教程 c#入门经典教程 Visual Basic从门到精通视频教程
当前位置:
首页 > c#编程 >
  • C#实例教程之汽车参数小实验

汽车参数小实验

  using System;
  namespace dirtysalt
  {
    public class Car//class of car,has attribute of 'weight' and 'speed'
    {
        private int weight;
        private int speed;
        public Car(int Weight,int Speed)
        {
            weight=Weight;
            speed=Speed;
        }
        public void setweight(int Weight)
        {
            weight=Weight;
        }
        public void setspeed(int Speed)
        {
            speed=Speed;
        }
        public int getspeed()
        {
            return speed;
        }
        public int getweight()
        {
            return weight;
        }
      };      


      public class Sportcar:Car//inherit class of Car,has  attributes of 'weight','speed','color'
    {
        private string color;
        public  Sportcar(int Weight,int Speed,string Color):base(Weight,Speed)
        {
            setweight(Weight);
            setspeed(Speed);
            color=Color;
        }
        public void setcolor(string Color)
        {
            color=Color;
        }
        public string getcolor()
        {
            return color;
        }
        public static void Main()
        {
            Car car=new Car(100,100);
            Sportcar sportcar=new Sportcar(100,200,"blcak");//here has a problem
            Console.WriteLine("小汽车的重量是: "+car.getweight());
            Console.WriteLine("小汽车的速度是:"+car.getspeed());
            Console.WriteLine("跑车的重量是: "+sportcar.getweight());
            Console.WriteLine("跑车的速度是:"+sportcar.getspeed());
            Console.WriteLine("跑车的颜色是:"+sportcar.getcolor());
        }
      }      
  }

以上代码中,首先创建一个类car public class Car//class of car,has attribute of 'weight' and 'speed'

    {
        private int weight;
        private int speed;
        public Car(int Weight,int Speed)
        {
            weight=Weight;
            speed=Speed;
        }
        public void setweight(int Weight)
        {
            weight=Weight;
        }
        public void setspeed(int Speed)
        {
            speed=Speed;
        }
        public int getspeed()
        {
            return speed;
        }
        public int getweight()
        {
            return weight;
        }
      };      

这里面的weight和speed是两个重要属性,分别代表重量和速度,在构造函数里面就通过参数进行赋值,然后后的方法是进行速度和重量更新,以及返回速度和重度

 public class Sportcar:Car//inherit class of Car,has  attributes of 'weight','speed','color'
    {
        private string color;
        public  Sportcar(int Weight,int Speed,string Color):base(Weight,Speed)
        {
            setweight(Weight);
            setspeed(Speed);
            color=Color;
        }
        public void setcolor(string Color)
        {
            color=Color;
        }
        public string getcolor()
        {
            return color;
        }
        public static void Main()
        {
            Car car=new Car(100,100);
            Sportcar sportcar=new Sportcar(100,200,"blcak");//here has a problem
            Console.WriteLine("小汽车的重量是: "+car.getweight());
            Console.WriteLine("小汽车的速度是:"+car.getspeed());
            Console.WriteLine("跑车的重量是: "+sportcar.getweight());
            Console.WriteLine("跑车的速度是:"+sportcar.getspeed());
            Console.WriteLine("跑车的颜色是:"+sportcar.getcolor());
        }
      }     

这里创建了一个sportcar类,继函了car类,也自己写了Sportcar、setcolor、 getcolor等方法,其作用分加紧是构造函数、设置跑车颜色和获取跑车颜色,量后由main方法进行调用



相关教程