构造器函数

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace SimpleClassExample
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("*******Fun with Class Typys******/n");
            Car chuck = new Car();
            chuck.PrintState();
            Car kun = new Car("kun");
            kun.PrintState();
            Car Marry = new Car("Marry",100);
            Marry.PrintState();
            Car myCar = new Car();
            myCar.petName = "Henry";
            myCar.currSpeed = 10;
            for (int i=0; i < 10; i++)
            {
                myCar.SpeedUp(5);
                myCar.PrintState();
            }

            Motorcycle c = new Motorcycle(5);
            c.SetDriverName("Tiny");
            c.PopAWheelly();
            Console.WriteLine("Rider name is {0} ",c.name);

            Console.ReadLine();
        }
    }
}
 

 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace SimpleClassExample
{
    class Car
    {
        public string petName;
        public int currSpeed;
        public Car()
        {
            petName = "chuck";
            currSpeed = 50;
        }
        public Car(string pn)
        {
            petName = pn;
        }
        public Car(string pn, int cs)
        {
            petName = pn;
            currSpeed = cs;
        }

        public void PrintState()
        {
            Console.WriteLine("{0} is going {1} MPH.",petName,currSpeed);
        }

        public void SpeedUp(int delta)
        {
            currSpeed += delta;
        }
    }
}
 

 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace SimpleClassExample
{
    class Motorcycle
    {
        public int driverIntensity;
        public string name;
        public Motorcycle()
        {

        }
        public Motorcycle(int intensity)
        {
            driverIntensity = intensity;
        }
        public void SetDriverName(string name)
        {
            this.name = name;
        }
        public void PopAWheelly()
        {
            for (int i = 0; i <= driverIntensity; i++)
            {
                Console.WriteLine("Yeeeeeee Haaaaawww");
            }
        }
     
    }
}
 

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值